Home > Mobile >  applescript to delete files in applications support directory
applescript to delete files in applications support directory

Time:11-22

As part of my uninstaller script, I'd like to delete my app's directory in ~/Application Support/My App Name.

My uninstaller script is Apple Script. I've tried the following:

tell application "Finder" to delete (POSIX file "~/Library/Application Support/My App Name")
do shell script \"
sudo rm -rf '~/Library/Application Support/My App Name'
\" with administrator privileges
"""

But none of these had any effect, my app's Application Support directory remains. How can I get this done?

CodePudding user response:

If you use System Events instead of Finder for file system operations, then you can freely use posix-style paths, including the tilde. Then your script needs very little changing:

tell application "System Events" to delete the item ¬
        "~/Library/Application Support/My App Name"

However, the System Events delete command mirrors the shell rm command, in that it permanently deletes files from the filesystem, without sending them to the trash.

Instead of supplying a hard-coded path to the Application Support folder, you can utilise the System Events property of the same name:

tell application "System Events" to delete the folder ¬
        "My App Name" in the application support folder

Tip: There are one or two Finder-specific capabilities that can only be achieved using Finder, namely accessing Finder windows, getting or setting selected files in Finder (by way of the selection property or the select command), revealing files (using the reveal command), and getting the insertion location property. But other than when you need to invoke one of these, it's best to avoid Finder as it's slow, buggy (so is System Events, but not as much), it doesn't handle posix-style paths, and it blocks Finder during any and all operations (and, since it's slow, it blocks for a potentially long time, or times-out altogether meaning you have to restart Finder). Even when invoking one of the Finder-specific functions, I typically have it perform literally that one task, making sure it returns either alias class file referneces, or plain text file paths (HFS-style colon-delimited paths are fine), rather than Finder file references. System Events can utilise alias references as well as HFS-style file paths.

CodePudding user response:

The main issue is that the tilde is not expanded.

In the user folder you don't need administrator privileges. Just get the folder with a relative path.

set applicationSupportFolder to path to application support folder from user domain
tell application "Finder" to delete folder "My App Name" of applicationSupportFolder
  • Related