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.

2 juin 2008

Multiple find replace

on nFindReplace(theText, string1, string2) -- multiple search replace
repeat with i from 1 to the length of my string1
set AppleScript's text item delimiters to character i of string1
set theList to text items of theText
set AppleScript's text item delimiters to character i of string2
set theText to theList as text
end repeat
set AppleScript's text item delimiters to ""
return theText
end nFindReplace

>>Ouvrir le script dans l'éditeur

Find replace

on findReplace(theString, oldChar, newChar) -- search replace
set AppleScript's text item delimiters to oldChar
set theList to text items of theString
set AppleScript's text item delimiters to newChar
set theString to theList as text
set AppleScript's text item delimiters to ""
return theString
end findReplace

>>Ouvrir le script dans l'éditeur

Space to underscore (8/8)

script space2underscore -- Remplacer espace par underscore
property parent : deleteSpace
property string1 : " "
property string2 : "_"
end script

>>Ouvrir le script dans l'éditeur

Delete space (7/8)

script deleteSpace -- Supprimer espace
property string1 : " "
property string2 : ""
on myProcess(theText)
set theText to theText as text
findReplace(theText, my string1, my string2)
end myProcess
end script

>>Ouvrir le script dans l'éditeur

Ligature (6/8)

script Ligature -- replacer les entrelacés
property entrelaces : {{"æ", "ae"}, {"Æ", "AE"}, {"œ", "oe"}, {"Œ", "OE"}}
on myProcess(theText)
set theText to theText as text
repeat with i in entrelaces
set theText to findReplace(theText, item 1 of i, item 2 of i)
end repeat
end myProcess
end script

>>Ouvrir le script dans l'éditeur

Diacritical (5/8)

script diacritical -- Remplacer diacritiques
property parent : myupperCase
property string1 : "äëïöüâêîôûàèùéçñÄËÏÖÜÂÊÎÔÛÀÈÙÉÇÑ"
property string2 : "aeiouaeiouaeuecnAEIOUAEIOUAEUECN"
end script

>>Ouvrir le script dans l'éditeur

Upper case (4/8)

script myupperCase -- Mettre en majuscules
property string1 : "æœäåçéñöüàãõÿâêáëèíîïìóôòúûùabcdefghijklmnopqrstuvwxyz"
property string2 : "ÆŒÄÅÇÉÑÖÜÀÃÕŸÂÊÁËÈÍÎÏÌÓÔÒÚÛÙABCDEFGHIJKLMNOPQRSTUVWXYZ"
on myProcess(theText)
set theText to theText as text
nFindReplace(theText, my string1, my string2)
end myProcess
end script

>>Ouvrir le script dans l'éditeur

Upper case words first letter (3/8)

script upperCaseWordsFirstLetter -- Majuscules 1ère lettre des mots
property parent : myLowerCase
property string1 : "æœäåçéñöüàãõÿâêáëèíîïìóôòúûùabcdefghijklmnopqrstuvwxyz"
property string2 : "ÆŒÄÅÇÉÑÖÜÀÃÕŸÂÊÁËÈÍÎÏÌÓÔÒÚÛÙABCDEFGHIJKLMNOPQRSTUVWXYZ"
on myProcess(theText)
set theText to theText as text
set theText to continue myProcess(theText) -- script mylowerCase
set temp to ""
set AppleScript's text item delimiters to " "
--••• every word of "to-to pi,po" --> {"to", "to", "pi", "po"} 
-- every text item of "to-to pi,po" --> {"to-to", "pi,po"} 
set theWords to every text item of theText
set AppleScript's text item delimiters to ""
repeat with theWord in theWords
set firstChar to nFindReplace(first character of theWord, string1, string2)
if the length of theWord > 1 then
set temp to temp & firstChar & characters 2 thru -1 of theWord & " "
else -- mot d'une lettre
set temp to temp & firstChar & " "
end if
end repeat
return characters 1 thru -2 of temp as string -- effacement espace final
end myProcess
end script


>>Ouvrir le script dans l'éditeur

Upper case first letter (2/8)

script upperCaseFirstLetter -- Majuscules 1ère lettre
property parent : myLowerCase
property string1 : "æœäåçéñöüàãõÿâêáëèíîïìóôòúûùabcdefghijklmnopqrstuvwxyz"
property string2 : "ÆŒÄÅÇÉÑÖÜÀÃÕŸÂÊÁËÈÍÎÏÌÓÔÒÚÛÙABCDEFGHIJKLMNOPQRSTUVWXYZ"
on myProcess(theText)
set theText to theText as text
set theText to continue myProcess(theText) -- script lowerCase
set firstChar to nFindReplace(first character of theText, string1, string2)
if the length of theText > 1 then
set theText to firstChar & characters 2 thru -1 of theText
else -- mot d'une lettre
set theText to firstChar
end if
return theText
end myProcess
end script

>>Ouvrir le script dans l'éditeur

Lower case (1/8)

script myLowerCase -- Mettre en minuscules
property string1 : "ÆŒÄÅÇÉÑÖÜÀÃÕŸÂÊÁËÈÍÎÏÌÓÔÒÚÛÙABCDEFGHIJKLMNOPQRSTUVWXYZ"
property string2 : "æœäåçéñöüàãõÿâêáëèíîïìóôòúûùabcdefghijklmnopqrstuvwxyz"
on myProcess(theText)
set theText to theText as text
nFindReplace(theText, string1, string2)
end myProcess
end script

>>Ouvrir le script dans l'éditeur

Cette série de 8 scripts objets modifiant les caractères d'un texte (majuscule, minuscule, diacritique,etc.) illustre la notion d'héritage exposée dans >>Histoire d'héritage.

Round a number

-- Parameters : number as string, number of decimals as integer
-- result : real
on Smart_Round(theNumber, theDecimal)
return (round (theNumber * (10 ^ theDecimal))) / (10 ^ theDecimal)
end Smart_Round

Auteur : Daniel Varlet


>>Ouvrir le script dans l'éditeur

Remove duplicate items from a list

on removeDuplicateItem(theList)
set newList to {}
repeat with theItem in theList
if theItem is not in newList then set newList to newList & theItem
end repeat
return newList
end removeDuplicateItem


>>Ouvrir le script dans l'éditeur