Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Assistance to run scripts #42

Open
Jopp-gh opened this issue Oct 6, 2024 · 2 comments
Open

Assistance to run scripts #42

Jopp-gh opened this issue Oct 6, 2024 · 2 comments

Comments

@Jopp-gh
Copy link

Jopp-gh commented Oct 6, 2024

Great plugin!
Since I've zero experience with python and prefer to run than to open bash scripts, I need some assistance to improve your code.

On top of your code, I added:

from ulauncher.api.shared.action.RunScriptAction import RunScriptAction

an if-else handler on line 206:

                file_extension = os.path.splitext(path_name)
                if file_extension is ".sh":
                    on_enter=RunScriptAction(str(path_name))
                else:
                    on_enter=OpenAction(path_name)

therefore:

        def create_result_item(path_name: str) -> ExtensionSmallResultItem:
            return ExtensionSmallResultItem(
                icon="images/sub-icon.png",
                name=KeywordQueryEventListener._get_display_name(path_name, path_prefix),

                file_extension = os.path.splitext(path_name)
                if file_extension is ".sh":
                    on_enter=RunScriptAction(str(path_name))
                else:
                    on_enter=OpenAction(path_name)

                on_alt_enter=KeywordQueryEventListener._get_alt_enter_action(
                    preferences["alt_enter_action"], path_name
                ),
            )

VsCode marks my if file_extension is "sh": as wrong. Could you kindly correct my code?

@hillaryychan
Copy link
Owner

Hi @Jopp-gh

os.path.splitext returns a tuple so you need to unpack it to get the extension.

>>> path = '/path/to/my/file.sh'
>>> os.path.splitext(path)
('/path/to/my/file', '.sh')

Updating your if condition to the following should work:

_, file_ext = os.path.splitext(path_name)
if file_ext is ".sh":
    on_enter=RunScriptAction(str(path_name))
else:
    on_enter=OpenAction(path_name)

@Jopp-gh
Copy link
Author

Jopp-gh commented Oct 16, 2024

Thanks for your feedback!

Agreed, it should work, in my opinion, the if-else handler doesn't belong inside the return var() which seems to be an output (array/list) separated by commas ,

In my experience with other code, I prepare results before sending anything to the output.

The only way to add conditions might be to use return twice:

if VAR = ".sh":
 return var()
else:
 return var()

But this didn't compile for me, because the def create_result_item(path_name: str) -> ExtensionSmallResultItem: seems to expect an output, not additional operations. I'm stuck here:
fzf
It says: syntax error, invalid syntax, maybe you meant other operators like "==", ":=", "=" which isn't the case. This code is a hard nut for me.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants