Hey All,
In the spirit of giving back (a tiny little bit) to the forum that’s offered so much help, here’s a script that can be put into a service set to take phone numbers from any application and put those to an existing contact (making a new one is too troublesome with my current skill set. The reason for this script is that the built-in contact info recognition (in osx 10.8) is very selective, in that sometimes it puts its magic box around info for you to effortlessly add the into to a contact or create a new one, but SOMETIMES IT DOESN’T, EVEN THOUGH THE PHONE NUMBER IS PLAIN AS DAY WITH PROPER FORMATTING AND EVERYTHING!!!
But I digress,
I’m not gonna lie; part of the motivation for this post is to collect criticism in regards to how the script functions and my way of going about this task in general.
Enjoy!!
-Tim
on run {input, parameters}
--set input to "(559) 553-3586"
--See if contacts should be closed after service is complete
if appIsRunning("Contacts") then
set closeAddressBook to false
else
set closeAddressBook to true
end if
--whole process is in a loop to allow for user to research for contact
repeat
--need to find contact in order to update their info
try
display dialog "Enter name or company of contact to add phone number to:" default answer ""
set thePerson to text returned of result
on error
tell application "Contacts"
if closeAddressBook is true then quit
end tell
error -128
end try
if thePerson is not "" then --check that search was not blank
set parsed to parseLine(thePerson, space) --if user enters more than one word, only the first word is considered
if (count of parsed) is greater than 1 then set thePerson to item 1 of parsed
set nameList to {}
set idList to {}
--populate list of results based on user's search
tell application "Contacts"
set thePeople to every person whose first name contains thePerson or middle name contains thePerson or last name contains thePerson or organization contains thePerson
if (count of thePeople) is 0 then
try
display dialog "No contacts found! Search again."
on error
if closeAddressBook is true then quit
error -128
end try
else
--creating list of names of results for user to select from
repeat with x from 1 to count of thePeople
if the first name of item x of thePeople = missing value then
set tempFirst to ""
else
set tempFirst to the first name of item x of thePeople
end if
if the last name of item x of thePeople = missing value then
set tempLast to ""
else
set tempLast to the last name of item x of thePeople
end if
if the organization of item x of thePeople = missing value then
set tempOrg to ""
else
set tempOrg to the organization of item x of thePeople
end if
set tempString to tempFirst & " " & tempLast & " @ " & tempOrg
set end of nameList to tempString
set end of idList to the id of item x of thePeople
end repeat
--let user select contact to add number to
activate
set thePerson to choose from list nameList with prompt "Choose contact to add phone number to (choose cancel to search again)"
if thePerson is not false then exit repeat
end if
end tell
end if
end repeat
--loop back through result list to find contact that user selected
repeat with x from 1 to count of nameList
if thePerson as string = (item x of nameList as string) then
set theID to item x of idList
exit repeat
end if
end repeat
--populate list of phone number types for user to select
set typeList to {"mobile", "iPhone", "home", "work", "main", "home fax", "work fax", "other", "Custom."}
set theType to choose from list typeList with prompt "Select phone number type:"
if theType is false then error -128
if theType is {"Custom."} then
repeat
try
display dialog "Enter phone number type:" default answer ""
set theType to text returned of result
set theType to {theType}
on error
tell application "Contacts"
if closeAddressBook is true then quit
end tell
error -128
end try
if theType is not "" then exit repeat
end repeat
else
end if
--finally add the phone number of the selected or entered type to the contact
tell application "Contacts"
set thePerson to item 1 of (every person whose id is theID)
make new phone at end of phones of thePerson with properties {label:theType, value:input}
save
if closeAddressBook is true then quit
end tell
return input
end run
--handler to check if an app is running
on appIsRunning(appName)
tell application "System Events" to (name of processes) contains appName
end appIsRunning
--handler to parse lines of text
on parseLine(theLine, delimiter)
set astid to AppleScript's text item delimiters
set AppleScript's text item delimiters to {delimiter}
set theTextItems to theLine's text items
set AppleScript's text item delimiters to astid
repeat with i from 1 to (count theTextItems)
if (item i of theTextItems is "") then set item i of theTextItems to missing value
end repeat
return theTextItems's every text
end parseLine
Model: MBP-r
AppleScript: 2.2.4
Browser: Safari 536.25
Operating System: Mac OS X (10.8)
Model: MBP-r
AppleScript: 2.2.4
Browser: Safari 536.25
Operating System: Mac OS X (10.8)