25/05/07
Des morceaux d'AppleScripts... #4

Billets précédents :
test activation UI (Accès universel)
-- test activation UI (Apple code) tell application "System Events" if UI elements enabled is false then tell application "System Preferences" activate set current pane to pane "com.apple.preference.universalaccess" display dialog "UI element scripting is not enabled. Check \"Enable access for assistive devices\"" buttons {"OK"} default button 1 with icon 2 return end tell end if end tell end tell
trouvé sur Gilles Carpentier's Research Web Site : test de l'activation de Accès Universel?… génial, c'est exactement ce que je cherchais.
Chronomètre en AppleScript
property t_command : quoted form of ("import time" & (ASCII character 10) & "print time.time()") set temps_Debut to (do shell script "python -c " & t_command) as real -- ---code -- set secondes_total to ((((((do shell script "python -c " & t_command) as real) - temps_Debut) * 100) + 0.5) div 1) / 100
trouvé sur les Forums MacBidouille : Chronomètre en AppleScript.
Un moyen efficace de chronométrer la vitesse d'exécution d'un script.
Fonction d'arrondissement à 2 chiffres après la virgule
Dans le billet : Des morceaux d'AppleScripts… #3, il était présentée une fonction d'arrondissement à 2 chiffres aprés la virgule ; en effet, comme le confirme AppleScript 2.1 Help: round : la commande round arrondit un nombre en nombre entier ou le coupe aux unités. Par défaut, round arrondit au nombre entier le plus proche. Vous avez la possibilité d'ajouter un paramètre facultatif afin de préciser d'arrondir à l'entier supérieur, inférieur, en tendant vers zéro ou au nombre le plus proche, or tout ceci ne permet pas, a priori, d'obtenir des décimales derrière la virgule?…
Sauf, si on fait tout simplement ainsi :
(round (3.1415926535 * 100)) / 100
pour obtenir 2 décimales derrière la virgule.
Les avantage étant : conserver le séparateur de décimales naturellement utilisé par le système en fonction des options de localisation ainsi que la simplicité et la légèreté du code. D'autres exemples sont disponibles sur MacScripter : How do I round a number to two decimal places?.
Diverses autres ressources
Ecrire dans un fichiers texte (logs)
on write_to_file(this_data, target_file, append_data) try set the target_file to the target_file as text set the open_target_file to ¬ open for access file target_file with write permission if append_data is false then ¬ set eof of the open_target_file to 0 write this_data to the open_target_file starting at eof close access the open_target_file return true on error try close access file target_file end try return false end try end write_to_file try --replace this line with your statements on error error_message number error_number set this_error to "Error: " & error_number & ". " & ¬ error_message & return -- change the following line to the name and location desired set the log_file to ((path to desktop) as text) & "Script Error Log" my write_to_file(this_error, log_file, true) end try
AppleScript Guidebook: Essential Sub-Routines - Writing to File.

