Get "Execute a shell script" working

I have a very simple shell script:

#!/bin/sh
cd /Users/username/code/path/foldername
code .

and it simply does not work...

I tried to do it in the Execute text script field, I saved it as .sh as .bash files and tried the Execute script file option.
I also chmodded al over the place with 600, 755 but nothing works.

My question is very simply how can is start vscode in a specific folder

Anyone any idea?

I think you'll find that it's always the same issue when a question title includes "Shell Script". (The question is asked dozens of times every year, and you will find quite a lot of coverage of it here).

The point is that (fortunately for debugging, script sharing etc etc) an Execute Shell Script action gets a pristine vanilla instance of the shell.

It doesn't depend on, and not related to, the instance set up by other applications like Terminal.app, and it needs you to either:

  • Define a value for $PATH which includes the path to application which you want to call,
  • or directly supply the full path to applications like code

On this system, for example, I would replace:

code .

with

/usr/local/bin/code .


For a more generic and local approach, see, for example:

2 Likes

Awesome it works.

The strange thing in here is that whenever I type code(whatsoever) it simply starts in Terminal, in iTerm2 or where-ever, so providing the full path never popped up with me (until now).

10 points for your answer.

In my humble opinion if many users make the same mistake... It is not an user-issue.

1 Like

It's mainly a user-issue -- but an understandable one given the complexities. For example, in Terminal/iTerm you are in an "interactive shell", which reads different config files when it starts than a KM "Execute Shell Script" action, a "non-interactive" shell, does. That's just the way shells work.

The PATH issue in particular is documented on the action's Wiki page -- it isn't a particularly easy read, but that's because it's a complicated subject!

Indeed, a checkbox to enable it would have been nice.
#featurerequest

CaseClosed

A checkbox to enable what?

To enable the environment that is available in the "normal" terminal.

1 Like

It is a good habit to get into using full paths to applications when creating shell scripts for automation apps, because most Mac GUI apps can't access the $PATH variables for the Terminal at all.

This may come up again if you take a script and try to use it in a different app.

2 Likes

Thanks, good to know.

First Mac ever.....

Congratulations.

You have a lot of things to get used to, but with Keyboard Maestro and a few other goodies most things are manageable.

I have to agree with @Nando_Lutgerink here. There must be an easier way. After setting the ENV_PATH variable as described above I still couldn't get KM to run the script at all. We're not all linux gurus and it would be great if KM can automatically pull that in and use it in the execution of scripts.

For anyone battling with this like I did, after two hours of trying I ended up opening the application Terminal and pasting text into the terminal window.

1 Like