1

I'm writing a script to pass a complete file path with spaces in the name to a remote computer over ssh, which needs to run using iTerm. It needs to happen through AppleScript embedded into a shell script so I can take control of iTerm, create new split views, etc.

I've simplified my script here purely to the section that isn't working. For demonstration purposes, and so people here might be able to try alternatives, I've changed the command in question to the 'cd' command, as the error is the same (it can't find the path due to spaces in it).

#!/bin/bash
PATH="$1"

ssh server@192.168.50.4 "osascript \
-e 'tell application \"iTerm\"' \
-e 'activate' \
-e 'set newWindow to (create window with default profile)' \
-e 'tell first session of current tab of current window' \
-e 'write text \"cd "$PATH"\"' \
-e 'end tell' \
-e 'end tell'"

This works fine if there's no spaces in the path. The remote machine opens up an iTerm window and the path is changed to cd path. However, if there's spaces in the name, such as…

Volumes/Work/My Folder With Spaces

…iTerm responds with -bash: cd: Volumes/Work/My: No such file or directory

I've tried using $@ and putting extra quotes around the $1 variable and/or the $PATH variable. Also, I know there's an AppleScript command that's something like "quoted form of (POSIX path)" but I have no idea how to integrate it here within the bash script.

1 Answers1

0

I just figured it out! the script I was writing was an After Effects render script. Turns out it became a lot simpler if I used EOF instead of starting each line with -e. It solved all the issues I was having with nested quotes, etc.

    #!/bin/bash
ssh server@192.168.50.4 osascript <<EOF
tell application "iTerm"
    activate
    set newWindow to (create window with default profile)
    select first window
    tell current session of current window
    end tell
    tell first session of current tab of current window
        write text "/Applications/Adobe\\\\ After\\\\ Effects\\\\ 2020/aerender -project \"$1\" -sound ON"
    end tell
end tell
EOF
  • Generally you need to escape characters special to the shell (such as spaces) or quote the string. The same escape character is also used in AppleScript strings, so those would need to be escaped as well. Your solution is called a here document, by the way. – red_menace Nov 23 '19 at 14:23
  • here document. interesting. Well you learn something everyday. Thanks! – markpaterson Nov 24 '19 at 02:22