Applescript Do Shell Script Issues

Hello All. I am rather new to applescripting but I am making some headway with a script I am using.

My trouble is that I am trying to execute a shell script that is not in the /bin/sh folder that Applescript Editor defaults to.

After a little searching I put in the path to the script, but I still get a command not found error.

Here is my trouble:

do shell script “/Library/Application Support/VMware Fusion/vmrun list”

I plan to pass the results to an if statement and take action under certain conditions.

It works just fine in the terminal, but for some reason after a hundred different iterations of this, I still cannot get it to work when in an applescript! Clearly there is something that I am missing, and any help would be most appreciated. Apologies for such a simple problem.


Model: Macbook Pro
AppleScript: 2.1.2
Browser: Chrome
Operating System: Mac OS X (10.6)


the shell treats space characters as parameter separators,
so you have to quote the full path

do shell script "'/Library/Application Support/VMware Fusion/vmrun' list"

or escape each space character with a backslash

do shell script "/Library/Application\\ Support/VMware\\ Fusion/vmrun list"

Stefan. Thanks a lot! I knew it was something silly. I’m still learning!

If you pass strings to do shell script, it’s a good thing to use quoted form of aString. (or you could escape your spaces with backslashes '').

do shell script quoted form of "/Library/Application Support/VMware Fusion/vmrun" & " list"
do shell script "/Library/Application\\ Support/VMware\\ Fusion/vmrun list"

however do not try to combine the arguments and the path to the executable into one string. The shell will think it’s the path to the executable file only!

Do not use:

do shell script quoted form of "/Library/Application Support/VMware Fusion/vmrun list"

Hope it helps,

EDIT: See StefanK was faster

A note: please post such questions into the AppleScript | OS X forum. Code exchange is the forum to share code

Moved the topic to the appropriate forum :smiley:

Why does one have to use two backslash characters to escape a space character; why not just one backslash? AppleScript indeed gives an error with the latter, saying it was expecting a single quotation mark (which I don’t understand). But in Terminal, one only needs one backslash.

Model: MacBook Pro (5,2)
AppleScript: 2.1.2
Browser: Firefox 28.0
Operating System: Mac OS X (10.6)


The first backslash escapes the second in the AppleScript text passed to ‘do shell script’. So while you see two of them on your screen, there is in fact only one in the text and this is used in the shell script to escape the space. :slight_smile:

Thanks, Nigel… I think. (My head is getting ready to explode! ;)) So if you’re not doing a “quoted form of,” then a backslash itself has to be escaped with a backslash? Is there something else that AppleScript uses a backslash for (by analogy corresponding to a shell treating space characters as parameter separators) that necessitates escaping a backslash, so that AppleScript won’t think it’s something else? If so, then what is that something else?

When using a do shell script scirpt there are two programming environments you have deal with with both different definitions of strings.

In AppleScript you can say that a string is between double quotes. Between these double quotes every character is considered to be part of the string. There is an exception that the backslash indicates that the next character is a special character and is not part of the string. It’s important that the next character is a special character otherwise it throw an error.

\\ = \ \r = return* \n = newline* \t = tab* \" = double quote \x = x is any other character; this is not a special character and throws and error
*these special characters depends on the settings of AppleScript editor

So to send a double quote or backslash to the shell we need to start then with a backslash.

That bash, do shell script, works similar to AppleScript C like strings is a common mistake. Bash on the other hand does have substitution mode. So in short every character that is followed and not defined as an field separator (variable IFS) and not a bash special character is part of the string. When you have a double quote the substitution mode is partially turned off when characters behind the single quotte is completely without command substition and every character expect the single quote is part of the string.

So in single quote:

Hello' 'World!

is the same as:

'Hello World!'

You can turn substitution mode on and of in the middle of a string, something that’s not possible in AppleScript. However because single quotation turns substitution completely off you have to be aware that you can’t escape characters either. So a string like “current application’s name” must be in bash something like

#no quotation Bash: current\ application\'s\ name AS==> "current\\ application\\'s\\ name" #double quotation Bash: "current application's name" AS: "\"current application's name\"" #single quotation Bash 'current application'\''s name' AS: 'current application'\\''s name'
Because there is no substitution in single quotation at all it’s the most safest way to quote and quoted form property has never failed me. At first It seems very difficult but after a while it’s even more common sense than the regular way of defining strings.

edit: the last string in more depth

We start the string with substitution mode completely off so bash will interpret every character to be part of the string until it reads again a single quote. So a '' has no meaning whatsoever and you can’t escape characters in any other way in single quotation which means we have to turn substitution mode on again before reaching the single quote inside our string. When we’re in substitution mode again we can escape the single quote of our string to be considered a special character. Right after that we turn substitution mode completely off again until the end of the string.

'current application’\''s name

red is substition mode off and green is substition mode on.

Thanks, DJ Bazzie Wazzie, I think I (almost) have it, or at least will completely after I re-read your terrific tutorial several more times.

DJ did of course mean to write backslashes in these examples.

Oops, I think it was getting late. I’ve corrected the code above.

Thanks Nigel