Terminal command in Applescript not working

applescript

I am trying to get a terminal command to work in Applescript and I get the error "Expected end of line but found identifier." How can I fix this?

This is my code

try
    do shell script "defaults write com.apple.dock persistent-apps -array-add '{"tile-type"="spacer-tile";}'; killall Dock" with administrator privileges
end try 

Best Answer

You mustn't use some special characters (e.g. " or \) between the quotation marks after the do shell script part of Apple Scripts or you have to escape them with a back slash.

In your example the second quotation mark is interpreted as the end of the shell command: defaults write com.apple.dock persistent-apps -array-add '{ which is no valid line.

So escape the inner quotation marks with a back slash and your Apple Script should work:

try
    do shell script "defaults write com.apple.dock persistent-apps -array-add '{\"tile-type\"=\"spacer-tile\";}'; killall Dock" with administrator privileges
end try 

If you have a shell command like this:

... awk '/disk/ {gsub("\"",""); print $NF}' ...

you would have to escape each " and \ with a back slash:

try
    do shell script "... awk '/disk/ {gsub(\"\\\"\",\"\"); print $NF}' ..."
end try