Deleting Address Book entries

I’m trying to write a script that deletes address book entries. I go through some code that eventually gets the ‘id’ of the person to be deleted. However, when I execute the delete I get an NSReceiverEvaluationScriptError: 4. Here is my code segment:


   .
   .
   .
set delete_person to target_person's id
delete person id delete_person
save addressbook
   .
   .
   .

What does the error mean? TIA.

Hi,

This works for me (as long as there is someone in Address Book with the name ‘test person’):

tell application "Address Book"
	set myPerson to item 1 of (people whose name contains "test person")
	delete myPerson
	save addressbook
end tell

The description for NSReceiverEvaluationScriptError: 4 is “the object doesn’t support the command that was sent to it”. This is quite a general error in Applescript and in this case probably means that your ‘person id delete_person’ isn’t a good reference to a record in Address Book.

Depending on what your ‘target_person’ contains, try changing your lines to:

delete target_person
save addressbook

Best wishes

John M

Hi John M,

You seem to be my mentor lately. :slight_smile:

Your suggestion either didn’t work or I did it wrong. To simpify, let’s assume I want to delete all entries. It tried this code in a test address book:


tell application "Address Book"
   repeat with this_person in every person
      delete this_person
      save addressbook
   end repeat
end tell

This code does nothing (no deletes) but it does not get an error. Using my original code:


tell application "Address Book"
   repeat with this_person in every person
      set lid to this_person's id
      delete person id lid
      save addressbook
   end repeat
end tell

This gives me the NSReceiver error.

Hi,

Yes, sorry :smiley:

Try this (after you have backed up your Address Book database - as this will delete every record):

tell application "Address Book"
	repeat with this_person in (get people)
		delete this_person
	end repeat
	save addressbook
end tell

I don’t really understand why you need to add get sometimes.

The script above is equivalent to:
(Back up your Address Book database)

tell application "Address Book"
	set myPeople to every person
	repeat with this_person in myPeople
		delete this_person
	save addressbook
	end repeat
end tell

So perhaps you need to get a list as reference to the records you want deleted and looping through the list? (if that makes sense)

Hope this helps.

John M

That fixed it. I don’t understand it but it works so I’m happy. Thanks. :slight_smile: