sirris Posted March 3, 2022 Posted March 3, 2022 Google released an excellent new shell-scripting tool called zx that allows you to write shell scripts in JavaScript/JS with a bunch of helpful modules: https://github.com/google/zx -- i.e. things like Fetch built in. I've been unsuccessful at getting a zx script (.mjs) to work as an external script in an Alfred workflow. I've chmod +x, ensured paths are correct, and it still won't execute. I've even tried using a bash script to initiate the zx script: #!/bin/sh /opt/homebrew/bin/zx /Users/username/myscript.mjs Any advice? I'd love to be able to use this versatile new tool with Alfred!
sirris Posted March 4, 2022 Author Posted March 4, 2022 Thanks much @vitor! Using External Script. Here's what I get with two attempts: #!/usr/bin/env zx console.log('Testing 123'); Gives error: [19:30:10.675] Logging Started... [19:30:12.617] ScrScriptipt[Hotkey] Processing complete [19:30:12.619] Script[Hotkey] Passing output '' to Run Script [19:30:12.632] ERROR: Script[Run Script] env: zx: No such file or directory [19:30:12.633] Script[Run Script] Processing complete [19:30:12.633] Script[Run Script] Passing output '' to Post Notification ... #!/opt/homebrew/bin/zx console.log('Testing 123'); Gives nothing: [19:33:06.696] Script[Hotkey] Processing complete [19:33:06.701] Script[Hotkey] Passing output '' to Run Script Both paths resolve at shell to zx. And I can run the script in terminal with: "./script.mjs". I use zsh as my default shell.
vitor Posted March 4, 2022 Posted March 4, 2022 I see zx depends on node, so you need to have it accessible too. Whatever environment you have set up in your shell’s startup files which makes node and zx available, you’ll have to replicate in the Workflow. That might be as simple as adding export PATH="/opt/homebrew/bin:/usr/local/bin:${PATH}" at the top or it might require something else if you use a node version manager. See understanding the scripting environment for an explanation and other options.
sirris Posted March 5, 2022 Author Posted March 5, 2022 @vitor, thank you! That makes perfect sense. For anyone else following, here's the line I added at top of my .mjs external script file: #!/opt/homebrew/bin/node /opt/homebrew/bin/zx For those less familiar, use 'which node' and 'which zx' at the terminal to discover the appropriate path for your binaries.
deanishe Posted March 5, 2022 Posted March 5, 2022 8 hours ago, sirris said: For anyone else following, here's the line I added at top of my .mjs external script file As a rule, the solution Vítor suggested (altering PATH) is a much better one. Adjusting the local environment to a script is preferable to hardcoding a script to a specific environment.
sirris Posted March 5, 2022 Author Posted March 5, 2022 6 hours ago, deanishe said: As a rule, the solution Vítor suggested (altering PATH) is a much better one. Adjusting the local environment to a script is preferable to hardcoding a script to a specific environment. Thanks, makes sense. Any advice on how to do so? I can't seem to add a PATH statement to my .mjs script (it's in JavaScript). I'm attempting to following this instruction: Method 3: Set PATH before calling your tools. I attempted putting it before the #! line-- it didn't work. Nor did creating a bash shell script (with the PATH statement) to run prior to my .mjs script.
giovanni Posted March 5, 2022 Posted March 5, 2022 you would add the PATH statement in Alfred before calling your script (e.g. in a script filter)
sirris Posted March 7, 2022 Author Posted March 7, 2022 Thanks @giovanni! I'm at a loss at how to do this, and still trigger my script with a hotkey:
giovanni Posted March 7, 2022 Posted March 7, 2022 Sorry I thought you would be calling the script from the bash, in which case it would be preceded by the PATH statement. Not sure how to do that if the script is in an external script object...
vitor Posted March 7, 2022 Posted March 7, 2022 You tell Alfred to use Language /bin/bash or /bin/zsh in the script editor and add export PATH="/opt/homebrew/bin:/usr/local/bin:${PATH}" as the first line, like you have in the screenshots. Under that, put the path to your script. If the script is in the Workflow directory, use ./myscript.mjs. . represents the current directory (.. is the parent), so ./myscript.mjs means “the myscript.mjs file in the current directory”.
sirris Posted March 8, 2022 Author Posted March 8, 2022 Thanks both. This worked great, after I copied the .mjs and node_modules folder into the Workflow folder. I could have also referenced an external file location just as easily, i.e., ~/Scripts/covert-epoch.mjs.
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now