Even more complicated.
The script read it correctly but when I tried to analyse it, the script didn’t found « on », «property» or «global».
Given that, I opened the text with Hexedit which told me that all characters are defined by at least two bytes so I thought that it is Utf16 encoded. I checked at the very beginning and saw $FF FE which are the well known BOM.
Worse, in my files the word property is preceded by a linefeed. In Stan file it’s preceded by a return but in other parts of its file there is also some return characters.
So, I added some instructions normalizing the line breaks before examining the read content.
# 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 theScript to (path to desktop as text) & "Dead Man Agent.applescript" as alias # Stan's one
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 ") and (itsText does not contain return & "on ") and (itsText does not contain return & "property ") and (itsText does not contain return & "global ") then
try
set itsText to read theScript as «class utf8»
on error
try
set itsText to read theScript as «class ut16»
on error
error "The file is not encoded as Utf16 !"
end try
end try
if (itsText does not contain linefeed & "on ") and (itsText does not contain linefeed & "property ") and (itsText does not contain linefeed & "global ") and (itsText does not contain return & "on ") and (itsText does not contain return & "property ") and (itsText does not contain return & "global ") then error "the file doesn't define any handler, global or property !"
end if
set theProperties to {}
set itsText to my remplace(itsText, return, linefeed)
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
#=====
(*
replaces every occurences of d1 by d2 in the text t
*)
on remplace(t, d1, d2)
local oTIDs, l
set {oTIDs, AppleScript's text item delimiters} to {AppleScript's text item delimiters, d1}
set l to text items of t
set AppleScript's text item delimiters to d2
set t to l as text
set AppleScript's text item delimiters to oTIDs
return t
end remplace
#=====
Yvan KOENIG running El Capitan 10.11.1 in French (VALLAURIS, France) samedi 21 novembre 2015 15:27:35