Jump to content

External script using google zx


Recommended Posts

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!

Link to comment

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.

Link to comment

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.

Link to comment

@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.

Link to comment
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.

Link to comment
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.

Link to comment

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”.

Link to comment

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...