Checking for an undefined variable *without* throwing an error
Checking for an undefined variable *without* throwing an error
- Subject: Checking for an undefined variable *without* throwing an error
- From: "Scott Babcock" <email@hidden>
- Date: Wed, 5 Jan 2005 19:25:49 -0800
- Thread-topic: Checking for an undefined variable *without* throwing an error
I've been doing some scripting with System Events, and I've noticed that
there are many instances where the value of a UI element attribute
returns 'undefined'. If you assign this value to a local variable and
refer to the variable, you get the "The variable <var-name> is not
defined" error. The traditional method of detecting an undefined
variable is to wrap the reference in a 'try' block, but this feels a bit
clunky. I was playing around with a few ideas and came up with this:
if (script property is (class of (a reference to myVar))) then set
myVar to missing value
By going through a reference, I avoid the error and get a testable
result. Can anyone see any obvious issues with this solution?
As an example, run the following:
property undefined : script property
set valueText to ""
tell application "TextEdit" to activate
tell application "System Events"
set objectRef to a reference to application process "TextEdit"
repeat with attrib in (attributes of objectRef)
set objectVal to value of attrib
if (undefined is (class of (a reference to objectVal))) then
set valueText to valueText & (name of attrib) & " =
{undefined}" & return
else
set valueText to valueText & (name of attrib) & " = " &
(objectVal as text) & return
end if
end repeat
end tell
tell application "TextEdit" to display dialog valueText
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Applescript-users mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden