Applescript and Address Book not working

Discussion in 'Mac Programming' started by maturnip, Jan 20, 2010.

  1. maturnip macrumors newbie

    Joined:
    Sep 22, 2008
    #1
    I have found multiple AppleScripts for address book that deal with my problem (the first and last name fields are reversed) every time i run the script however it does not correct the problem.

    I run the script from the apple menu and it opens Address Book but it does not correct the issue. What am I doing wrong? Thank you.
     
  2. chown33 macrumors 604

    Joined:
    Aug 9, 2009
  3. maturnip thread starter macrumors newbie

    Joined:
    Sep 22, 2008
    #3
    Here is the code

    This is one of many i have tried with similar results or lack there of.

    tell application "Address Book"
    set mySelected to selection
    repeat with myPerp in mySelected
    set tmp to last name of myPerp
    set last name in myPerp to (first name in myPerp)
    set first name in myPerp to tmp
    end repeat
    end tell
     
  4. maturnip thread starter macrumors newbie

    Joined:
    Sep 22, 2008
    #4
    This is the other script that I tried.

    tell application "Address Book"
    set mylist to selection
    repeat with this_person in mylist
    set first_name to first name of this_person
    set last_name to last name of this_person
    set first name of this_person to last_name
    set last name of this_person to first_name
    end repeat
    end tell
     
  5. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #5
    This script works for me. If I run it twice on the same selection, it reverses the first run.

    Please explain what's not working when you run it. Be specific.

    Which OS version are you using?
     
  6. maturnip thread starter macrumors newbie

    Joined:
    Sep 22, 2008
    #6
    I'm running 10.6.2

    I double click the script and it opens Address Book and it seems to stop there. If I try to re-run it, it makes the open Address Book window kind of blink like it de-selects it then selects it again. Nothing changes in the app though i.e. the names don't change.
     
  7. robbieduncan Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #7
    Note the bit of the script "set mylist to selection"? You need to select the records in Address Book you want the script to run on before you run the script.
     
  8. maturnip thread starter macrumors newbie

    Joined:
    Sep 22, 2008
    #8
    I have been selecting the records and it is still not working. I am running the script correctly right?
     
  9. maturnip thread starter macrumors newbie

    Joined:
    Sep 22, 2008
    #9
    Ok so when I run the app from the apple menu pull down menu what I listed previously happens. It loads Address Book up but no changes happen. When I select a contact and run it from the pull down nothing happens other than it looks like it is de-select the window then re-selecting it. When I run the script from the AppleScript Editor same thing. In the events window below I get this:

    tell application "Address Book"
    get selection
    --> {person id "B2DFFBA4-9815-4689-8273-16AFDD2ED368:ABPerson"}
    get first name of person id "B2DFFBA4-9815-4689-8273-16AFDD2ED368:ABPerson"
    --> "Michael"
    get last name of person id "B2DFFBA4-9815-4689-8273-16AFDD2ED368:ABPerson"
    --> "2222"
    set first name of person id "B2DFFBA4-9815-4689-8273-16AFDD2ED368:ABPerson" to "2222"
    set last name of person id "B2DFFBA4-9815-4689-8273-16AFDD2ED368:ABPerson" to "Michael"
    end tell

    I get the feeling I am the problem in this situation. Is there another way to run this? When I run scripts in iTunes, I do it from a script pull down menu in iTunes. Thanks again.
     
  10. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #10
    I tried the script from my earlier post on 10.6.2. It does not behave correctly: the changes don't get saved.

    I googled the following terms:
    snow leopard address book problems
    snow leopard address book scripting

    In the first page of results for the 2nd search terms, I found this:
    http://discussions.apple.com/thread.jspa?messageID=10673588

    Halfway down that page, someone describes almost the exact same problem (changes not saved), but he's adding new entries.

    The fix is to add the save command to the script, right before end tell. This causes the changes to be saved, and Address Book's window also shows the changes properly:

    Code:
    tell application "Address Book"
    	set mylist to selection
    	repeat with this_person in mylist
    		set first_name to first name of this_person
    		set last_name to last name of this_person
    		set first name of this_person to last_name
    		set last name of this_person to first_name
    	end repeat
    	[B]save[/B]
    end tell
    
     
  11. maturnip thread starter macrumors newbie

    Joined:
    Sep 22, 2008
    #11
    That did it! Thank you so much you're amazing! I am a total noob when it comes to this stuff. Thanks again everyone!
     

Share This Page