0

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 
0

1 Answer 1

1

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 
2
  • @user3439894 Good question - though not related to the question here. I just copied a part of an AppleScript answer containing a lot of "s and back slashes.
    – klanomath
    Aug 20, 2016 at 16:00
  • @user3439894 I have tested it. The $ is obligatory in the context of my answer to Tetsujin's question! Now I have to explain it :-O... With NF i get a 10 and with $NF I get disk2 and I needed some diskXresult!
    – klanomath
    Aug 20, 2016 at 16:28

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .