Pages

Affichage des articles dont le libellé est AppleScript : Tools. Afficher tous les articles
Affichage des articles dont le libellé est AppleScript : Tools. Afficher tous les articles

3 juin 2008

Chrono

script chrono
property time1 : null
property time2 : null
property theCount : null
on start() -- start chrono
set theCount to 0
set time1 to (the current date)
end start
on break() -- stop chrono
set time2 to (the current date)
activate
if theCount is not null then
display dialog (theCount as string) & plural(" item", theCount) & " proceded in " & formatSeconds(round (time2 - time1)) & " ." buttons {"OK"} default button 1 with icon 1 giving up after 10
else
display dialog "Process completed in " & formatSeconds(round (time2 - time1)) & " ." buttons {"OK"} default button 1 with icon 1 giving up after 10
end if
end break
on countLoop() -- increase counter
set theCount to theCount + 1
end countLoop
on formatSeconds(theSeconds)
set {h, m, s} to {theSeconds div 3600, (theSeconds mod 3600) div 60, (theSeconds mod 3600) mod 60}
return h & ":" & twoDigit(m) & ":" & twoDigit(s) as string
end formatSeconds
on twoDigit(theNumber) -- add zero before one digit number
if theNumber < 10 then set theNumber to "0" & theNumber
return theNumber as string
end twoDigit
on plural(theString, num) -- add "s" after a string if num> 1
if num > 1 then set theString to theString & "s"
return theString
end plural
end script

>>Ouvrir le script dans l'éditeur

Quantifie le gain de temps d'un operation automatisée vs "à la main" au prix d'un léger ralentissement si appel à la routine countloop()

tell chrono to start() -- Initialisation chronomètre : A placer au debut du script utilisateur
tell chrono to countLoop() -- incrementation  compteur : A placer (facultatif) dans le script utilisateur, par ex. dans une routine qui renomme des fichiers.
tell chrono to break() -- Arrêt du chronomètre : A placer à la fin du script utilisateur.

2 juin 2008

Debug a droplet

property debug : true
on open theDrop
aa()
try
repeat with theItem in theDrop
-- script principal
end repeat
on error errorMessage number errorNumber
zz("Error: " & the errorNumber & ". " & the errorMessage)
end try
end open

---============== DEBUG ==============
on aa() -- Creation fichier log
if debug then
set scriptPath to "Path: " & POSIX path of (path to me)
set scriptName to "Script: " & displayed name of (info for (path to me))
set currentDate to "Date/Time: " & (do shell script "date '+%d/%m/%y %H:%M:%S'")
set OSversion to do shell script "sw_vers"
set UserName to "User: " & (do shell script "logname")
set ASVersion to "AppleScript: " & (AppleScript's version as Unicode text)
set addLog to scriptName & return & scriptPath & return & currentDate & return & OSversion & return & ASVersion & return & UserName
do shell script "echo '" & addLog & "' >> ~/Desktop/debug.log"
end if
end aa
on zz(msg) -- Ecriture fichier log
if debug then
set theTime to do shell script "date '+%d/%m/%y %H:%M:%S'" -- Ex: 19/01/59  16:30:00"
do shell script "echo '" & theTime & " " & msg & "' >> ~/Desktop/debug.log"
end if
end zz
---============== /DEBUG ==============

>>Ouvrir le script dans l'éditeur

Debug an applet

property debug : false
(* ••• BOITE DE DIALOGUE •••
set btn to button returned of (display dialog "Lancer le script" buttons {"Annuler", "Debug", "OK"} default button 3)
if btn is "Annuler" then return
set debug to (btn = "Debug")
*)
aa()

try
-- script principal
on error errorMessage number errorNumber
zz("Error: " & the errorNumber & ". " & the errorMessage)
end try


---============== DEBUG ==============
on aa() -- Creation fichier log
if debug then
set scriptPath to "Path: " & POSIX path of (path to me)
set scriptName to "Script: " & displayed name of (info for (path to me))
set currentDate to "Date/Time: " & (do shell script "date '+%d/%m/%y %H:%M:%S'")
set OSversion to do shell script "sw_vers"
set UserName to "User: " & (do shell script "logname")
set ASVersion to "AppleScript: " & (AppleScript's version as Unicode text)
set addLog to scriptName & return & scriptPath & return & currentDate & return & OSversion & return & ASVersion & return & UserName
do shell script "echo '" & addLog & "' >> ~/Desktop/debug.log"
end if
end aa
on zz(msg) -- Ecriture fichier log
if debug then
set theTime to do shell script "date '+%d/%m/%y %H:%M:%S'" -- Ex: 19/01/59  16:30:00"
do shell script "echo '" & theTime & " " & msg & "' >> ~/Desktop/debug.log"
end if
end zz
---============== /DEBUG ==============

>>Ouvrir le script dans l'éditeur