AppleScript to Create Restaurant Group

I’m trying to write an applescript to automatically create an addressbook group for entries containing the keyword “Restaurant”.

My script will currently find entries where any field is exactly “Restaurant”, but not “Italian Restaurant”. I’d appreciate any help.


– AppleScript to Create Restaurant Group

tell application “Address Book”

if not (group “Restaurants” exists) then
make new group with properties {name:“Restaurants”}
end if
add (every person whose properties contains “Restaurant”) to group “Restaurants”
save addressbook

end tell

Is there a reason that a Smart Group will not work for you? You can simply select card contains Restaurant, and all the contacts are there instantly.

Yes, the reason is that the iPhone does not support synching smart lists. I’m planning on running an applescript nightly to update a “dumb” group.

I’ve got the following script working, but was thinking of using a smart group and scripting a copy of the contacts from the smart group to the dumb group.

Thoughts?


— current working script

tell application “Address Book”
if not (group “Restaurants” exists) then
make new group with properties {name:“Restaurants”}
end if
repeat with this_person in every person
repeat with this_property in ((get properties of this_person) as list)
try
if (this_property as string) contains “Restaurant” then
add this_person to group “Restaurants”
save addressbook
exit repeat
end if
end try
end repeat
end repeat
end tell


— improved idea (syntax is wrong here, but you get this picture)

tell application “Address Book”
select group “Smart Restaurant”
select all
copy
paste into group “Restaurants”
save addressbook
end tell


—or

tell application “Address Book”
add (every person whose group is “Smart Restaurant”) to group “Restaurants”
save addressbook
end tell

I think you have an excellent idea. I am amazed that the iPhone cannot read a smart Group. Does it have any difficulty with smart Playlists in iTunes? I can’t live without my smart stuff, regardless of the application.

Anyway, this script works for me:

tell application "Address Book"
	set smart_Rest to id of every person in group "Restaurant" --This is the name of the Smart Group
	set hard_Rest to id of every person in group "Restaurants" --This is the name of the 'hard' Group
	repeat with sr in smart_Rest
		if hard_Rest does not contain (contents of sr) then add (every person whose id = sr) to group "Restaurants"
	end repeat
	save addressbook
end tell

This is much faster! Do you know if the smart group can be created from AppleScript?

Here’s my version:

tell application “Address Book”
if not (group “Restaurants” exists) then
make new group with properties {name:“Restaurants”} --Create our ‘hard’ Group if missing
end if
save addressbook
set smart_Rest to id of every person in group “Smart Restaurants” --This is the name of the Smart Group
set hard_Rest to id of every person in group “Restaurants” --This is the name of the ‘hard’ Group
repeat with sr in smart_Rest
if hard_Rest does not contain (contents of sr) then add (every person whose id = sr) to group “Restaurants”
end repeat
save addressbook
end tell

Creating smart anything in iLife applications is the pipe dream of all AppleScripters. I doubt we will ever see it. Glad the script works well for you, but you do not need the first save addressbook that you have. That command is nothing more than a type of visible refresh. Just because you cannot immediately see the creation of a ‘hard’ group; it is still there and can still be worked with.

Thanks for all your help, Craig.

I posted the final result here:

http://dfbills.com/display/277

Looks good!! Thanks for the mention.