Determine if a record contains one field

Hi guys,

I have a record like {firstName:“John”, lastName:“Smith”} returnerd from another application.
Sometimes this app return one field instead of two.
How I can check if a particular field is in my Record?
Something like:

set myRecord to {lastName:“Smith”}
If firstName is not in myRecord then
display dialog "Field not present
end if

PS: without a try on error…


You can check that a record has the requisite number of properties simply by counting it

if ((count myRecord) < 2) then
	-- Fewer than two properties.
end if

Another approach is to concatenate a full record containing dummy or default values. You can then test the properties of the result to see if they have the dummy values or values likely to be from the record you’re testing:

{firstName:"John", lastName:"Smith"} & {firstName:missing value, lastName:missing value}
--> {firstName:"John", lastName:"Smith"}

-- But:
{lastName:"Smith"} & {firstName:missing value, lastName:missing value}
--> {lastName:"Smith", firstName:missing value}

-- So:
set testRecord to myRecord & {firstName:missing value, lastName:missing value}
if (testRecord's firstName is missing value) then
	-- firstName missing from myRecord
end if
if (testRecord's lastName is missing value) then
	-- lastName missing from myRecord
end if

We can use cocoa to solve this easily…

set theRecord to {firstName:"John", lastName:"Smith"}

tell application "Automator Runner"
	set keysList to call method "allKeys" of theRecord
end tell

if "firstName" is in keysList then
	return "firstName is in theRecord"
	return "firstName is not in theRecord"
end if

I’ve been using List 'n Record Tools to get record properties.
Would you say osaxen are now (largely or to some extent) obsolete?
Note to self: if yes, learn ASOC ASAP - or something else?

I use list and record tools too, however there isn’t any native applescript method to get the key names of a record… thus the objective-c method was used.

Not obsolete although I don’t have any scripting additions on my system. The problem with scripting additions is that not all systems have the same ones, so if you’re distributing code then that could be an issue. Plus you’re relying on third parties to keep them updated as new operating systems are released.

If you want to expand the capabilities of applescript, the best way is to learn the “call method” construct of Automator Runner. Then you can access all of the cocoa methods which yields a vast amount of tools not normally available to applescript. I don’t see Automator going away and thus “call method” should be with us for many years to come (although that’s just a guess on my part).

If you want to develop cocoa applications then learn cocoa/objective-c. And if you want to run applescript code in your cocoa application, there’s several tools already available (namely NSApplescript and the scripting bridge). As such I don’t see ASOC as a useful language. It’s just something else to learn and doesn’t give you any real advantages over the other two applescript solutions from cocoa.

Tusind tak, Hank

The second approach of Nigel is the winner. Clean and cleaver solution without invoke any other app or extra scripting addition.
You are the best.



Just for the hell of it, I’ve just knocked together a vanilla script which does the same as this using the File Read/Write commands. :slight_smile: