Shell Script and Spaces Between Words

I have created a Keyboard Maestro macro which outputs to the clipboard and a window the macOS version, the name of the front app and the version number of the front app. For example, for Curio:

macOS 11.2.2 - Curio, version 14.4

I use a macro provided Christopher Stone for getting the macOS version, and found through a search the following terminal command "mdls -name kMDItemVersion /Applications/" to get the version number.

The macro below works fine where the app consists of only one word.

But if the app consists of two words (such as "Keyboard Maestro" it fails). I understand in Terminal/Shell Script it is necessary to prepend spaces with an "\" and the macro adds the "\ " to any spaces in the app name.

For apps with two names, the command I create to run in the shell script works fine if I copy it run it manually in Terminal but not through the shell script command in Keyboard Maestro. For example the macro creates for Keyboard Maestro:

mdls -name kMDItemVersion /Applications/Keyboard\

I would be grateful if I could have some help on how to amend my macro so that it can run if the app name has more than one word.

Keyboard Maestro Actions.kmactions (10 KB)

Hey Victor,

I always single-quote the entire path.

mdls '/Volumes/Firestorm/• TEST_FOLDER/Many_Files_24000/DIR WITH SPACES/File Name With Spaces.txt'

You can do a find/replace to escape spaces in the path, but I find that to be ugly, inelegant, and hard to read.


1 Like


Thank you for the reply. But amending the macro to add the single-quote stops the macro working at all - whether or not the app has one or more words.

I removed the search and replace to add a "\" and add the single-quotes to produce

mdls -name kMDItemVersion '/Applications/'

Running it manually in Terminal works, but not through the Keyboard Maestro script.

The Keyboard Maestro Engine Log produces the following error:

2021-02-28 08:17:34 Execute macro “applicaton ID copy” from trigger The Hot Key ⌃⌥⇧⌘F12 is pressed
2021-02-28 08:17:34 Task failed with status 1
2021-02-28 08:17:34 Task failed with status 1. Macro “applicaton ID copy” cancelled (while executing Execute Shell Script).

Any further help would be gratefully received.

Keyboard Maestro Actions.kmactions (7.6 KB)

Hey Victor,

I rewrote your macro a bit.

  • Changed method of getting macOS version to sw_vers.
  • Changed hard-coded /Application/ to use mdfind.
  • Cleaned up Execute Shell Scripts and associated variables.
    ⠀⠀- Fixed quoting of paths with double-quotes.
  • Run variable-based shell command using eval.
    ⠀⠀- A bare $KMVAR_ is not good practice when running an Execute Shell Script Action.

This is most of what I did, and I'll let you look through the macro to decipher the details.


Get macOS and Front Application Version Numbers 1.01.kmmacros (20 KB)


Thank you very much for rewriting the macro - although I cannot understand the various shell script commands you have used.

For all the Apple apps I have tried it does not provide the version number of the app. For example for Calendar:

macOS 11.2.3 - Calendar, version : could not find .

The Engine log stating the following error:

2021-03-12 10:02:39 Execute macro “Quick Macro for ⌥F1 [Quick Macro]” from trigger While the Hot Key ⌥F1 is down
2021-03-12 10:02:40 Task failed with status 1
2021-03-12 10:02:40 Task failed with status 1 In macro “Get macOS and Front Application Version Numbers 1.01” (while executing Execute Shell Script).

Isn't that just peachy – it works fine on my system.

What do you get when you run this in the

printf "$BASH\n\n$(bash --version)\n\n" | pbcopy

It will automatically get copied to the clipboard.