Help With Running a Shell Command in KM

Hello,

Here is what I want to build: Shortcuts that keeps a counter of how many times times it was run, takes image from system clipboard, saves it to a specified folder named with the count and then runs a shell command with variables from user input and the count. I am almost there. Everything works except for inserting variables into the shell command and successfully running it.

image

And this is how I would hope the command to look like when passed into the terminal (when typed into the terminal, it works):

jj -i /Users/yanbarta/Library/Mobile\ Documents/iCloud~is~workflow~my~workflows/Documents/Answer_sheet.json -v "B" "7" -o /Users/yanbarta/Library/Mobile\ Documents/iCloud~is~workflow~my~workflows/Documents/Answer_sheet.json

But I want to replace "B" with variable: Answer and "7" with variable New_counter

Thanks for your help.

I'm far from a shell script expert, but is there any reason you're using so many quotes around each KM variable in the script? What happens when you use only one set of double quotes (i.e. "$KMVAR_Answer" and "$KMVAR_New_counter")?

Also, while not germane to your shell script question, I'm not seeing any reason to use a named clipboard to keep track of the count when you could just as easily increment a single counter variable each time you run the macro; as it is, you're already setting the "New counter" variable and "question counter" clipboard to identical values anyway.

Thanks for your answer. The many quotes are from desperation. I tried single quotes, double quotes, single and double quotes... nothing worked.

Thanks for the tip with variables. I only knew that named clipboard persist over restart but I just checked and variables do too. I will change it to variables only.

Hello, you can try to write your Shell script into a file and execute it. Write your script with action « write To file » as a .sh file and use the option inside execute Shell command action.

Hope I’m clear enought.

In that case, my next suggestion is to try a much simpler shell command like echo $KMVAR_Answer $KMVAR_New_counter just to make sure you're using the right references and that the shell is seeing the variable values you want.

Thanks everyone for contributing. I ended up solving the problem using the custom action from Execute a Script in Terminal - #5 by Tom

Here is the working script if anyone else struggled: