Addressbook Applescripting

Discussion in 'Mac Programming' started by ViViDboarder, Jan 12, 2009.

  1. ViViDboarder macrumors 68040

    ViViDboarder

    Joined:
    Jun 25, 2008
    Location:
    USA
    #1
    So, I'm trying to write an applescript that will go through my address book and get everyones mobile numbers. It will then cURL a webpage with the number as part of the string. Then it will pars the webpage for the carrier of the mobile service. From there it will take that carrier and number information and generate the proper sms portal email, and add it to the address book contact. example xxxxxxxxxx@txt.att.net

    So my frustration is that I've never used AppleScript before. At the moment I have:
    Code:
    tell application "Address Book"
    	set mylist to selection
    	repeat with this_person in mylist
    		set phonenumber to {label, value} of phones of this_person
    	end repeat
    end tell
    This gives me {{"mobile"}, {"(###) ###-####"}} instead of just the number string. Also, if there are more than one number, it gives me all of them. I only want ones with the label mobile.

    Any help?
     
  2. ViViDboarder thread starter macrumors 68040

    ViViDboarder

    Joined:
    Jun 25, 2008
    Location:
    USA
    #2
    In my own tinkering I got futher. I have:

    Code:
    set command to "curl \"http://www.whitepages.com/carrier_lookup?carrier=att&name_0=&number_0="
    
    tell application "Address Book"
    	-- makes a list of all selected entries
    	set mylist to selection
    	-- iterates for each person
    	repeat with this_person in mylist
    		-- makes list of each phone number for this_person
    		set phonesList to every phone of this_person
    		-- iterates for each phone number
    		repeat with phonenumber in phonesList
    			-- if the phonenumber is a mobile number extract the number
    			if label of phonenumber is "mobile" then
    				set theNumber to value of phonenumber
    				set theNumber to do shell script "perl -e 'use URI::Escape; print uri_escape(\"" & theNumber & "\")';"
    				set theCommand to command & theNumber & "\""
    				set quotedCommand to quoted form of theCommand
    				set T to (do shell script "" & quotedCommand)
    			end if
    		end repeat
    	end repeat
    end tell
    but the problem is, instead of getting the webpage it throws this error

    Code:
    APPLE SCRIPT ERROR
    Address Book got an error: sh: curl "http://www.whitepages.com/carrier_lookup?carrier=att&name_0=&number_0=(555)%20555-5555": No such file or directory
    But if I take the command and copy and paste it to terminal it works fine. I'm not sure where the error is coming from. Maybe because I'm running my script from bash and AppleScript does it from sh or something?
     
  3. ViViDboarder thread starter macrumors 68040

    ViViDboarder

    Joined:
    Jun 25, 2008
    Location:
    USA
    #3
    One way conversation...

    I thought I was stuck again, but I was right. I just put an extra command in to change over to bash and it worked. I'll refrain from posting now until I'm finished or certain I'm stuck! :p
     
  4. ViViDboarder thread starter macrumors 68040

    ViViDboarder

    Joined:
    Jun 25, 2008
    Location:
    USA
    #4
    Done!

    Code:
    -- RUNS ON SELECTED ENTRIES
    -- this script adds email addresses for selected AddressBook cards
    -- for the SMS gateways associated with the cellphone number provided on the card
    -- This script checks for and does not act on duplicates
    -- May not work 100% if number was brought from another carrier
    
    
    -- beginning of bash script command
    set command to "curl \"http://www.whitepages.com/carrier_lookup?carrier=att&name_0=&number_0="
    -- used for parsing the webpage
    set astid to AppleScript's text item delimiters
    set justBefore to "<td id=\"status_column\" valign=\"middle\">"
    
    tell application "Address Book"
    	-- makes a list of all selected entries
    	set mylist to selection
    	-- iterates for each person
    	repeat with this_person in mylist
    		-- makes list of each phone number for this_person
    		set phonesList to every phone of this_person
    		-- iterates for each phone number
    		repeat with phonenumber in phonesList
    			-- if the phonenumber is a mobile number extract the number
    			if label of phonenumber is "mobile" then
    				-- gets the mobile number
    				set theNumber to value of phonenumber
    				-- makes mobile number URI compatable
    				--set theNumber to do shell script "perl -e 'use URI::Escape; print uri_escape(\"" & theNumber & "\")';"
    				-- removes spaces - and () and . from phone number
    				set illegalCharacters to {" ", "-", "(", ")", "."} -- Whatever you want to eliminate.
    				set replaceWith to ""
    				repeat with thisChar in illegalCharacters
    					set AppleScript's text item delimiters to {thisChar}
    					set theNumber to text items of theNumber
    					set AppleScript's text item delimiters to {replaceWith}
    					set theNumber to theNumber as Unicode text
    				end repeat
    				theNumber
    				-- adds the number and ending " to the command
    				set theCommand to command & theNumber & "\""
    				-- quotes the command to ensure & is executed fine
    				set quotedCommand to quoted form of theCommand
    				-- switches to bash and gets the source of the page as T
    				set T to (do shell script "/bin/bash -c " & quotedCommand)
    				-- begin parsing
    				set AppleScript's text item delimiters to justBefore
    				set lastPart to text item 2 of T
    				set AppleScript's text item delimiters to "<td>"
    				set X to text item 2 of lastPart
    				set AppleScript's text item delimiters to "</td>"
    				set carrier to text item 1 of X
    				-- reset the delimiter
    				set AppleScript's text item delimiters to astid
    				-- creates the email address for the phone number
    				set theEmail to ""
    				if carrier is "Verizon" then
    					set theEmail to theNumber & "@vtext.com"
    				end if
    				if carrier is "AT&T/Cingular" then
    					set theEmail to theNumber & "@txt.att.net"
    				end if
    				if carrier is "T-Mobile" then
    					set theEmail to theNumber & "@tmomail.net"
    				end if
    				if carrier is "Cellular One" then
    					set theEmail to theNumber & "@mobile.celloneusa.com"
    				end if
    				if carrier is "Alltel Wireless" then
    					set theEmail to theNumber & "@message.alltel.com"
    				end if
    				if carrier is "Sprint PCS" then
    					set theEmail to theNumber & "@messaging.sprintpcs.com"
    				end if
    				-- if no email address found
    				if theEmail is "" then
    					set theEmail to "UNKNOWN GATEWAY"
    				end if
    				
    				-- checks all email addresses of the person for duplicates
    				set emailList to every email of this_person
    				set dupe to false
    				repeat with existingMail in emailList
    					if value of existingMail is equal to theEmail then
    						set dupe to true
    					end if
    				end repeat
    				-- if there are no duplicates, add the new email address
    				if dupe is false then
    					make new email at end of emails of this_person with properties {label:"mobile", value:theEmail}
    				end if
    				
    			end if
    		end repeat
    	end repeat
    	
    	save addressbook
    	
    end tell
     

Share This Page