• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Checking for an undefined variable *without* throwing an error
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

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

  • Prev by Date: Re: Palm Desktop
  • Next by Date: Re: Scripting mail at add an Atachment
  • Previous by thread: automated response
  • Next by thread: Re: Scripting mail at add an Atachment
  • Index(es):
    • Date
    • Thread