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: