Shell, $KMVAR and Finder Path

I'm often having trouble with shell scripts and variables containing file paths.

Usually, it is enough to place the variable in quotes in the script text. But this time, I can't get it to work.

Tried single quotes. Double quotes. Tried adding the quotes within the variable. Tried placing the entire command string into a variable.

Content of path looks like this:

/Volumes/SamSSD-1TB/Dropbox/PWB - Meins/Fragebogen INCOMING/_Incoming/Höfling, Petra - ABC/Höfling, Petra - ABC (dragged) 15.pdf

If I prepend "echo" to the script and execute it, everything looks perfect. I can just copy what is echoed and paste it into Terminal and it executes perfectly.

Is there any reliable way of using a path variable from "For Each Finder Selection" in a shell script? Something that will always work?

This suggests to me that the problem is with the command, and not with the use of a KM Variable for the path.

Have you been able to get the Execute Shell Script to work with any hard-coded path?


Thank you, @JMichaelTX.

You mean a problem with the ENV_PATH variable, perhaps?

I just checked:

  1. The file "mdimport" is saved at /usr/bin/, and that path is contained in my KM ENV_PATH variable.
  2. When I change the script to execute only "mdimport -d2", I get this notification, which suggests that the command itself is working:


Good suggestion!

I just tried:

This also produces no output at all.

So you're right. It's actually not a file path problem.

If, in the above example, I add one letter to the path, so that it no longer points to an existing file, I actually get a warning:


If I run the command with the correct path in Terminal, I do get output - lots of it:

iMac:~ admin$ mdimport -d2 /Volumes/SamSSD-1TB/Dropbox/KPM\ -\ Meins/Fragebogen\ INCOMING/_Incoming/Hoefling\,\ Petra\ -\ ABC.pdf
2018-02-24 14:32:31.722 mdimport[19977:1579069] Imported '/Volumes/SamSSD-1TB/Dropbox/KPM - Meins/Fragebogen INCOMING/_Incoming/Hoefling, Petra - ABC.pdf' of type 'com.adobe.pdf' with plugIn /System/Library/Spotlight/PDF.mdimporter.
2018-02-24 14:32:31.724 mdimport[19977:1579069] Attributes: {
    ":EA:kMDItemWhereFroms" =     (
    ":MD:DeviceId" = 16777221;
    ":MD:kMDExtendedImportDictionary" =     {
    ":MD:kMDItemPath" = "/Volumes/SamSSD-1TB/Dropbox/KPM - Meins/Fragebogen INCOMING/_Incoming/Hoefling, Petra - ABC.pdf";
    ":PR:kMDItemUserModifiedDate" =     (
        "2018-02-12 14:16:19 +0000"
    ":PR:kMDItemUserModifiedUserHandle" =     (
    "_kMDItemContentChangeDate" = "2018-02-12 14:16:19 +0000";
    "_kMDItemCreationDate" = "2018-02-12 14:05:45 +0000";
    "_kMDItemCreatorCode" = 0;
    "_kMDItemFileName" = "Hoefling, Petra - ABC.pdf";

(snipped here)

For some reason, KM apparently doesn't treat this output as "results" to be "displayed in a window".


Found the solution:

"Include Errors" must be selected.

Apparently, KM interpreted the output delivered by mdimport as an error message and filtered it. Or something like that.

Thank you, @JMichaelTX for putting me on the right track. Who knows how much more time I would have wasted hunting down errors with paths, variable, single quotes and double quotes... :slight_smile:

1 Like

In unix, scripts (all programs really) have one input, called STDIN, and two outputs, called STDOUT and STDERR. STDIN is the input stream. STDOUT is the output stream, and STDERR is the errors output stream. The "Include Errors" in the Keyboard Maestro action means that the output from STDERR is included.

mdimport is outputting the debugging information you are requesting to the STDERR stream (which is normal for debugging information).


Ah! Thank you for the clarification. I've learned something.

Hey Leonard,

The canonical method of redirecting STDERR to STDOUT is to use the shell’s own redirect method:


<your-shell-command> 2>&1


mdimport -d2 ~/'Downloads/2018.03.01 · 13.11.45 { Gathered Items }/' 2>&1

Peter is doing that behind-the-scenes with the “Include Errors” option of the Execute a Shell Script action.

While there’s nothing wrong with using the option, I prefer to use the redirect in the code – so I can see at a glance what it’s doing.

Here’s a decent bit of reference: