2 votos

Borrar todos los archivos excepto algunos de una carpeta usando applescript

Estoy intentando escribir un script de Apple para borrar todo el contenido de una carpeta excepto un archivo (stage.txt). Este es el script que he escrito:

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

Pero recibo el siguiente error: "System Events" obtuvo un error: AppleEvent handler failed." número -10000

Por favor, ayuda.

PD: Si alguien conoce algún buen tutorial para iniciarse en Apple script, por favor que lo comente también. ¡¡¡Gracias!!!

1voto

UweTheSailor Puntos 1

Tuve un problema similar y lo solucioné de esta manera:

(*
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

Espero que esto te ayude a entender cómo manejar las excepciones. Estoy seguro de que hay formas más sofisticadas de hacerlo, pero para mí esto es suficiente. Saludos.

AppleAyuda.com

AppleAyuda es una comunidad de usuarios de los productos de Apple en la que puedes resolver tus problemas y dudas.
Puedes consultar las preguntas de otros usuarios, hacer tus propias preguntas o resolver las de los demás.

Powered by:

X