Testing element specifiers
Testing element specifiers
- Subject: Testing element specifiers
- From: Sean DeNigris <email@hidden>
- Date: Wed, 23 Jul 2008 14:18:34 -0400
Does anyone have any improvements or suggestions re the following
script?
I wrote it to programmatically test which element forms I can use with
a particular element, so I don't have to rely on the dictionary and
trial and error.
It works like this:
getElementSpecifiers([class to test as string], [container reference
as string], [property as string], [boundarySpecifier as string],
[command as string], [application name as string])
-- boundary specifier parameter is for "Range" per Terminology Reference
-- Result: a list of all the valid ways to get this element
Examples:
getElementSpecifiers("account", "", "port", "", "exists", "Mail")
-- Result: {"index", "some", "every", "thru range", "relative",
"attribute from list", "boolean", "attribute from boolean", "command
over list", "name"}
getElementSpecifiers("character", "text of document 1", "font",
"word", "exists", "TextEdit")
-- Result: {"index", "some", "every", "thru range", "to range",
"relative", "attribute from list", "boolean", "attribute from
boolean", "command over list"}
n.b. I'm using display dialogs to report errors at the moment because
I am not clear how applescript errors work i.e. will a certain kind of
error always give the same #? Is this effected by the targeted app?
If error #-1728 is thrown when an object does not have a certain
property, can I rely on that # always meaning that? Ultimately, I
will only catch the errors that tell that the specifier is either
valid or invalid, and rethrow the others
Script to run:
to getElementSpecifiers(forElement, ofParent, withProp,
boundaryObject, takingCommand, inApplication)
-- Open a script that returns a list of valid ways to specify an
element
tell application "Script Editor"
-- Open the document that contains the test code
set codeDoc to open "Disk:...:Get Element Specifiers Template.scpt"
set scriptTemplate to (text of codeDoc)
-- close codeDoc
end tell
-- Replace Mail-specific objects with user-supplies values using sed
in the shell
--
-- Build command to plug in user-suppled application
set replaceCommand to " -e 's/\"" & "TextEdit" & "\"/\"" &
inApplication & "\"/g'"
-- Build and add command to plug in user-supplied element
set replaceCommand to replaceCommand & " -e 's/" & "character" & "/"
& forElement & "/g'"
-- Build and add command to plug in user-supplied property
set replaceCommand to replaceCommand & " -e 's/" & "font" & "/" &
withProp & "/g'"
-- If user specified a boundary class for "to range" specifier
if boundaryObject is not "" then
-- Build and add command to plug in user-supplied property
set replaceCommand to replaceCommand & " -e 's/" & "word" & "/" &
boundaryObject & "/g'"
else
set replaceCommand to replaceCommand & " -e 's/" & "123456789" & "/1/
g'"
end if
-- Build and add command to plug in user-supplied command
set replaceCommand to replaceCommand & " -e 's/" & "exists" & "/" &
takingCommand & "/g'"
-- If no parent was supplied, set it to the Application (which is the
target of the script
if ofParent is "" then set ofParent to "it"
-- Build and add command to plug in parent
set replaceCommand to replaceCommand & " -e 's/" & "text of document
1" & "/" & ofParent & "/g'"
-- Execute sed with above commands
set codeToRun to do shell script "echo '" & scriptTemplate & "' |
sed" & replaceCommand
--display dialog "echo '" & "tmp" & "' | sed" & replaceCommand
-- Run script which is now set up for the values the user supplied
return run script codeToRun
--return codeToRun
end getElementSpecifiers
Script "Get Element Specifiers Template.scpt":
(*TODOS:
* let user know if a referenced must be dereferenced in any particular
situation
*)
tell application "TextEdit"
-- Create list to store valid specifiers
set specifierList to {}
(* Try to get element specifier by index *)
try
character 1 of text of document 1
-- If there was no error, add "Index" to list of valid specifiers
set end of specifierList to "index"
log "Index specifier added (Index valid)"
-- Save this element to use with property-based specifiers
set anElement to a reference to character 1 of text of document 1
on error msg number num partial result p from f to t
(*
-- If the index was out of range, elements can be specified by index
if num is -1719 and msg contains "Invalid index" then
-- so still add "Index" to list of valid specifiers
set end of specifierList to "Index"
log "Index specifier added (Index out of range)"
else*)
-- There was another error, so index may not be valid
tell me to display dialog "[index]: Error #" & num & ": " & msg & "
p: " & p & " t: " & t
--error msg number num partial result p from f to t
--end if
end try
(* Try to get element using "some" *)
try
some character of text of document 1
-- If there was no error, add "some" to list of valid specifiers
set end of specifierList to "some"
log "some specifier added"
on error msg number num partial result p from f to t
tell me to display dialog "[some]: Error #" & num & ": " & msg & "
p: " & p & " t: " & t
end try
(* Try to get all elements using "every" *)
try
every character of text of document 1
-- If there was no error, add "every" to list of valid specifiers
set end of specifierList to "every"
log "every specifier added"
on error msg number num partial result p from f to t
tell me to display dialog "[every]: Error #" & num & ": " & msg & "
p: " & p & " t: " & t
end try
(* Try to get element usings "thru range" *)
try
characters 1 thru -1 of text of document 1
-- If there was no error, add "range" to list of valid specifiers
set end of specifierList to "thru range"
log "thru range specifier added"
on error msg number num partial result p from f to t
tell me to display dialog "[thru range]: Error #" & num & ": " & msg
& " p: " & p & " t: " & t
end try
(* Try to get element usings "to range" *)
-- Other script will replace "123456789" with 1 if this test will not
be run
if 123456789 is greater than 2 then
try
try
characters from word 1 to word -1 of text of document 1
on error
characters from word 1 to word -1 of (get text of document 1)
log "Parent must be dereferenced for to range specifier"
end try
-- If there was no error, add "range" to list of valid specifiers
set end of specifierList to "to range"
log "to range specifier added."
on error msg number num partial result p from f to t
tell me to display dialog "[To Range]: Error #" & num & ": " & msg
& " p: " & p & " t: " & t
end try
end if
(* Try to get a relative element *)
try
character before character 2 of text of document 1
-- If there was no error, add "relative" to list of valid specifiers
set end of specifierList to "relative"
log "relative specifier added"
on error msg number num partial result p from f to t
tell me to display dialog "[Relative]: Error #" & num & ": " & msg &
" p: " & p & " t: " & t
end try
-- Get attribute distributed over multiple references
--
-- Try to get attribute of each element in list
try
font of every character of text of document 1
-- If there was no error, add "attribute from list" to list of valid
specifiers
set end of specifierList to "attribute from list"
log "attribute from list specifier added"
on error msg number num partial result p from f to t
tell me to display dialog "[Attribute from list]: Error #" & num &
": " & msg & " p: " & p & " t: " & t
end try
(* Try to get element with property-based specifiers *)
-- See if we have a valid element to use for by property-based
specifiers
try
anElement
on error msg
log "Skipping property-based specifiers. Could not get a valid
element using index"
set anElement to null
end try
-- If we got a valid specifier by index, use it to get elements using
property-based specifiers
if anElement is not null then
-- Try to get elements using boolean test of property
try
first character of text of document 1 whose font = (font of
anElement)
-- If there was no error, add "boolean" to list of valid specifiers
set end of specifierList to "boolean"
log "boolean specifier added"
on error msg number num partial result p from f to t
tell me to display dialog "[Boolean]: Error #" & num & ": " & msg &
" p: " & p & " t: " & t
end try
(***TODO
-- Test dictionary for defect: property and class with same name
and tell user must use its in boolean test
*)
(* Multiple reference operations *)
-- Try to get attribute of each element from a boolean test
-- TODO: may want to move this out of property-based specifiers and
use random value for attribute test
try
font of every character of text of document 1 whose font is (font
of anElement)
-- If there was no error, add "attribute from boolean" to list of
valid specifiers
set end of specifierList to "attribute from boolean"
log "attribute from boolean specifier added"
on error msg number num partial result p from f to t
tell me to display dialog "[Attribute from list]: Error #" & num &
": " & msg & " p: " & p & " t: " & t
end try
-- Try to distribute command over each element in a list
try
exists every character of text of document 1
-- If there was no error, add specifier to list of valid specifiers
set end of specifierList to "command over list"
log "aommand over list specifier added"
on error msg number num partial result p from f to t
tell me to display dialog "[Command over list]: Error #" & num & ":
" & msg & " p: " & p & " t: " & t
end try
(* Try to get element by name, using name property and another
element specifier *)
try
-- Use name property to get the name of an element
set elementName to name of anElement
-- Try to get the element by name
character elementName of text of document 1
-- If there was no error, add "Name" to list of valid specifiers
set end of specifierList to "name"
log "Name specifier added"
on error msg number num partial result p from f to t
-- elements can not be specified by name
if num is -1728 then
log "name specifier not added (error -1728: could not get name of
element)"
else
-- There was another error, so index may not be valid
tell me to display dialog "[name]: Error #" & num & ": " & msg & "
p: " & p & " t: " & t
end if
end try
(* Try to get element by id, using id property and another element
specifier *)
try
-- Use id property to get the id of an element
set elementID to id of anElement
-- Try to get the element by id
character id elementID of text of document 1
-- If there was no error, add "ID" to list of valid specifiers
set end of specifierList to "ID"
log "ID specifier added"
on error msg number num partial result p from f to t
-- If we could not get the id property, log and continue
if num is -1728 then
log "ID specifier NOT added (error -1728: could not get id
property)"
else
-- If we got another error, display it
tell me to display dialog "Error #" & num & ": " & msg & " p: " &
p & " t: " & t
--error msg number num partial result p from f to t
end if
end try
end if
return specifierList
end tell
_______________________________________________
Do not post admin requests to the list. They will be ignored.
AppleScript-Users mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
Archives: http://lists.apple.com/archives/applescript-users
This email sent to email@hidden