Connect iPhone to Bluetooth

Here is a sample script to connect your iPhone to Bluetooth. It took a lot of trial and error and thanks to others on this forum for posting their code. I even found some good help on Google Code. So, here’s my finished product. I hope that it helps others trying to accomplish the same thing.

The logic is: When I connect to my iPhone, I want to turn off the wi-fi connection to save the battery and make sure bluetooth is on, turn it on if its not, and then connect to my phone. I modified this script to disconnect the iPhone too.

One cool thing to note, is that you don’t have to use the Show command for system preferences. If you omit that step, the script still runs but you don’t see the changes occurring in the system preferences window. It looks cleaner.

Good luck!!

– Created by Chad Carney on 3/8/12.

– iPhone Connect

TurnBluetoothOn()
TurnWifiOff()
CleanUp()

on Bluetooth()
set AppleScript’s text item delimiters to “.”
set btooth to “Bluetooth”
set netWorkPane to “com.apple.preference.network”
set winNetwork to “Network”

tell application "System Preferences"
	set current pane to pane netWorkPane
end tell

tell application "System Events" to tell process "System Preferences"
	set theRow to row 1 of table 1 of scroll area 1 of window winNetwork whose value of static text 1 contains btooth
	select theRow --clicks the bluetooth row
	--If Bluetooth is already connected, the button will say Disconnect, so we don't want to turn it off:
	try
		click (button 1 of group 1 of window winNetwork whose title is "Connect")
	end try
end tell

end Bluetooth

on TurnWifiOff()
set AppleScript’s text item delimiters to “.”
set wifi to “Wi-Fi”
set winNetwork to “Network”
set netWorkPane to “com.apple.preference.network”

tell application "System Preferences"
	set current pane to pane netWorkPane
end tell

tell application "System Events" to tell process "System Preferences"
	--Click on Network in the left scroll area:
	set theRow to row 1 of table 1 of scroll area 1 of window winNetwork whose value of static text 1 contains wifi
	select theRow
	-- use the try block in case wi-fi is already off, then it moves on w/ no error message:
	try
		click button "Turn Wi-Fi Off" of group 1 of window winNetwork
	end try
end tell

end TurnWifiOff

on TurnBluetoothOn()
set AppleScript’s text item delimiters to “.”
set btooth to “Bluetooth”
set winBtooth to “Bluetooth”
–NOTE: Notice that there is an “s” in preference for Bluetooth. That will mess you up!:
set bToothPane to “com.apple.preferences.Bluetooth”

tell application "System Preferences"
	set current pane to pane bToothPane
end tell

--See if Bluetooth is off.  If it is turn it on:          
tell application "System Events" to tell process "System Preferences"
	set chkBoxState to value of checkbox "On" of window winBtooth
	if chkBoxState = 0 then
		click checkbox "On" of window winBtooth
	end if
end tell

end TurnBluetoothOn

on CleanUp()
tell application “System Preferences”
quit
end tell
end CleanUp

Hi, ¨¨I’m having trouble getting this to work. I’m trying to use this so that I can use a keyboard shortcut with QS or Keyboard Maestro to enable internet tethering with my iPhone via a script like this, so any assistance to make this script work is greatly appreciated!! -A

What exactly is your trouble? :slight_smile:

I’m specifically have trouble making the script to essentially hit “connect” in the Network preference pane. The script opens System Preferences and sets the preference pane to Network, but can’t figure out how to make it ‘click’ on the “connect” button.

Hello.

Please try to run this:

tell application "System Events"
	if not UI elements enabled then
		set UI elements enabled to true
	end if
end tell

Then try to re-run the script above.

Thanks for the consideration McUsrII, but it was a no go. And on second thought the script actually doesn’t bring to focus the System Preferences.

I’m only focusing on the “Bluetooth()” portion of the script. I’ve removed the other sections as I don’t need it. So this is the current script I’m working on:

--  Created by Chad Carney on 3/8/12.
--
-- iPhone Connect


on Bluetooth()
	set AppleScript's text item delimiters to "."
	set btooth to "Bluetooth PAN"
	set netWorkPane to "com.apple.preference.network"
	set winNetwork to "Network"
	
	tell application "System Preferences"
		set current pane to pane netWorkPane
	end tell
	
	tell application "System Events" to tell process "System Preferences"
		set theRow to row 1 of table 1 of scroll area 1 of window winNetwork whose value of static text 1 contains btooth
		select theRow --clicks the bluetooth row
		--If Bluetooth is already connected, the button will say Disconnect, so we don't want to turn it off:
		try
			click (button 1 of group 1 of window winNetwork whose title is "Connect")
		end try
	end tell
end Bluetooth

Hello.

I have just made the System Preferences pane activate, see if that works.

--  Created by Chad Carney on 3/8/12.
--
-- iPhone Connect


on Bluetooth()
	set AppleScript's text item delimiters to "."
	set btooth to "Bluetooth PAN"
	set netWorkPane to "com.apple.preference.network"
	set winNetwork to "Network"
	
	tell application "System Preferences"
		activate
		set current pane to pane netWorkPane
	end tell
	
	tell application "System Events" to tell process "System Preferences"
		set theRow to row 1 of table 1 of scroll area 1 of window winNetwork whose value of static text 1 contains btooth
		select theRow --clicks the bluetooth row
		--If Bluetooth is already connected, the button will say Disconnect, so we don't want to turn it off:
		try
			click (button 1 of group 1 of window winNetwork whose title is "Connect")
		end try
	end tell
end Bluetooth

Awesome! Thank you McUsrII! I also did some additional minor tweaks: In Network pane, I removed and readded the Bluetooth network with the name “Bluetooth” (removed PAN from the name); in the script, I just removed the “Bluetooth()” and “end Bluetooth” and recompiled the script.

Now the following script works, at least on my Mac:

--  Created by Chad Carney on 3/8/12.
--
-- iPhone Connect

tell application "System Preferences"
	activate
	set AppleScript's text item delimiters to "."
	set current pane to pane "com.apple.preference.network"
	set winNetwork to "Network"
	set btooth to "Bluetooth"
	
	tell application "System Events" to tell process "System Preferences"
		set theRow to row 1 of table 1 of scroll area 1 of window winNetwork whose value of static text 1 contains btooth
		select theRow --clicks the bluetooth row
		--If Bluetooth is already connected, the button will say Disconnect, so we don't want to turn it off:
		try
			click (button 1 of group 1 of window winNetwork whose title is "Connect")
		end try
	end tell
end tell

Thanks again McUserII, I’m pretty sure I didn’t have the UI elements enabled before employing this script, so thank you for the consideration!

-A

Hello.

I am glad it works for you. I use something like this for turning off airport (in an Applet), it is work in progress, as I’d have to test that the interface is up before I let go, if I am to rely on it from a script, and not just using it manually, as a replacement for the QS command.

do shell script "networksetup -setairportpower en1 off"

I am sure you can achieve something similiar for bluetooth, and scrap the UI Scripting alltogether!

And if you do, please post your solution! :slight_smile:

I’m not sure exactly what you’re aiming with regards to enabling/disabling airport, but I think I may have a workaround. So the purpose of this script is to help me connect to the internet by tethering to my iPhone via bluetooth. I first created a new network location which only has the bluetooth connection in the network services under the Network preference pane. So any time I switch to this location, I’ve essentially turned off WiFi and enabled the potential connection to the already paired bluetooth device.

With the additional changes I’ve made to the above script I can now first enable bluetooth and then connect to the device. Here’s what I conjured up to enable/disable bluetooth and connect to bluetooth device. To get it to work you first must install blueutil. I’ve also added Growl notifications to the script, so if you don’t have Growl installed please remove the lines from the scripts that refer to Growl.

-- Enable Bluetooth and Connect to iPhone

property blueutilPath : "/usr/local/bin/blueutil"

-- Turn on bluetooth.
if execBlueutil("") is "0" then
	execBlueutil("1")
	tell application "Growl"
		set the allNotificationsList to ¬
			{"Bluetooth Setting"}
		set the enabledNotificationsList to ¬
			{"Bluetooth Setting"}
		register as application ¬
			"AppleScript - Bluetooth" all notifications allNotificationsList ¬
			default notifications enabledNotificationsList
		notify with name ¬
			"Bluetooth Setting" title ¬
			"Bluetooth is On & iPhone Connected" description ¬
			"Bluetooth has been enabled with iPhone tethered." application name "AppleScript - Bluetooth" icon of file (path to me)
	end tell
end if

on execBlueutil(command)
	set res to do shell script blueutilPath & " power " & command
	if res contains "Error" then
		display dialog res
		quit
	end if
	return res
end execBlueutil

-- Connect Device
tell application "System Preferences"
	activate
	set AppleScript's text item delimiters to "."
	set current pane to pane "com.apple.preference.network"
	set winNetwork to "Network"
	set btooth to "Bluetooth"
	
	tell application "System Events" to tell process "System Preferences"
		set theRow to row 1 of table 1 of scroll area 1 of window winNetwork whose value of static text 1 contains btooth
		select theRow --clicks the bluetooth row
		--If Bluetooth is already connected, the button will say Disconnect, so we don't want to turn it off:
		try
			click (button 1 of group 1 of window winNetwork whose title is "Connect")
		end try
	end tell
	tell application "System Preferences"
		quit
	end tell
end tell
-- Disable Bluetooth

property blueutilPath : "/usr/local/bin/blueutil"

-- Turn off Bluetooth.
execBlueutil("0")
tell application "Growl"
	set the allNotificationsList to ¬
		{"Bluetooth Setting"}
	set the enabledNotificationsList to ¬
		{"Bluetooth Setting"}
	notify with name ¬
		"Bluetooth Setting" title ¬
		"Bluetooth Off" description ¬
		"Bluetooth has been disabled." application name "AppleScript - Bluetooth" icon of file (path to me)
end tell

on execBlueutil(command)
	set res to do shell script blueutilPath & " power " & command
	if res contains "Error" then
		display dialog res
		quit
	end if
	return res
end execBlueutil

I’ve ran these scripts a couple of times and I didn’t have any errors with it. So now with Quicksilver I can set up triggers which are essentially keyboard shortcuts to run each of these scripts. I’m a complete novice at writing scripts so I can’t promise anything if you’ve got errors, so I’m hoping others can lend support if there are any errors.

Also if there is anything additional that could be added to make these scripts better please don’t hesitate to add.

-A

Hello.

I meant that at least after you have performed the initial setup, then there should be some command-line tools, to open and close the bluetooth connection. :slight_smile: So you don’t need to use UI scripting. It will leave your screen more stable, and such a solution would work a little bit faster.

Ohhh, I didn’t know that I could do that through the command line. That’s pretty neat. Thanks for the heads up. I’ll look into it and see if I can figure it out.

Hello.

I googled “OS X bluetooth commandline” and I found this post that should get you in the right direction.

Here is a rather old article from Mac Os X hints.com this seems to elaborate, and is old.

I was more thinking that you set up your phone as default in the bluetooth preferences pane, and then use the method described in the first post, if that is possible then.

The slide here marked “Disabling bluetooth” should provide some info.

I used the searchwords: in google, and that looks promising at least for me, as you don’t have to get the same results. :confused:

connect iphone to mac with bluetooth from commandline

I wish you good luck with this!

My general idea is, that you set up your setup in the preferences pane, and assures that it works from there.

From thereafter, you can use command-line utilies against your default settings.