Keyboard Maestro + Shell Script =?

Hi,

I built a small shell script for Obsidian and it works perfectly fine when running it from the terminal. My initial plan has been to invoke it via a keyboard shortcut and I thought that Keyboard Maestro would be the perfect fit for this kind of problem. So I built a little macro that only works in Obsidian using the “Execute Sheet Script” action, set the parameters to “execute script file” and pointed to the script.

When I try to run it nothing happens. When I replace the script with a very simple one like an echo command it also works. So the script works and the macro works… however, when I put it together it does nothing.

Any ideas? I use Pandoc in my script, has KM issues with dependencies?

Thank you!

Sounds like a PATH problem. By default, KM searches only a few directories for executables (commands). You can expand that by setting the ENV_PATH variable according to @JMichaelTX’s answer to this question. Set it to the same value you get from running

echo $PATH

in Terminal and the macro should start working.

3 Likes

The bash environment executed in keyboard maestro is not the same as in your shell.

If you’re just using Pandoc, invoke this in your shell

which pandoc

And use the resulting location as how you call pandoc.

(On my machine it’s /usr/local/bin/pandoc…so it would be like /usr/local/bin/pandoc input_file.md -o pdf output.pdf

2 Likes

Thank you for both of your answers! However, I am fairly new to scripting, KM and so on. So if you don’t mind I’d post the script and what I try to achieve. Maybe anyone could give some assistance on my mistake? This would be great for learning purposes!

What do I try to do? I want a simple possibility to export an Obsidian note to pdf and I want to solve this with a combination of a shell script and KM. To do so I have to copy the note’s title and then invoke the script:

#!/bin/sh

cd ~/Documents/Obsidian/

var1=`pbpaste`

pandoc "${var1}.md" -s -o "${var1}.pdf"

mv "${var1}.pdf" ~/Library/Mobile\ Documents/com~apple~CloudDocs/Desktop

Fairly simple but it does what it should – at least when I run it from the terminal. As a newbie it took me a while to learn about variables and copying from the clipboard but when it worked I thought that I’ve achieved the hard part. Invoking it via KM can’t be difficult task, can it…?

So I chose the Execute Shell Script action, execute script file and chose the script (path: ~/Documents/Obsidian/Scripts/Obsidian_Export.sh).

What do I miss? Is the tilde the issue when pointing to the file in KM? Or is there an even bigger misunderstanding on my side regarding the capabilities of KM?

Thank you for any help!

Where is pandoc installed? In your Obsidian folder? Probably not.

When you run from the terminal, you probably have the Pandoc install folder in your path variable, which the terminal (actually something lower than that, but let’s just say the terminal for now) will check to see if it can find a file in when you don’t give it a full path to the Pandoc executable.

When you run it in Keyboard Maestro, it knows nothing about your normal path variable, so when it tries to run Pandoc in your Obsidian folder, I expect it won’t find it and it will fail.

That brings you back to exactly what @dustinknopoff suggested above, and the details about the path variable is what @drdrang was pointing you towards checking.

So, first thing’s first, change pandoc to use the full path to wherever you installed Pandoc. That’s got to be your first thing to address and it is probably easier for you to just set that out at this point than populating a path variable in Keyboard Maestro for a single use.

2 Likes

Thank you, this is interesting and indeed something I have not considered so far. However… my thinking was that KM should just run the script and not do the script itself. In other words only do what I do when typing

cd ~/Documents/Obsidian/Scripts/
./Obsidian_Export.sh

into the terminal but I guess this is not how things work?

It is already late in Europe so I probably wait until tomorrow to try what you, @dustinknopoff and @drdrang have suggested. Thanks!

However, is there a way to do what I initially had in mind?

It is that thought that is an inaccurate assumption for the reason described above by @drdrang.

In a way, but as I noted, it actually involves more effort to set things up so that your path works that way in your script than it does to simply be explicit about your Pandoc location.

1 Like

Thank you very much for all your help. @drdrang’s suggestion was the key. I definitely learnt a lot on this little adventure, incl. using variables in KM and variables for shell scripts in KM. :blush:

I am trying to run a script that calls Keyboard Maestro upon a successful Carbon Copy Cloner backup task completion.

My script runs fine from the terminal but does not run after the backup is completed. I also get an error from CCC at the end of the task “The postflight shell script exited with a non-zero exit status”

Here is my script (mostly copied from Keyboard Maestro). Anything I’m doing wrong?

#!/bin/sh
osascript -e 'tell application "Keyboard Maestro Engine" to do script "80C571FF-AFE7-4E5F-9B6D-2FBED621E040"'
exit 0

Any chance your Mac is locked and your macro involves UI interactions that would therefore not be available?

What is the return code it is generating?

Have you tried adding any logging in the macro?

Aso, please post new questions as new topics

Any chance your Mac is locked and your macro involves UI interactions that would therefore not be available?

The macro runs perfectly when triggered by the terminal, which I do so using the same exact command (2nd line of my script with “sh” in front) so I am sure the keyboard maestro permissions are all good.

What is the return code it is generating?

All I can tell is what CCC tells me which is “The postflight shell script exited with a non-zero exit status”. From what I can see CCC is not running the script at all, or not getting to the line for triggering Keyboard Maestro.

Aso, please post new questions as new topics

Sorry, noted. I have been chastised in the past on forums for starting new threads before searching for relevant/semi-relivant ones (albeit not here) so I er on the side of caution! Will take more liberty next time!

My point is not aboit permissions but about interacting with the UI which is not available when the Mac is locked.

Being able to run from terminal is not a test for this. You either need to run it from a command line scheduler with your Mac locked and check the results, or you choose ck your macro directly for any UI interaction.

No the Mac is not locked, I’m watching CCC complete its task and the Mac will always be running when this task is run as it’s manually triggered by me connecting the external drive.

What happens if you try different scripts? For example to trigger a different macro or a non-Keyboard Maestro-based script?

That should theoretically help you narrow down if it is a script running issue or a specific macro issue.

This is the first time I have dealt with a shell script as a file triggered by CCC (my only other exposure is embedded shell scripts in text expander) so I don’t have any other scripts to try. Do you or anyone have a basic script that could do something (eg display a notification or place a text file on the desktop, just something simple that is done by the script itself and not keyboard maestro) that I can confirm happened after my CCC task completes successfully?

This one liner (ignoring the shebang definition) should create a file called “hello_world.txt” in your Desktop folder. The file will contain the text “Hello World”. You can check the file creation timestamp to determine when it was created.

#!/bin/zsh
echo "Hello world" > "$HOME/Desktop/hello_world.txt"

Thanks a lot @sylumer! I have tried this and it is failing just the same. When I run your script from the terminal, I do get the text file on the desktop.

So, I have put in a support request to CCC to see if they have any suggestions and also on the off chance there is a bug or something. Will report back on their advice!

I have just had a read, and according to the CCC docs, I found, the script is excuted as the root user.

Did you check the root user desktop for the output file?

Also is the shell script set to be executable by the root user, and not just your personal user account?

CCC support confirmed that running a shell script that calls an apple script is not going to work. Suggestion is to use the built in scheduling functionality instead rather than using my task manager to remind me to plug in my backup drive. Will run with that for a while and see if it gets me where I need to be. Thanks @sylumer for your help!