I am trying to automate a bash script which calls pandoc. I would like to highlight a markdown file in finder, then select the 'md2tex' service, which will turn the markdown file into a .tex file. I have got this to run fine in the terminal. However, when I try to automate it, it doesn't work. When I open Automator, I create a new service, then a drag and drop 'run shell script' into the window on the right. Other options are shown in the window below. I have used the $@ symbol to refer to the variable name (name of file). The screenshot shows all of the options / settings.

enter image description here

I get a weird error message 'syntax error at -e line 1'. Any help appreciated.

1 Answer 1


First, change the Shell pop-up from /usr/bin/perl to /bin/bash. That's what's causing the error -- it's trying to run the script with perl, and it's not in valid perl syntax.

Second, the script itself needs some work. $@ expands to all arguments (in this case, the paths of all selected files), but your script expects just one item. If you don't want it to fail if multiple items are selected, you should either just have it operate on the first, or make it loop over all of the items. looping is usually what you want, and in fact when you add that action to your Automator service it should auto-fill a template for looping over the items. Next, you need to put double-quotes around all references to file paths (and variable references in general) or it'll be hopelessly confused by file/folder names with spaces. Next, the path(s) it's going to receive will include the full filename, including ".md", so adding another ".md" will make it fail. To get the output filename, you may want to remove the ".md" before adding ",tex".

Here's my quick stab at a script:

for f in "$@"
    if [[ "$f" = *.md ]]; then
        /path/to/pandoc -s --template=generic.latex "$f" "${f%.md}.tex"

You'll need to fill in the full path to the pandoc executable, in place of "/path/to/pandoc" in the script. You can probably fund this by running the command which pandoc in your regular shell.

I made it skip anything other than ".md" files; you might want to change that part. If so, you might need to generalize the "${f%.md}.tex" part -- that removes a ".md" extension before adding ".tex", but if the input file has some different extension it'll just tack on ".tex" in addition to whatever's there. Warning: don't use "${f%.*}.tex" because in some cases that'll remove part of the directory name the file's in!

Oh, and for actual use as a Finder service, you need to remove the "Get Selected Finder Items" action -- that's good for testing in Automator, but you need to remove it or it'll add whatever's selected in the Finder to what it's supposed to run on (probably meaning it runs twice on everything).

  • 1
    Also note that leaving in the Get Selected Finder Items action when the workflow runs as a Service will cause it to send the selected items to the the Run Shell Script action twice in this instance, first by the Service's settings and then by the Get Selected Finder Items action. BTW Nice answer! +1 May 19, 2018 at 12:30
  • 1
    @user3439894 Hmm, I was assuming the Get Selected Finder Items action would replace the list of items (usually harmless, but not always), but you're right: it adds to the list of items, meaning it'll usually run twice on each item. The exception is if you select some item(s) in the Finder, and then right-click on something other than the selection, and choose Services > yourservice; in that case, it'll run on both what was selected and what was right-clicked on. Not good in any case, just a little different from what I thought. May 19, 2018 at 20:37
  • Thanks. I have accepted this as it deals with my main query about passing variables. However, I still cannot get the script to work. The reason why I set the path to usr/bin/perl is because when I tried all other locations I got an error message that the command was not found. So I assumed (wrongly) that the perl location was the right one. On my computer pandoc is located at /usr/local/bin/pandoc. I've tried to address this issue by creating a symlink (with sudo) from pandoc to usr/bin/bash. However, I am not allowed to do this. Perhaps I should turn of System Integrity Protection? May 20, 2018 at 7:20
  • 1
    @NickRiches Do not turn off System Integrity Protection. Messing with the things that SIP protects is a good way to go from "my Automator action doesn't work" to "my Mac doesn't boot". Seriously, don't mess with it unless you know exactly what you're doing. It's also completely irrelevant to the problems you're running into. May 20, 2018 at 8:35
  • 1
    The script in the Run Shell Script action is a bash script, so the pop-up must be set to /bin/bash. If you're getting a file not found error, you probably need to specify the full path to pandoc inside the script. In your regular shell, run the command which pandoc, and it should print out the full path to the pandoc executable. Substitute that into the script where it says /path/to/pandoc (I just edited that into my answer). Then try it again. If it doesn't work, include the output from which pandoc in your next comment. May 20, 2018 at 8:36

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .