Pages

2 juin 2008

Item number in a list


on ItemNumber(theItem, theList)
repeat with NumItem from 1 to count theList
if theItem = item NumItem of theList then return NumItem
end repeat
return 0
end ItemNumber

>>Ouvrir le script dans l'éditeur

Convert a string to a list

on text2List(theText, theDelimiter)
set {tid, AppleScript's text item delimiters} to {AppleScript's text item delimiters, theDelimiter}
set theList to every text item of theText
set AppleScript's text item delimiters to tid
return theList
end text2List

>>Ouvrir le script dans l'éditeur

Write data to a file

on writeFile(theText, thePath, appendData)
try
set thePath to thePath as string
set idFile to open for access file thePath with write permission
if appendData is false then set eof of the idFile to 0
write theText to idFile starting at eof
close access id_file
on error
close access file thePath
return false
end try
end writeFile

>>Ouvrir le script dans l'éditeur

Read data from a file

on readFile(thePath)
try
set thePath to thePath as string
set idFile to open for access file thePath
set fileData to read idFile
close access idFile
on error errMsg number errNum
close access file thePath
return false
end try
return fileData
end readFile

>>Ouvrir le script dans l'éditeur

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