MacOS – Problem with empty spaces when executing shell commands in AppleScript

applescriptmacos

The following code to purge a directory recursively does not work because I cannot get past the Application Support folder.

try
    do shell script "rm -r ~/Library/Application\ Support/Google/Chrome/Default/Pepper Data/Shockwave Flash"
end try
  • If I use ~/Library/Application Support/Google... nothing happens. Which is expected.
  • If I use ~/Library/Application\ Support/Google... I cannot save the script due to the error message: Syntax Error Expected “"” but found unknown token.

How can I work around this error?

Best Answer

Try:

set pathwithSpaces to "/Users/John/Desktop/This is a test.docx"
do shell script "rm -r " & quoted form of pathwithSpaces