Execute shell Script doesn't work where AppleScript does

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"

What am I doing wrong, or is this a bug?

What do you get out if you execute this shell script (set to display results in a window):

echo "$KMVAR_theShell"

-rob.

Turn on include errors, turn off Failure aborts macro, and display results in a window to see the results.

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.

See the Quoting Strings section of the Execute a Shell Script documentation.

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.

3 Likes

When I echo "$KMVAR_theShell", I get the same string as shown in my OP.

Yea, @peternlewis explained the problem and the fix, much better than I could have done :).

-rob.

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.

Thanks.

I tried your suggestion and still got errors unpacking the paths.

Then I tried a very basic test. I set the variable "Test" to "Test" and when I Execute Shell Script:

echo “$KMVAR_Test”

I get:

"“€"

This is strange. Is something corrupt in my setup? I'm using Version 11.0.3.

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?

I quit the Keyboard Maestro engine and relaunched and everything now works correctly. I must have corrupted something with my numerous shell attempts.

This works:
rsync -hvrt "$KMVAR_LittleSurPath"/ "$KMVAR_DisklavierPath"

I need the slash after the source to sync the entire folder.

Thanks.

1 Like

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".

A comparison of file dates decides the source and destination (they could be swapped) so I can't include the slash in the variable.

1 Like

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!

1 Like