2

I am trying to write an Apple script to delete all the contents of a folder except one file (stage.txt). This is the script I've written:

tell application "System Events"
    if exists folder "~/Library/Application Support/ABC/CS" then
        delete (every item of folder "~/Library/Application Support/ABC/CS" whose name is not "stage.txt")
    end if
end tell

But I am getting the following error: "System Events got an error: AppleEvent handler failed." number -10000

Please help.

PS: If anyone knows any good tutorial to get started on Apple Script, kindly comment that as well. Thanks!

1 Answer 1

1

I had a similar problem and solved it this way:

(*
Apple Scritp to Delete Files in all Startup and auto-launch Folders
    with dialog box to choose Delete or Cancel
    Uwe @UweTheSailor May 2022
    
    Exception handling for Roguea files (the Piezo App)
    added by Uwe in July 2022
*)

-- Variable Declaration Launch Deamons
set xyz to alias "Macintosh HD:Library:LaunchDaemons:"
-- Counter i increment to repeat loop (for loop)
set i to 1
set listOfNames to {}
set ItemNr to 0
set RmFile to 0

-- Loop all folders and check if there are files in the Startup and Launch Folders:
repeat until i = 4
    -- empty the list before filling it up again!
    set listOfNames to {}
    -- Fill the List "listOfNames" with files in the folder:
    tell application "Finder"
        set filelist to every file of the xyz
        repeat with currentFile in filelist
            set currentFileName to (the name of currentFile)
            -- exception for the two files, do not copy into list and do not remove from folder
            if currentFileName ≠ "com.rogueamoeba.aceagent.plist" and currentFileName ≠ "com.rogueamoeba.acetool.plist" then
                copy currentFileName to the end of listOfNames
                set AppleScript's text item delimiters to {" " & return}
                set ItemNr to ItemNr + 1
            end if
        end repeat
    end tell
    -- Only show dialog when Folder is empty (=content of listOfNames is Zero)!
    if length of listOfNames = 0 then
        display dialog "    There are " & length of listOfNames & " files in the folder: 
    " & xyz with title "Folder is empty!" default button "OK" giving up after 5
        
        -- Only offer Delete if there are files in the folder! 
    else if length of listOfNames > 0 then
        -- Display a dialog window with buttons and auto Cancel if no interaction after 10 seconds:
        set theButton to button returned of (display dialog "The Folder contains " & length of listOfNames & " files as follows: 
" & listOfNames with title "Found Files in the " & xyz with icon caution buttons {"Cancel", "Delete"} default button "Cancel" giving up after 5)
        -- Move all Files to the Bin if Button Delete is pressed
        if theButton is "Delete" then
            tell application "Finder"
                repeat until ItemNr = 0
                    delete (item ItemNr of (xyz))
                    set ItemNr to ItemNr - 1
                end repeat
            end tell
        end if
    end if
    
    -- Increment i + 1 and move to the next folder
    set i to i + 1
    -- Update the variable xyz to the next folder
    if i = 2 then
        set xyz to alias "Macintosh HD:Library:StartupItems:"
    else if i = 3 then
        set xyz to alias "Macintosh HD:Library:LaunchAgents:"
    end if
end repeat

I hope this helps you to understand how to handle exceptions. I am sure there are more sophisticated ways to do that, but for me this is good enough. Cheers!

You must log in to answer this question.

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