Scripting Address Book to send SMSs

I’d like to be able to have scripts send SMS and access the Dial bluetooth phone actions. These are only present on phone numbers if a bluetooth phone is connected (the B thing goes blue)

I had a look at the Address Book dictionary and started hard at perform action but didn’t make it sing. If you can think of how to access those menu items without GUI scripting then I’ll be happy :wink:

So I turned to GUI scripting. Using the Interface Browser from apple I got this almost working (replace James Howison with the name of a card in your address book):

This correctly pops up the menu with the actions in it but the second click line that actually tries to click the menu item doesn’t work and I get the dreaded, “System Events got an error: NSReceiverEvaluationScriptError: 4” which isn’t very helpful. Tried it with and without the delay line.

Clearly I’m accessing the menu item wrongly or something similar


tell application “Address Book”
set someones_ID to person “James Howison”'s id
end tell

open location “addressbook://” & someones_ID

activate application “Address Book”
tell application “System Events”
tell process “Address Book”
– GUI Scripting statements:
click pop up button “mobile” of scroll area 3 of splitter group 1 of window “Address Book”
delay 2
click menu item “SMS Message” of menu 1 of scroll area 3 of splitter group 1 of window “Address Book”
end tell
end tell

Any ideas (be they vanilla Applescript or GUI) much appreciated!

Hi James,

I am trying to do this too! Any luck yet?

Do you have to use address book?

Thanks drpete

Actually I did get this to work :slight_smile: Thanks to a fellow at FOOCamp whose name I’ve sadly forgotten, he was an AppleScript guru.

tell application “Address Book”
set someones_ID to person “Adrian Elton”'s id
end tell

open location “addressbook://” & someones_ID

activate application “Address Book”
tell application “System Events”
tell process “Address Book”
click pop up button “mobile” of scroll area 3 of splitter group 1 of window “Address Book”
keystroke “d”
keystroke return
end tell
end tell

So basically instead of trying to click on the popup menu you can use the keystroke commands to select the elements of the menu. Simple, huh?

hth,
James

I also have a script that automates the process of creating such scripts for everyone in your address book, which can then be accessed via Quicksilver or LaunchBar or similar. Ah for the day when such tools make their way to cell phones!

tell application “Address Book”
repeat with thisPerson in the people
if (exists phone in thisPerson) then
set thisName to name of thisPerson
set pathName to “Users:james:Documents:Development:CellPhoneInterface:tasks:”
set dialFileName to pathName & "Call " & thisName & “.scpt”
set txtFileName to pathName & "TXT " & thisName & “.scpt”

		set dialContent to "tell application \"Address Book\" 

set someones_ID to person "" & thisName & ""'s id
end tell

open location "addressbook://" & someones_ID

activate application "Address Book"
tell application "System Events"
tell process "Address Book"
click pop up button "mobile" of scroll area 3 of splitter group 1 of window "Address Book"
keystroke "d"
keystroke return
end tell
end tell" as string

		my write_to_file(dialContent, dialFileName, false)
		
		set txtContent to "set theString to the text returned of (display dialog \"What message would you like to send?\" default answer \"Cellphones suck, let's fix them?\")

tell application "Address Book"
set someones_ID to person "" & thisName & ""'s id
end tell

open location "addressbook://" & someones_ID

activate application "Address Book"
tell application "System Events"
tell process "Address Book"
click pop up button "mobile" of scroll area 3 of splitter group 1 of window "Address Book"
keystroke "s"
keystroke return
keystroke theString
click button "Send" of sheet 1 of window "Address Book"
end tell
end tell" as string

		my write_to_file(txtContent, txtFileName, false)
		
	end if
end repeat

end tell

on write_to_file(this_data, target_file, append_data)
try
set the target_file to the target_file as text
set the open_target_file to ¬
open for access file target_file with write permission
if append_data is false then ¬
set eof of the open_target_file to 0
write this_data to the open_target_file starting at eof
close access the open_target_file
return true
on error
try
close access file target_file
end try
return false
end try
end write_to_file