Pair your Bluetooth Mouse automatically

Apple’s Mighty Mouse is a great device, but you have to clean it periodically.
Sometimes I do it during work, if one direction is starting to “stutter”.
This script can reconnect the mouse quickly. Using FastScripts (or something like that) you can run it from the keyboard.

activate application "Bluetooth Setup Assistant"
tell application "System Events"
	tell process "Bluetooth Setup Assistant"
		tell window 1
			click button 4 -- button "Continue"
			delay 1
			click button 4 -- button "Continue"
			repeat until exists static text 3 of group 1
				delay 0.5
			end repeat
			repeat until (count rows of table 1 of scroll area 1 of group 1) > 0
				delay 0.5
			end repeat
			tell button 4
				click -- button "Continue"
				set n to its title
				repeat while its title is n -- wait until the button change to "Quit"
					delay 0.5
				end repeat
				click -- button "Quit"
			end tell
		end tell
	end tell
end tell

PS: the script shoud work independent of any localization

I didn’t follow this through because I only have one bluetooth device to test on: my phone, but your elegant script can be generalized as follows: (untested, and, of course, linguistically sensitive)

tell application "System Events" to if exists process "Bluetooth Setup Assistant" then quit application "Bluetooth Setup Assistant"
set chosen to character 1 of ((choose from list {"1. Mouse", "2. Keyboard", "3. Mobile Phone", "4. Printer", "5. Headset", "6. Any Device"}) as text) as number -- returns an integer from 1 to 6
activate application "Bluetooth Setup Assistant"
tell application "System Events"
	tell process "Bluetooth Setup Assistant"
		tell window 1
			click button 4 -- button "Continue"
			delay 1
			click radio button chosen of radio group 1 of group 1 -- chosen is number of the device.
			click button 4 -- button "Continue"
			repeat until exists static text 3 of group 1
				delay 0.5
			end repeat
			repeat until (count rows of table 1 of scroll area 1 of group 1) > 0
				delay 0.5
			end repeat
			tell button 4
				click -- button "Continue"
				set n to its title
				repeat while its title is n -- wait until the button change to "Quit"
					delay 0.5
				end repeat
				click -- button "Quit"
			end tell
		end tell
	end tell
end tell

Very nice, Adam, thank you for the enhancement :smiley:

Hi
The script looks almost perfect but I have become stuck on a few bits.

I was trying to create a script to pair my bluetooth headset but when I run the script it stops at the point where I have to enter a pass code. I have tried modifying the script but to no avail - I have only been using Applescript for a very short time :frowning:

The final task I am attempting is to unpair my headset.

Basically if I go out of range it screws up the settings so I thought the best solution would be to automate the unpairing and subsequent repairing of the headset.

Any help is greatfully received

many thanks

Mark

running Leopard

Hi, Mark;

Show us your code so far and perhaps we can help (don’t include your real pass code if it’s there).

Hi,

here is a modified script to pair a headset.
I also tried to write a script to disconnect the headset (UI scripting of System Preferences), but I got a couple of kernel panics.
So I gave up!


property pass : "0000"

activate application "Bluetooth Setup Assistant"
tell application "System Events"
	tell process "Bluetooth Setup Assistant"
		tell window 1
			click button 4 -- button "Continue"
			delay 1
			click radio button "Headset" of radio group 1 of group 1
			click button 4 -- button "Continue"
			repeat until exists static text 3 of group 1
				delay 0.5
			end repeat
			repeat until (count rows of table 1 of scroll area 1 of group 1) > 0
				delay 0.5
			end repeat
			click button 4
			delay 0.5
			repeat until enabled of button 4
				delay 0.5
			end repeat
			repeat until value of static text 3 of group 1 contains "passkey" -- works probably only on english systems!
				click button 4
				delay 0.5
			end repeat
			keystroke pass
			tell button 4
				click -- button "Continue"
				set n to its title
				repeat while its title is n -- wait until the button change to "Quit"
					delay 0.5
				end repeat
				click -- button "Quit"
			end tell
		end tell
	end tell
end tell

Note: The option to pair a known headset again without entering a passkey is not considered