Address book Applescript Delete

Discussion in 'Mac Programming' started by ViViDboarder, Mar 25, 2010.

  1. ViViDboarder macrumors 68040

    ViViDboarder

    Joined:
    Jun 25, 2008
    Location:
    USA
    #1
    So I'm having some weird issues with deleting items from my address book using Applescripts...

    I run a delete script and nothing seems to happen, so I did a little test. First I ran:
    Code:
    tell application "Address Book"
    	count (emails of people whose label is "mobile")
    end tell
    And it tells me '221'

    Then I run:
    Code:
    tell application "Address Book"
    	delete (emails of people whose label is "mobile")
    end tell
    And it gives no errors.

    Finally I run the first script over again and it gives me 0.

    I'd think that all would be well, but I open address book and it seems like the items are still there. In fact, I know they are still there. I can close Address Book and then open it again and run the count script and it gives me 221 again!

    Any idea how to get these changes to stick?
     
  2. mysterytramp macrumors 65816

    mysterytramp

    Joined:
    Jul 17, 2008
    Location:
    Maryland
    #2
    You say no errors, but are you expecting AS to burp if there's a mistake?

    You might want to wrap your delete line in a try block just to be sure.

    Code:
    tell application "Address Book"
    	try
    		delete (emails of people whose label is "mobile")
    	on error errMsg number errNum
    		display dialog "Error No. " errNum & return & errMsg
    	end try
    end tell
    mt
     
  3. ViViDboarder thread starter macrumors 68040

    ViViDboarder

    Joined:
    Jun 25, 2008
    Location:
    USA
    #3
    Yea, nothing comes up. I'm almost certain it thinks it's doing something. I played around and had it show all labels for the selected person. It showed:
    home
    mobile
    mobile

    Then I ran my delete and ran the show script again and it shows:
    home

    And that's it. But when I look at the entry all the items are there and if I restart Address Book they are still there and the Applescript will show that again.
     
  4. mysterytramp macrumors 65816

    mysterytramp

    Joined:
    Jul 17, 2008
    Location:
    Maryland
    #4
    Try this:

    Code:
    tell application "Address Book"
    	try
    		delete (emails of people whose label is "mobile")
    	on error errMsg number errNum
    		display dialog "Error No. " errNum & return & errMsg
    	end try
    	[B]save[/B]
    end tell
    mt
     
  5. ViViDboarder thread starter macrumors 68040

    ViViDboarder

    Joined:
    Jun 25, 2008
    Location:
    USA
    #5
    It says missing value. I'll try a slightly different script to save thePerson later.

    Worked.

    I used the script attached and added the line for the save this_person.

    Thanks! I had looked all over and all the scripts I found were doing exactly what I had before. I wonder if 10.6 behaves differently and these scripts are for 10.5. Oh well.

    I edited these lines and added the save:
    Code:
    repeat with email_id in emails_to_remove
    	delete (emails of this_person whose id is email_id)
    	set emails_removed to emails_removed + 1
    end repeat
    save this_person
     

    Attached Files:

Share This Page