Address book - delete all contacts within a group

Beginner/Int user: can’t seem to delete the contacts that are within a group, or if I could delete the group and all contacts within, that would achieve the goal as well.

Anyone with knowledge?

Many thanx,

Troy

Model: MAcPro
AppleScript: 2.1.2
Browser: Safari 533.20.27
Operating System: Mac OS X (10.6)

Assuming you’d like to delete a group by name:


tell application "Address Book"
	delete (groups whose name = "SomeName") -- you supply the name
	save addressbook -- saving is necessary to see the result of the deletion.
end tell

thank you for your help, I am very appreciative,

the script you suggested only deletes the group, not the contacts in the group which is the goal.

I would like to either:

1 - delete the ‘parent’ group and all the contacts in that group, or

2 - delete all contacts within a named group

many thanx,

Troy

It’s not clear if you simply want to remove the contacts from the group or delete them from Address Book altogether.

In Address Book 5.0.3, there’s a ‘remove’ command which can be used in a repeat loop to remove a person from a group or a group from a person. But the first ‘delete’ command used below seems to have the same effect. It worries me a bit as it looks as if it should have the same effect as the second ‘delete command’, so try it yourself first with a few test contacts and a test group!


tell application "Address Book"
	set theGroup to group "Test" -- Indentify this yourself somehow.
	
	-- **********
	-- Use only one of the following.
	-- Either remove all contacts from the group .
	delete people of theGroup
	
	-- . or delete the group's contacts from Address Book altogether.
	delete (people whose groups contains theGroup)
	-- *********
	
	delete theGroup
	save
end tell

awesome, I used the second command, works great, thank you. – ya hoo!

I’m exporting a .csv from Filemaker and then importing that into address book.
Using Quickeys and applescript I’ve been able to make it work.
I’m trying to eliminate the quickeys elements for speed and stability.

I found an applescript command that will let me open a vCard but it doesn’t like the .csv.

set thefile to choose file
tell application “Finder” to set file_Name to name of thefile
–set Entry_Name to text 1 thru -5 of file_Name → this line strips off the “.vcf” extension

tell application “Address Book”
activate
open thefile
end tell

Is there a way to applescript an import command so that I can ‘automatically’ import my .csv?

Many thanx again,

Troy-

actually while trying to implement this script in my solution it seems to not work,

  • back to the basic goal of deleting contacts within a group-
    I used the following script and it doesn’t clear out the group-- (I don’t need to delete the group, only delete the people within) neither script does the trick… help?? ( very appreciative)

tell application “Address Book”
set theGroup to group “MDA-C”
delete (people whose groups contains theGroup)
–delete theGroup
save
delay 5
end tell

tell application “Address Book”
set theGroup to group “MDA-E”
delete (people whose groups contains theGroup)
–delete theGroup
save
delay 5
end tell

Hi, Troy.

The script worked for me until I made some of my test people members of other groups as well as the one to be cleared. In that case, the script ignored many of the people it was supposed to delete and deleted other people from my Address Book instead. That’s a serious bug in Address Book’s scripting implementation and I hope you haven’t lost any data yourself. (I’m fortunate in that my Address Book contacts are duplicated on another machine.)

It appears to be safer to delete the contacts individually :

tell application "Address Book"
	set theGroup to group "MDA-C"
	set peopleToDelete to people of theGroup
	repeat with thisPerson in peopleToDelete
		delete thisPerson
	end repeat
	save
end tell

The references ‘people of the group’ and ‘(people whose groups contains theGroup)’ are different as direct parameters of ‘delete’, but the lists they return when simply ‘got’ contain exactly the same people (although not necessarily in the same order). I’ve therefore used the simpler reference here.

:smiley: Nigel, thank you for your time, really,…
I found the same thing, I was out on the road after syncing my phone and didn’t have my wife’s number!!! I really had to think for a minute to come up with it…:slight_smile:
I realized what had happened and tested when I got back home and found as you did.
(I had a backup handy)

I’ll try this new script later when I get back. Thank you again…

Troy

I just ran the script three times and every time it deleted everyone in the group except 1 person, it was never the same person. any idea?
Troy-

Well I just ran it three more times and it worked perfectly… I’ll keep testing it later, sorry for the false alarm!!

Enjoy your Saturday!

Cheers,

Troy-