Chris G.
Chris G.

Reputation: 25974

Apple script parameter with space in path

I am unable to do the following - as there is a space in the $MY_PATH

osascript -e "tell application \"Terminal\" to do script \"cd $MY_PATH\""

Can this be done. I know that in a normal sh script I need to do something like "$(pwd)" but I don't know how to do this, when passing it to an Apple script?

Upvotes: 0

Views: 1559

Answers (2)

Gordon Davisson
Gordon Davisson

Reputation: 126038

This is actually quite tricky, because you essentially have two levels of quoting/parsing to deal with: you have to get the path string from shell (your script) into AppleScript intact, then from AppleScript back into shell (the argument to cd). You could wrap it in multiple layers of quotes and let each translation unwrap one layer, but that gets messy quickly (especially since shell and AppleScript have significantly different quoting rules).

But there are a couple of tricks you can use: to get the path into AppleScript, pass it as an argument to the script instead of part of the script (see this previous answer to "osascript using bash variable with a space"), then use the AppleScript quoted form of to prep it to use with cd:

osascript \
    -e "on run(argv)" \
    -e "set my_path to item 1 of argv" \
    -e "tell application \"Terminal\" to do script \"cd \" & the quoted form of my_path" \
    -e "end" \
    -- "$MY_PATH"

UPDATE: I haven't used docker, and I don't have a setup to test with, but I think I see how to fix up the combined script. First, let me point out some problems and unnecessary confusion in your current script:

DOCKER_EVAL=$(printf "%s" 'eval $(docker-machine env default)')

...this is just an elaborate equivalent of DOCKER_EVAL='eval $(docker-machine env default)' -- the whole printf "%s" thing isn't actually doing anything. It's also not the what you should be using; that's DOCKER_EVAL='eval "$(docker-machine env default)"'; the double-quotes might not be necessary, but are recommended to avoid possible misparsing. Now look at this line:

SERVER_COMMAND=$(printf "%s;sh %q" "$DOCKER_EVAL" "$SERVER_PATH")

here the %q format string is asking for the SERVER_PATH to be "quoted", but it's being quoted in a form suitable for bash, not for AppleScript. Including that string in an AppleScript is probably what's causing the error (though as I said, I don't have a setup to test with).

There are several ways to solve this, but the easiest is probably to pass the DOCKER_EVAL command in the same way as the path:

DOCKER_EVAL='eval "$(docker-machine env default)"'
osascript \
    -e "on run(argv)" \
    -e "set docker_eval to item 1 of argv" \
    -e "set my_path to item 2 of argv" \
    -e "tell application \"Terminal\" to do script docker_eval & \"; sh \" & the quoted form of my_path" \
    -e "end" \
    -- "$DOCKER_EVAL" "$SERVER_PATH"

...alternately, you could include the eval "$(... bit directly in the AppleScript, but you'd wind up with multiple layers of escapes. This way's cleaner.

BTW, I don't know if the commands docker-machine env produces export the various variables, or just set them; if they're not exported, the sh shell won't inherit them; you may have to source it instead. But that's uglier in some ways, and hopefully won't be necessary.

Upvotes: 2

Zombo
Zombo

Reputation: 1

Perhaps this:

printf 'tell application "Terminal" to do script "cd %q"' '/a/path with spaces' |
osascript

Result:

tell application "Terminal" to do script "cd /a/path\ with\ spaces"

Or this:

printf 'tell application "Terminal" to do script "cd \x27%s\x27"' '/a/path with spaces' |
osascript

Result:

tell application "Terminal" to do script "cd '/a/path with spaces'"

Upvotes: 1

Related Questions