I have some weird problem with appleScript and the adress book.
When I run the script multiple time without exiting address book, it looks like the scripts sees the number it already added, at least in the log messages I can see that a new number is there. But it never shows up in the adress book, neither after the first run, nor after the subsequent runs. And if I close and reopen my address book, it is completely gone (meaning even the script doesn’t see the new number anymore).
Any idea why the “make new phone” has not the expected effect here?
display dialog "Warning: Be sure to back up your Address Book database first!" & return & return & "Do you still want to continue?"
set countryCodes to {"1", "7", "20", "27", "30", "31", "32", "33", "34", "36", "39", "40", "41", "43", "44", "45", "46", "47", "48", "49", "51", "52", "53", "54", "55", "56", "57", "58", "60", "61", "62", "63", "64", "65", "66", "81", "82", "84", "86", "90", "91", "92", "93", "94", "95", "98", "212", "213", "216", "218", "220", "221", "222", "223", "224", "225", "226", "227", "228", "229", "230", "231", "232", "233", "234", "235", "236", "237", "238", "239", "240", "241", "242", "243", "244", "245", "246", "247", "248", "249", "250", "251", "252", "253", "254", "255", "256", "257", "258", "260", "261", "262", "263", "264", "265", "266", "267", "268", "269", "290", "291", "297", "298", "299", "350", "351", "352", "353", "354", "355", "356", "357", "358", "359", "370", "371", "372", "373", "374", "375", "376", "377", "378", "380", "381", "382", "385", "386", "387", "389", "420", "421", "423", "500", "501", "502", "503", "504", "505", "506", "507", "508", "509", "590", "591", "592", "593", "594", "595", "596", "597", "598", "599", "670", "673", "674", "675", "676", "677", "678", "679", "680", "681", "682", "683", "685", "686", "687", "688", "689", "690", "691", "692", "850", "852", "853", "855", "856", "870", "880", "886", "960", "961", "962", "963", "964", "965", "966", "967", "968", "971", "972", "973", "974", "975", "976", "977", "992", "993", "994", "995", "996", "998", "6723"}
set numberlist to {}
tell application "Address Book"
-- set peopleCount to (count every person)
set contactlist to selection
repeat with n from 1 to count of contactlist
set currentPerson to item n of contactlist
set phoneProperties to properties of phones of currentPerson
set numberlist to (get value of every phone of currentPerson)
log n
repeat with i from 1 to the count of phoneProperties
set currentItem to item i of phoneProperties
set currentPhone to value of currentItem
set duplicated to false
log "currentPhone: " & currentPhone
if text 1 thru 1 of currentPhone is "+" then
log "phone starts with +"
repeat with otherPhone in numberlist
log "other phone is: " & otherPhone
if (((text -7 thru end of currentPhone) = (text -7 thru end of otherPhone)) and not (text of currentPhone = text of otherPhone)) then
set duplicated to true
exit repeat
end if
end repeat
log "duplicated: " & duplicated
if duplicated is false then
set ccLength to 0
repeat with i from 1 to 3
set parseCC to (text 2 thru (2 + i) of currentPhone)
log "parseCC: " & parseCC
if parseCC is in countryCodes then
set ccLength to i + 1
exit repeat
end if
end repeat
log "ccLength:" & ccLength
if ccLength = 0 then exit repeat
set originalLabel to label of currentItem
set SNRlabel to "SNR - " & originalLabel
set SNRphone to (text 1 thru (1 + ccLength) of currentPhone) & " 00 " & (text (2 + ccLength) thru -1 of currentPhone)
make new phone at end of phones of currentPerson with properties {label:SNRlabel, value:SNRphone}
end if
end if
end repeat
end repeat
end tell