Pages

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.

Aucun commentaire: