Hello
Stan asked me off line about two problems.
(1) He discovered that my code fail to extract globals when they are defined in a handler. Honestly, as I don’t see any interest to this kind of variable, I didn’t thought that they may exist. But the message make me think that sometimes I define properties in a script object. In such case, there is a tab in front of the word property as there is one in front of the word global in handlers or scripts. I edited the script accordingly.
(2) Stan asked about the use of «class ut16» in my script. To test it, I grabbed a script stored for months as .applescript. I have a lot of them because as they are large, the editor refused to store them as .scpt. All of them are stored as Utf16 so I used «class ut16» to read them. After receiving Stan’s posts I exported some other scripts and this time, the files are plain text ones. I don’t understand what made the difference. I edited the script so that it may work with both encodings.
# Edited according to Stan Cleveland comments. # Now works with scripts stored as text or as Utf16. # Now extracts properties and globals defined in handlers or script objects. # Extracts handlers defined in script objects.
set theScript to (path to desktop as text) & "work in progress 1.applescript" as alias # Utf16 one --set theScript to (path to desktop as text) & "essai - copie.applescript" as alias
set itsText to read theScript if (itsText does not contain linefeed & "on ") and (itsText does not contain linefeed & "property ") and (itsText does not contain linefeed & "global ") then try set itsText to read theScript as «class ut16» on error error "The file is not encoded as Utf16 !" end try if (itsText does not contain linefeed & "on ") and (itsText does not contain linefeed & "property ") and (itsText does not contain linefeed & "global ") then error "the file doesn't define any handler, global or property !" end if
set theProperties to {}
set maybe to my decoupe(itsText, {linefeed & "property ", linefeed & tab & "property "})
repeat with i from 2 to count maybe set maybe2 to my decoupe(maybe's item i, space) if item 2 of maybe2 starts with ":" then set end of theProperties to item 1 of maybe2 end if end repeat log theProperties # List of declared properties
set theHandlers to {} set maybe to my decoupe(itsText, {linefeed & "on ", linefeed & tab & "on "}) repeat with i from 2 to count maybe set maybe2 to maybe's item i set dropIt to false repeat with unwanted in {"run", "open", "idle", "quit", "reopen", "error"} if maybe2 starts with unwanted then set dropIt to true exit repeat end if end repeat if not dropIt then set end of theHandlers to item 1 of my decoupe(maybe2, "(") end if end repeat
log theHandlers # list of handlers
set theGlobals to {} set maybe to my decoupe(itsText, {linefeed & "global ", linefeed & tab & "global "}) repeat with i from 2 to count maybe set maybe2 to maybe's item i if maybe2 does not contain ", " then set end of theGlobals to item 1 of my decoupe(maybe2, {space, linefeed}) else # we have several globals in the same instruction # must drop possible comments set maybe2 to item 1 of my decoupe(maybe2, {" #", " --", linefeed}) # grab the list of globals available in maybe2 set theGlobals to theGlobals & my decoupe(maybe2, ", ") end if end repeat
theGlobals
#=====
on decoupe(t, d) local oTIDs, l set {oTIDs, AppleScript's text item delimiters} to {AppleScript's text item delimiters, d} set l to text items of t set AppleScript's text item delimiters to oTIDs return l end decoupe
#=====
Yvan KOENIG running El Capitan 10.11.1 in French (VALLAURIS, France) samedi 21 novembre 2015 10:12:45
|