Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change directory with spaces in applescript terminal?

I'm new to applescripts and I'm trying to automate a process, but how do you change directory through the script when there are spaces inside the directory? My commands should be correct but a syntax error keeps popping up:

Expected “"” but found unknown token.

Here is my script:

tell application "Terminal"
activate
do script "cd ~/Pictures/iPhoto\ Library"
end tell

I don't understand where it is wrong. It works fine on my terminal.

Thanks a bunch guys!!

UPDATE: this worked best!!

# surround in single quotes
tell application "Terminal"
    activate
    do script "cd  '/Users/username/Pictures/iPhoto Library'"
end tell
like image 213
user805981 Avatar asked Dec 22 '12 18:12

user805981


1 Answers

The are a few ways.

# escape the quotes with a backslash. AND Escape the first backslash for Applescript to accept it.
tell application "Terminal"
    activate
    do script "cd ~/Pictures/iPhoto\\ Library"
end tell

# surround in double quotes and escape the quotes with a backslash. 
tell application "Terminal"
    activate
    do script "cd \"/Users/username/Pictures/iPhoto Library\""
end tell

# surround in single quotes using quoted form of 
tell application "Terminal"
    activate
    do script "cd " & quoted form of "/Users/username/Pictures/iPhoto Library"
end tell
# surround in single quotes
tell application "Terminal"
    activate
    do script "cd  '/Users/username/Pictures/iPhoto Library'"
end tell

Also I do not thing the tild will expand when you use the quotes on the whole path. So you will need to get the user name another way.

Examples:

# inserting the user name. And surrond in brackets so the name and path are seen as one string before the quotes are added
set whoami to do shell script "/usr/bin/whoami"
tell application "Terminal"
    activate
    do script "cd /Users/" & quoted form of whoami & "/Pictures/iPhoto\\ Library"
end tell



tell application "System Events" to set whoami to name of current user
# inserting the user name. And surrond in brackets so the name and path are seen as one string before the quotes are added
tell application "Terminal"
    activate
    do script "cd /Users/" & quoted form of (whoami & "/Pictures/iPhoto Library")
end tell

As you can see there is more than one way to do any of this.

Or just quote the directory part.

Example.

tell application "Terminal"
    activate
    do script "cd ~" & quoted form of "/Pictures/iPhoto Library"
end tell
like image 197
markhunte Avatar answered Sep 21 '22 17:09

markhunte