Without the rest of your script, it’s hard to know if this will work for you, but I have done something similar in the past by defining a variable as a property with an initial empty value so I can check to see if it has been assigned a value later. I’m not aware if the “exists” command can be used in the way you are trying to use it.
property some_var : ""
property some_other_var : ""
if some_var is not "" then DO SOMETHING
if some_other_var is not "" then DO SOMETHING
Model: G5 Tower (not Intel) - Script Editor ver 2.1.1
Browser: Safari 419.3
Operating System: Mac OS X (10.4)
AppleScript doesn’t have “unset” variables; Trying to get the value of a nonexistent error will throw the error you mentioned.
I think your on the right track. I think you just need to use separate try blocks.
try
-- you could also say `get some_var` or even just `some_var`, but I think this makes the most sense when you read it
some_var exists
--DO SOMETHING
on error errMsg
display dialog errMsg
end try
try
some_other_var exists
--DO SOMETHING
on error errMsg
display dialog errMsg
end try
Would you tell us why you would have an undefined variable?
Edit:
FWIW, it does seem to work. (Though I doubt that it’s actual testing the existence of a variable; it’s probably just getting some kind of value and retuning true.)
set test to "abc"
test exists
--> true
I was going to suggest something like that. You may not want to use properties though, as that value will persist across multiple executions (until the script is recompiled). Unless you want persistence, you could just set some variables to an empty string.
set someVar to ""
set someOtherVar to ""
-- whatever else
the reason i was doing it was
i have a varialbe thePerson which is an address book person
then i set variables (homephone, workphone) with values from thePerson
set homePhone to value of first phone whose label is "home"
but if person does not have a home phone # that var does not get set
then later i wanna populate a text field with that homePhone # if it exists
i sppose il have to use the set homephone to “” for now
Good point about properties. I was referencing a print script I wrote, where the code that I copied was doing just what Bruce mentions - checks to see if the values you ran last time are still saved in the property. The first time you run the script, it has to check to see if there are saved values or not. Here is that part of my print script in case anyone might find some of it useful for setting up a script that saves the last settings as the “default” settings next time you run:
property RegMarks : ""
property IncludeTag : ""
property PDFResolution : ""
tell application "Finder"
activate
if RegMarks = "" or IncludeTag = "" or PDFResolution = "" then
set RegMarks to the button returned of (display dialog "Registration marks?" buttons {"Yes", "No", "Cancel"} default button "Yes") as text
set IncludeTag to the button returned of (display dialog "Include job slug/tag?" buttons {"Yes", "No", "Cancel"} default button "Yes") as text
set PDFResolution to the button returned of (display dialog "High Res or Low Res PDF?" buttons {"High Res", "Low Res", "BOTH"} default button "Low Res")
else
try
set ChangeInputQuery to the button returned of (display dialog ("Registration Marks: " & RegMarks & return & "Including job tag: " & IncludeTag & return & "PDF Resolution: " & PDFResolution & return & return & "Continue with these settings?") buttons {"OK", "Change", "Cancel"} default button "OK")
on error
return
end try
if ChangeInputQuery = "Change" then
set RegMarks to the button returned of (display dialog "Registration marks?" buttons {"Yes", "No", "Cancel"} default button "Yes") as text
set IncludeTag to the button returned of (display dialog "Include job slug/tag?" buttons {"Yes", "No", "Cancel"} default button "Yes") as text
set PDFResolution to the button returned of (display dialog "High Res or Low Res PDF?" buttons {"High Res", "Low Res", "BOTH"} default button "Low Res")
end if
end if
end tell
Model: G5 Tower (not Intel) - Script Editor ver 2.1.1
Browser: Safari 419.3
Operating System: Mac OS X (10.4)
Here is an extract ( also slightly edited ) from one of my scripts to show you how to get any phone number that the person has.
The key is just to use the ‘label of phone’ and ‘value of phone’ of the person and the count them.
Hope it makes sense.
property the_numbers : {}
tell application "Address Book"
set theentry to selection -- in this example just select one person (entry)
set this_item2 to item 1 of theentry
--Phone Numbers
set the_phone_label to label of phone of this_item2 -- this gets ALL phone label names as a list
set the_phone to value of phone of this_item2 -- this gets ALL phone values as a list
set the_count to 0
repeat with i from 1 to number of items in the_phone -- count the number of items in the list the_phone
set the_count to the_count + 1 -- sets up a number counter
set this_item to item i of the_phone -- gets the next tel number in list
set this_item to item the_count of the_phone_label & " Tel : " & this_item & return --this matches the list item of the_phone_label with its value from the list the_phone
-- --set item the_count of the_phone to this_item as string
copy this_item to end of the_numbers -- makes a new list with all the numbers and labels for that person matched
end repeat
end tell
the_numbers
set x to ""
tell application "Address Book"
-- set thePerson to some person in the address book
set thePerson to some person
tell thePerson
display dialog x
end tell
end tell
i get an error because it says that x is not defined
and that makes sense, x is not defined for thePerson
how can i access the ‘x’ that is outside of the tell blocks?
Actually, the extract you posted here on its own does in fact work, and does not give an error.
It may be worth posting more of you script to give a better idea of you your real end goal.
Again an example to get all the numbers the have. But this time put any that contain home, work, into the own variable list
set the_numbers to {}
set home_numbers to {}
set work_numbers to {}
set other_numbers to {}
tell application "Address Book"
--set this_item2 to some person -- in this example just select one person (entry)
set theentry to selection -- in this example just select one person (entry)
set this_item2 to item 1 of theentry -- comment this line out if you use the 'some person' line above instead of the selection.
--Phone Numbers
set the_phone_label to label of phone of this_item2 -- this gets ALL phone label names as a list
set the_phone to value of phone of this_item2 -- this gets ALL phone values as a list
set the_count to 0
repeat with i from 1 to number of items in the_phone -- count the number of items in the list the_phone
set the_count to the_count + 1 -- sets up a number counter
set this_item to item i of the_phone -- gets the next tel number in list
set this_item to item the_count of the_phone_label & " Tel : " & this_item & return --this matches the list item of the_phone_label with its value from the list the_phone
copy this_item to end of the_numbers -- makes a new list with all the numbers and labels for that person matched
end repeat
end tell
repeat with i from 1 to number of items in the_numbers
set this_item to item i of the_numbers
if this_item contains "home" then
copy this_item to end of home_numbers
else if this_item contains "work" then
copy this_item to end of work_numbers
else if this_item is not equal to "" then
copy this_item to end of other_numbers
end if
end repeat
home_numbers
Going back to exists briefly, it’s an application-only command, according to the AppleScript Language Guide. That means you can test the existence of items belonging to applications that support it, but not to AppleScript language things like variables. The trick in this case would be to set the variable to one thing if the item exists and to something else if it doesn’t.
tell application "Address Book"
set thePerson to some person
tell (thePerson's first phone whose label is "home"
if (it exists) then
set homePhone to its value
else
set homePhone to missing value -- or "" or whatever.
end if
end tell
end tell
Another approach, shorter and possibly more useful, would be to set the variable to the value of every phone of the relevant label that the person might have. (This is similar to Mark’s idea.) The result in this case is a list containing either phone numbers or nothing.
tell application "Address Book"
set thePerson to some person
set homeNumbers to value of thePerson's phones whose label is "home"
set workNumbers to value of thePerson's phones whose label is "work"
end tell
This way, the variable definitely exists and is definitely a list, which is easier to deal with than a variable that may or may not exist. And if the person has more than one number under that heading, you have them all together.