I'm using the terminal command "rsync" to synchronize 2 folders based on the timestamp of a particular file. I'm using several steps to set the variable %theShell% to this command, which works in the terminal:
rsync -hvrt /Volumes/Little\ Sur\ Drive/Users/john/\ Xojo/LookIt/LookIt\ 24\ w-NJ/ /Volumes/Disklavier\ Boot/Users/john/Documents/Xojo/Lookit/LookIt\ 24\ w-NJ
This AppleScript works:
tell application "Keyboard Maestro Engine" to set myshell to getvariable "theShell"
do shell script myshell
However, this Execute Shell Script does nothing (no results), whether I use the quotes or not:
"$KMVAR_theShell"
Note that the first action has Process Nothing selected, otherwise Keyboard Maestro would be interpreting the \ itself which will change everything.
Result
building file list ... rsync: link_stat "/Volumes/Little\" failed: No such file or directory (2)
rsync: link_stat "/Sur\" failed: No such file or directory (2)
done
rsync: link_stat "/Drive/Users/john/\" failed: No such file or directory (2)
rsync: link_stat "/Xojo/LookIt/LookIt\" failed: No such file or directory (2)
rsync: link_stat "/24\" failed: No such file or directory (2)
rsync: link_stat "/w-NJ/." failed: No such file or directory (2)
rsync: link_stat "/Volumes/Disklavier\" failed: No such file or directory (2)
rsync: link_stat "/Boot/Users/john/Documents/Xojo/Lookit/LookIt\" failed: No such file or directory (2)
rsync: link_stat "/24\" failed: No such file or directory (2)
sent 21 bytes received 20 bytes 82.00 bytes/sec
total size is 0 speedup is 0.00
rsync error: some files could not be transferred (code 23) at /AppleInternal/Library/BuildRoots/289ffcb4-455d-11ef-953d-e2437461156c/Library/Caches/com.apple.xbs/Sources/rsync/rsync/main.c(996) [sender=2.6.9]
Which you can see the issue, the quoting is all over the place. And it will be, because quoting is extremely hard.
You really do not want to be sending a command as a string like that. A string containing a command does not make sense without additional context of how the string will be processed, which will depend on the various ways you could execute the command, as you have found out.
Instead, have the two paths in variables, with no quoting (no back slashes for the spaces for example), and then your command in Keyboard Maestro will be:
rsync -hvrt "$KMVAR_Source" "$KMVAR_Dest"
It's not impossible to have the command all in a single string, but it might as well be, you have to know how it will be processed in order to ensure the quoting is undone in the right way.
Peter, thanks for your insight. Yes, I get that same result, with the paths split up like that. From your explanation, Keyboard Maestro must be handling the POSIX transformation. I hadn't tried it without the quoting since I didn't see anything about POSIX in the Keyboard Maestro docs. So I hard coded the quoting without trying to use the two paths separately. I may try that out, but since the AppleScript method works, I might just leave it that way.
There's one other possibility. I have a macro that expands "KM" to "Keyboard Maestro". Could that be getting triggered when it appears in the shell script?
Keyboard Maestro does not do any translation. Environment Variables will be stored in UTF8.
Are you sure you are actually setting the Test variable?
It's possible the Keyboard Maestro variables database has become corrupted.
What does Display Text in Window show if you use %Variable%Test%. If you quit and relaunch the Keyboard Maestro editor, what does the Test variable in the Variables preferences pane show?
You can include that trailing slash in the variable, if it makes things easier/cleaner (and doesn't break another part of your macro, obvs!).
Have a look at the KM manual section on variable "Scope". Tightly scoping your variables, particularly using locals whenever you can, is good way of preventing such "oddities".
You could add it immediately before using the variable in the Shell Script action -- but it's a question of style and preference more than anything. If you're happy with what you're doing then that's the best way for you to do it!