validating a not defined variable

Hello all,

Working on 10.4.10 and I'm having trouble b/c I need to validate a returned person from Address Book.  The issue is that when I put in the name of a person who is not in my address book, the variable this_person is not set.  I'm trying to determine how to find out code wise that it is not set.  I could probably put a try block around the first time I try to use it, but I was hoping for something more graceful.  (like the if line below..... if it worked)

 Script works well for valid names but for non existent people, I get AppleScript:"The variable this_person is not defined." on the if line below.  I've been googling and searching the forum without luck, and I suspect that I may just be using the wrong language.

Needless to say, the 'oh crud' response is not being triggered.

thanks for your help!


on run argv
	using terms from application "Address Book"
		
		if (count of argv) is 0 then set argv to {"persona", "fake", "event test 1", "work"}
		tell application "Address Book"
			
			try
				set this_person to first person whose first name = item 1 of argv and last name = item 2 of argv
			on error
				return "oh crud"
			end try
--AppleScript:The variable this_person is not defined in next line
			if (this_person is not null) then return "crud"
		end tell
	end using terms from
end run

Welcome. :slight_smile:

It’s a problem with Address Book not returning any value.

Try something like this:

on run argv
	if (count of argv) is 0 then set argv to {"persona", "fake", "event test 1", "work"}
	tell application "Address Book"
		try
			set this_person to first person whose first name = item 1 of argv and last name = item 2 of argv
			-- If Address Book didn't return a value, then this variable is undefined.
			-- Thus, trying to access it will cause an error.
			this_person
		on error
			return "oh crud"
		end try
	end tell
end run

Hi,

just add one line, if this_person is not defined (no match), getting the value causes the error

tell application "Address Book"
	try
		set this_person to first person whose first name = item 1 of argv and last name = item 2 of argv
		get this_person
	on error
		return "oh crud"
	end try
end tell

Edit: You got the point, Bruce :wink:

Thanks for both the welcome and the quick responses!

I’ll probably just run with what you’ve handed me, but it does beg a question: Can you determine whether Schrödinger’s Cat has been poisoned… errr … sorry… Can you determine whether the address book returned a value until you actually observer the variable where the return value is to be stored.

It almost seems like if the call to address book cannot identify a hit from the data supplied, that the “set this_person to first person whose first name = item 1 of argv and last name = item 2 of argv” call never occurs at all and the variable ‘this_person’ is not created.

Still trying to get my head around AppleScripting: apologies for the basic questions.

thanks again!

Absolutly right, look at the “rules” of AppleScript.
In AppleScript you should declare a variable before using it!!

AppleScript is very nice and does it for you if it gets any result of the set line
In your example if there is no match, no value (result) will be returned and the variable will not be defined.

Actually this is the correct way

on run argv
	set this_person to missing value
	if (count of argv) is 0 then set argv to {"persona", "fake", "event test 1", "work"}
	tell application "Address Book"
		set this_person to first person whose first name = item 1 of argv and last name = item 2 of argv
		if this_person is missing value then return "oh crud"
	end tell
end run

An alternative is to test against all names:

-- I do have a test entry named Cadabra Abra (last name first)
set Q to {}
tell application "Address Book" to set folks to name of every person
set tNames to {"Cadabra Abra", "Enstein Frank"}
repeat with N in tNames
	set Q's end to isThere(N, folks)
end repeat
Q --> {"It's here", "crud"}

to isThere(aName, folks)
	if aName is in folks then
		return "It's here"
	else
		return "crud"
	end if
end isThere

First, this is a bug in Address Book. If a reference fails to identify an object, the application should raise an ‘object not found’ error, not fail silently as Address Book does. Submit a bug report on it (http://bugreport.apple.com).

Second, to test for an object’s existence, use the application’s ‘exists’ command:

on run {firstName, lastName}	
	tell application "Address Book"
		return exists (people whose first name = firstName and last name = lastName)
	end tell
end run

I don’t think so. It’s a feature. It happens every time you use a whose filter regardless of which program you use.

The variable theMail will (almost ;)) never be defined.

tell application "Mail"
	set theMail to get 1st message of inbox whose sender is "asdjhgrtzu"
end tell

No match is no result

Thanks for the help everyone! This discussion cleared up a lot and I have a few more tools in my belt and a much better understanding.

sincerely,
asdjhgrtzu :slight_smile:

tell application "Finder"
	set test to first file of desktop whose name is "not here.txt"
	-- > error "Finder got an error: Can't get file 1 of desktop whose name = \"not here.txt\"."
end tell
tell application "iTunes"
	set test to first track of library playlist 1 whose name is "blah blah"
	--> error "iTunes got an error: Can't get track 1 of library playlist 1 whose name = \"blah blah\"."	
end tell

No, it’s a bug. The Cocoa Scripting framework has a lot of those, and they affect all Cocoa applications.

A command should either 1. always return a value, or 2. never return a value. Any command that sometimes returns a value and sometimes doesn’t is buggy and should be reported.

I agree with hhas’ summary - a point he has raised many times in many places. The key words are “Always” and “Never”. Some Cocoa apps are “Sometimes”, which is absolutely useless.

So do I, but unfortunately AppleScript has a hugh amount of these “sometimes”. :wink: