3 votes

Comment exclure des fichiers ou des dossiers verrouillés Dans un AppleScript qui sélectionne tous les fichiers d'un dossier particulier

Voici ma situation. J'ai créé un AppleScript pour nettoyer mon bureau. Il sélectionne tous les fichiers et dossiers de mon bureau, crée un nouveau dossier avec un nom et un format de date spécifiques, et déplace tous les fichiers de mon bureau vers le nouveau dossier. Ce script fonctionne à merveille jusqu'à ce que je me rende compte qu'il y a parfois des fichiers ou des dossiers que je souhaite conserver sur mon bureau. Ma solution consistait à ouvrir la fenêtre d'information pour chaque fichier ou dossier que je souhaitais conserver sur mon bureau, et à sélectionner l'option "verrouiller" dans la fenêtre d'information.

Mon problème est qu'à cause des fichiers verrouillés, le script ne peut pas s'exécuter complètement sans afficher un message d'erreur. Si je clique sur "OK" sur le message d'erreur, le script se termine et déplace tous les fichiers à l'exception de ceux qui sont verrouillés.

Je préférerais ne pas avoir à franchir cette étape supplémentaire en cliquant sur le bouton "OK". J'ai essayé d'ajouter des actions d'événements système au script pour cliquer automatiquement sur le bouton "ok", mais cela n'a pas fonctionné.

Je commence à penser que la seule vraie solution serait de ne pas sélectionner ces fichiers ou dossiers verrouillés. C'est là que je suis perdu. Quelqu'un peut-il m'indiquer comment éviter de sélectionner les fichiers verrouillés en premier lieu ?

enter image description here


Voici le script dans son intégralité.


set scriptPath to (load script file "Macintosh HD:Users:Smokestack:Library:Mobile Documents:com~apple~ScriptEditor2:Documents:Cleanup Desktop.scptd:Contents:Resources:Scripts:Current Time A.M. P.M. And Short Date.scpt")

tell scriptPath
    timeandDate() -- This will get the time and date in this format "05/31/2017 @ 9:10:48 PM" called from the loaded script file above
end tell

set timeandDate to the result -- This will copy the time and date results from the previous step and and set it as this new variable

tell application "Finder"
    if running then
        close every window
        activate
        make new Finder window
        set target of Finder window 1 to folder "Desktop" of folder "Smokestack" of folder "Users" of startup disk
    end if
    open Finder window 1
    activate
end tell

delay 1
tell application "System Events"
    key code 0 using (command down) -- This will select all files and folders on the desktop in the active finder window
end tell

tell application "Finder"
    set these_items to the selection
    set destinationFolder to make new folder at POSIX file "/Users/Smokestack/Jimz_Important_Stuff/Desktop_Cleanups/" with properties {name:timeandDate}
    move these_items to destinationFolder
    reveal destinationFolder
end tell

Voici une version révisée du script sans appeler les gestionnaires à partir de fichiers externes

on timeandDate()
    set CurrentTime to (time string of (current date))
    set AppleScript's text item delimiters to ","
    set theLongDate to (current date)
    set theLongDate to (date string of theLongDate)
    set currentMonth to (word 1 of text item 2 of theLongDate)
    set currentDay to (word 2 of text item 2 of theLongDate)
    set currentYear to (word 1 of text item 3 of theLongDate)
    set monthList to {January, February, March, April, May, June, July, August, September, October, November, December}
    repeat with x from 1 to 12
        if currentMonth = ((item x of monthList) as string) then
            set theRequestNumber to (text -2 thru -1 of ("0" & x))
            exit repeat
        end if
    end repeat
    set currentMonth to theRequestNumber
    set currentDay to (text -2 thru -1 of ("0" & currentDay))
    set theShortDate to (currentMonth & "/" & currentDay & "/" & currentYear) as string
    set CurrentTime to (time string of (current date))
    set CurrentTimeandShortDate to (theShortDate & " @ " & CurrentTime)
    set AppleScript's text item delimiters to ""
end timeandDate

timeandDate()

set timeandDate to the result

tell application "Finder"
    close every window
    activate
    make new Finder window
    set target of Finder window 1 to folder "Desktop" of folder "Smokestack" of folder "Users" of startup disk
    select every file of the front Finder window 
    delay 1
    set these_items to the selection
    set destinationFolder to make new folder at POSIX file "/Users/Smokestack/Jimz_Important_Stuff/Desktop_Cleanups/" with properties {name:timeandDate}
    try
        move these_items to destinationFolder
    end try
    reveal destinationFolder
end tell

Après avoir apporté quelques modifications au script, tant que les éléments verrouillés sur mon bureau ne sont que des dossiers et non des fichiers, ce script fonctionne à merveille. Cependant, il génère toujours une erreur s'il y a des fichiers individuels verrouillés.

3voto

Monomeeth Points 61435

Mon approche consisterait à placer une partie de votre code dans un fichier try et utiliser également le bloc on error d'ignorer ce message d'erreur spécifique, mais d'afficher un message s'il rencontre une autre erreur.

L'avantage de cette approche est que vous ne dites pas à votre script d'ignorer toutes les erreurs, juste au cas où quelque chose d'autre se produirait et que vous devriez en être conscient.

Pour y parvenir, essayez ce qui suit :

set scriptPath to (load script file "Macintosh HD:Users:Smokestack:Library:Mobile Documents:com~apple~ScriptEditor2:Documents:Cleanup Desktop.scptd:Contents:Resources:Scripts:Current Time A.M. P.M. And Short Date.scpt")

tell scriptPath
    timeandDate() -- This will get the time and date in this format "05/31/2017 @ 9:10:48 PM" called from the loaded script file above
end tell

set timeandDate to the result -- This will copy the time and date results from the previous step and and set it as this new variable

tell application "Finder"
    if running then
        close every window
        activate
        make new Finder window
        set target of Finder window 1 to folder "Desktop" of folder "Smokestack" of folder "Users" of startup disk
    end if
    open Finder window 1
    activate
end tell

delay 1
tell application "System Events"
    key code 0 using (command down) -- This will select all files and folders on the desktop in the active finder window
end tell

tell application "Finder"
    try
        set these_items to the selection
        set destinationFolder to make new folder at POSIX file "/Users/Smokestack/Jimz_Important_Stuff/Desktop_Cleanups/" with properties {name:timeandDate}
        move these_items to destinationFolder
        reveal destinationFolder
    on error error_message number error_number
        if the error_number is not -50 then
            display dialog error_message buttons {"OK"} default button 1
        end if
    end try

end tell

Maintenant, vous verrez que j'ai placé le deuxième bloc de tell application "Finder" à l'intérieur d'un try (vous remarquerez l'utilisation de try y end try . Dans ce bloc, j'ai inséré le code suivant :

    on error error_message number error_number
    if the error_number is not -50 then
        display dialog error_message buttons {"OK"} default button 1
    end if

Fondamentalement, il s'agit de devrait ont pour effet de dire au script qu'en cas d'erreur de -50 dans le Finder, il doit l'ignorer, mais que si ce n'est pas -50, il doit afficher l'erreur. (Je suppose que c'est l'erreur que votre script obtient - si ce n'est pas le cas, vous pouvez simplement remplacer le fichier -50 avec le numéro d'erreur correct.

Il est évident que je ne peux pas tester cela de mon côté, alors n'hésitez pas à me tenir au courant.

LesApples.com

LesApples est une communauté de Apple où vous pouvez résoudre vos problèmes et vos doutes. Vous pouvez consulter les questions des autres utilisateurs d'appareils Apple, poser vos propres questions ou résoudre celles des autres.

Powered by:

X