Nick Balistreri Nick Balistreri - 10 months ago 72
AppleScript Question

Applescript remove folder from directory with special characters and spaces

I have run into some installation problems with the new Sierra update.

I want to run a script that checks the version number and deletes a certain .mpkg file based on the version number because I am having a lot of customers running the wrong installation which is causing a lot of issues. I have tried multiple versions of this code and nothing seems to be working. My result in Applescript console is: "".

Any help would be greatly appreciated.

tell application "Finder"

set os_version to do shell script "sw_vers -productVersion"

if ((os_version as string) is equal to "10.12") then

do shell script (" rm -rf \"Step 1 Installer.mpkg\" ")


do shell script (" rm -rf \"Step 1 Installer (SIERRA ONLY).mpkg\" ")

end if
end tell

Answer Source

The problem is that when you run the rm command, you don't specify the directory the .mpkg file is in. It does not automatically look in the same directory the script is in. Instead, it looks in whatever the script's working directory is, which seems to be / (i.e. the top level of the system volume). You can use path to me to get the script's path, then you need to convert that to a POSIX path in quoted form to use in the shell, then get the parent directory name... Here's what I came up with:

set scriptPath to POSIX path of ((path to me) as string)
do shell script "rm -Rf \"$(dirname " & (quoted form of POSIX path of (scriptFile)) & ")/Step 1 Installer.mpkg\""

(and a similar variant for the other installer)

Warning: *I have not fully tested this, and it contains an rm -Rf command. Thus, if something goes wrong it could go very very wrong. Test well, on a system that you don't care about.