Concatenate KM variable with string in osascript shell script

Hi everyone. I am trying to concatenate a KM variable with a string inside an osascript in a KM shell script action. I'm trying with single / double quotes and escaping combinations but I can't seem to make it work.

The following works:

echo $KMVAR_activar;
osascript -e 'tell application "Finder" to set desktop picture to POSIX file ("/Users/user/Pictures/" & "I003" & ".jpg")'

The echo statement displays "I003" so the variable is set right; now I'd like to replace the literal "I003" with this variable. I've tried the following but it doesn't work (unknown token found):

echo $KMVAR_activar;
osascript -e 'tell application "Finder" to set desktop picture to POSIX file ("/Users/user/Pictures/" & $KMVAR_activar & ".jpg")'

What am I doing wrong? How can I make osascript recognize the KM variable and making this action work?

Thanks for helping!

See:

and


However, may I ask:  Why are you using a shell script with osascript?
Using a KM Action "[Execute AppleScript](https://wiki.keyboardmaestro.com/action/Execute_an_AppleScript)" is much simpler, and you can avoid the quoting issues.
1 Like

Thanks! I’ve managed to make it simple by using the “Execute Applescript” – the info showed I needed to pass the KM var to the applescript first. That made me understand how to use the shell script version (which I can use outside KM). Posting them both for future reference:

AppleScript version:

tell application "Keyboard Maestro Engine"
   set newVar to getvariable "activar"
end tell
set theFile to POSIX file ("/Users/user/Pictures/" & newVar & ".jpg")
tell application "Finder" to set desktop picture to theFile

Shell script version:

osascript -e 'tell application "Keyboard Maestro Engine"
   set newVar to getvariable "activar"
end tell
set theFile to POSIX file ("/Users/user/Pictures/" & newVar & ".jpg")
tell application "Finder" to set desktop picture to theFile'
1 Like

With the correct quoting your original version will work also:

osascript -e 'tell application "Finder" to set desktop picture to POSIX file ("/Users/tom/Pictures/Screenshots/" & '\""$KMVAR_activar"\"' & ".jpg")'

or

myvar=\"$KMVAR_activar\"
osascript -e 'tell application "Finder" to set desktop picture to POSIX file ("/Users/tom/Pictures/Screenshots/" & '"$myvar"' & ".jpg")'

or

printf -v myvar '"%s"' "$KMVAR_activar"
osascript -e 'tell application "Finder" to set desktop picture to POSIX file ("/Users/tom/Pictures/Screenshots/" & '"$myvar"' & ".jpg")'
1 Like

Great, glad you got it to work.

Just to be clear, you can also run the AppleScript outside of KM.

Just put it in Script Editor and save as a .scpt file.
You can then run this script file using the Script Editor, KM, the Apple Script menu, FastScripts, or, from the Script Editor, save the script as an application. It can then be executed just by double-clicking on it like any other app.

Hey Bruno,

Any special reason you’re wanting to use the shell?

When using AppleScript and the Finder it’s generally a good idea to stay away from POSIX Paths and Posix Files.

Posix Files work for some things, but it’s easy to run into unexpected situations where they don’t.

For bombproof safety use HFS paths and aliases.

set imageFileAlias to alias ((path to pictures folder as text) & "Desktop Image 22.jpg")
tell application "Finder" to set desktop picture to imageFileAlias

If you need to be able to run from the shell it’s a good idea to avoid quoting issues by using read:

read -r -d '' appleScr <<'EOF'

   tell application "Keyboard Maestro Engine"
      set actiVar to getvariable "actiVar"
   end tell

   set imageFileAlias to alias ((path to pictures folder as text) & actiVar & ".jpg")
   tell application "Finder" to set desktop picture to imageFileAlias

EOF

osascript -e "$appleScr"

This is safer and makes your script much more readable.

I’m getting the Keyboard Maestro variable value from the KM engine, so this script can be used outside of Keyboard Maestro.

(Keyboard Maestro shell variables are only available when a script is run from its own Execute a Shell Script actions.)

Regular shell variables can be expanded in read statements.

#!/usr/bin/env bash

imageName='Desktop Image 22'

read -r -d '' appleScr <<EOT
   set imageFileAlias to alias ((path to pictures folder as text) & "$imageName" & ".jpg")
   tell application "Finder" to set desktop picture to imageFileAlias
EOT

osascript -e "$appleScr"

This technique will also work with Keyboard Maestro shell variables when run from an Execute a Shell Script action:

read -r -d '' appleScr <<EOT
   set imageFileAlias to alias ((path to pictures folder as text) & "$KMVAR_imageName" & ".jpg")
   tell application "Finder" to set desktop picture to imageFileAlias
EOT

osascript -e "$appleScr"

-Chris

1 Like

Thanks!

Thanks Chris, didn’t know those alternative techniques, and limitations of using POSIX.