Need help building address book properties

I have a script which builds a properties list for adding a person to my address book. However, I cannot seem to build it in such that the ‘make new person’ does not get an error. I can type in the properties in the ‘make new’ but when I use ‘myproperties’ I cannot get it to work. I’m guessing this is a problem formatting ‘myproperties’ to get applescript to pass it properly to the application.


set myproperties to {}
--<this happens in a bunch of extraction code>
set end of myproperties to "first name:" & "\"" & firstname & "\""
set end of myproperties to "last name:" & "\"" & lastname & "\""
set end of myproperties to "organization:\"My Company, Inc.\""
--<end of bunch of extraction code>
tell application "Address Book"
make new person with properties myproperties
end tell

Can someone help me get this syntax right? TIA.

Hi,

Applescript can’t deal with creating variables during execution in the way you are trying. Also the Address Book variables don’t compile outside an Address Book tell block due to the spaces in ‘fist name’ and ‘last name’. Try this:

tell application "Address Book"
	set myproperties to {first name:"", last name:"", organization:""}
	--<this happens in a bunch of extraction code>
	set first name of myproperties to "test"
	set last name of myproperties to "Name"
	set organization of myproperties to "My Company, Inc."
	--<end of bunch of extraction code>
	make new person with properties myproperties
	save addressbook -- to update the address book
end tell

Best wishes

John M

Thanks for the reply. That certainly moved me closer but I’m not there yet. When I look at the address book after, there are the correct number of entries but all have only the organization filled in. Here is my current code:

tell application "Address Book"
		set staff to {first name:"", last name:"", organization:""}
		repeat with j from 1 to count of mailentry by 2
			if item j of mailentry is "givenName" then
				set first name of staff to item (j + 1) of mailentry
			else if item j of mailentry is "sn" then
				set last name of staff to item (j + 1) of mailentry
			end if
		end repeat
		set organization of staff to "AIM Systems, Inc."
		make new person with properties staff
		save addressbook
	end tell

This is a code segment of a larger repeat that runs this segment once for each entry. It is incomplete at this point but 'givenName and ‘sn’ will occur only once in the ‘mailentry’ list. I did a display on the 3 items in ‘staff’ and everything looks right.

Hi,

I can’t test this at the moment, but try:

tell application "Address Book"
       set staff to {first name:"", last name:"", organization:""}
       repeat with j from 1 to count of mailentry by 2
           if item j of mailentry is "givenName" then
               set first name of staff to contents of (item (j + 1) of mailentry)
           else if item j of mailentry is "sn" then
               set last name of staff to contents of (item (j + 1) of mailentry)
           end if
       end repeat
       set organization of staff to "AIM Systems, Inc."
       make new person with properties staff
       save addressbook
   end tell

It may be that item (j + 1) of mailentry is giving a reference to the point in the array and not it’s contents.

Best wishes

John M

Thanks again for the reply. This stuff is over my head. Damn the learning curve on this is steep. :confused: Sorry to report that your suggestion didn’t help. Same results.

After extensive fumbling around (I can’t call it trying different things because I am completely lost figuring out this list and properties structure stuff). However, I have concluded that everything works as advertised when I manually type the properties in the code. It fails whenever I use the built properties list/string or whatever it is called. Clearly there is a problem with the way I am building it and I deperately need help getting this right. How do I build these silly properties list so it does then same thing as when I type it in manually? TIA.

Well, even a blind squirrel finds an occasional nut. One thing I tried while fumbling around was add ‘as string’ the end of the ‘set’ statements. Viola, it now works. I have no clue as to why but I won’t argue with success.