Skip to content

Instantly share code, notes, and snippets.

@dommmel
Last active December 25, 2015 04:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dommmel/6916534 to your computer and use it in GitHub Desktop.
Save dommmel/6916534 to your computer and use it in GitHub Desktop.
Export your Mac addressbook to your SNOM VoIP phone. Source: http://megasnippets.com/en/source-codes/applescript/export_mac_addressbook_snom_voip_phone
-- Addressbook to SNOM export
-- Be sure to open Contacts app before running this script
set resultText to ""
set currentLine to ""
set numPhones to 0
tell application id "com.apple.AddressBook"
set the clipboard to people's vcard
-- Find the maximum number of phone numbers
repeat with x from 1 to the count of people
set thePerson to person x
set countP to count of phone of thePerson
if countP > numPhones then
set numPhones to countP
end if
end repeat
-- Export all contacts
repeat with x from 1 to the count of people
set thePerson to person x
set my currentLine to ""
set my currentLine to my currentLine & "\"" & the name of thePerson & "###phoneLabelHere###\","
set my currentLine to my currentLine & "\"###phoneHere###\""
-- Walk through Phone entries
set numCurrentPhones to the count of phone of thePerson
repeat with thePhone in the phone of thePerson
set thePhoneValue to the value of thePhone
set cleanedPhoneValue to do shell script "echo \"" & thePhoneValue & "\" | sed -e \"s/^\\+/00/\" -e \"s/[^0-9]//g\""
set thePhoneLabel to the label of thePhone
set theLine to my searchReplace(my currentLine, "###phoneHere###", cleanedPhoneValue)
if numCurrentPhones > 1 then
set theLine to my searchReplace(theLine, "###phoneLabelHere###", " (" & thePhoneLabel & ")")
else
set theLine to my searchReplace(theLine, "###phoneLabelHere###", "")
end if
set my resultText to my resultText & theLine & linefeed
end repeat
end repeat
-- Save file
set theFile to choose file name with prompt "Save as:" default name "Unbenannt.csv" default location the path to home folder
set fp to open for access theFile with write permission
write resultText to fp as «class utf8»
close access fp
end tell
on searchReplace(origStr, searchStr, replaceStr)
set old_delim to AppleScript's text item delimiters
set AppleScript's text item delimiters to searchStr
set origStr to text items of origStr
set AppleScript's text item delimiters to replaceStr
set origStr to origStr as string
set AppleScript's text item delimiters to old_delim
return origStr
end searchReplace
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment