UPDATED: Ghostscript (Homebrew package) produces error while exact same string works in Terminal (solved but I don't know why 😅)

Howdy folks, I'm working on a macro to combine PDFs into one file using Ghostscript but am running into an issue.

The macro is setup fine (basically based on a template I use for similar macros), and the string that it produces works when ran from Terminal. Here is the string that is produced by the macro:

gs -dNOPAUSE -sDEVICE=pdfwrite -sOUTPUTFILE="/Users/cdthomer/Dropbox/Test Folder/combined.pdf" -dBATCH "/Users/cdthomer/Dropbox/Test Folder/1.pdf" "/Users/cdthomer/Dropbox/Test Folder/2.pdf"

But when that string is ran from the shell script action in the macro it produces the following error:

GPL Ghostscript 9.55.0 (2021-09-27)
Copyright (C) 2021 Artifex Software, Inc.  All rights reserved.
This software is supplied under the GNU AGPLv3 and comes with NO WARRANTY:
see the file COPYING for details.
Error: /undefinedfilename in (/Users/cdthomer/Dropbox/Test Folder/1.pdf /Users/cdthomer/Dropbox/Test Folder/2.pdf)
Operand stack:

Execution stack:
   %interp_exit   .runexec2   --nostringval--   --nostringval--   --nostringval--   2   %stopped_push   --nostringval--   --nostringval--   --nostringval--   false   1   %stopped_push
Dictionary stack:
   --dict:759/1123(ro)(G)--   --dict:0/20(G)--   --dict:75/200(L)--
Current allocation mode is local
Last OS error: No such file or directory
GPL Ghostscript 9.55.0: Unrecoverable error, exit code 1

I have verified that my ENV_PATH variable contains all folders where my scripts are installed so I don't believe that to be the issue. It almost looks like GS is dropping the enclosing quotation marks that are needed when using full file paths to point to the files to be combined but I'm not certain of that.

The macro is attached and hopefully somebody can shed some light on what the error is. All my other macros I've built like this the last few weeks I've been able to figure out any errors and correct them but this one has me stumped so far. Any help is appreciated!


UPDATE: I found a solution... but I don't know why it's a solution.

Calling on @ccstone to help me out if possible since the solution was part of a shell script he helped me with several weeks ago.

Instead of passing KM variables to the shell script in-line with the GS command, I setup the entire command as a KM variable and then ran that using bash as can be seen in the updated shell script below:

# Acquire the command string from Keyboard Maestro.

# For Eyballing/Debugging (comment-out `bash` line below).
# printf "$shCmdStr"

# Run the command.
bash -c "$shCmdStr"

Why does this work and my original method does not?

Updated macro below.

22)[MG-FileDocs] Combine PDFs.kmmacros (30 KB)

Macro screenshot (click to expand/collapse)

Because quoting in shell scripts can get really weird?


Haha that's what I ended chalking it up to and left it at that. :laughing:

Glad to have you back Chris!

1 Like