Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

GimmeSlack12

macrumors 603
Original poster
Apr 29, 2005
5,403
12
San Francisco
Alright, I think I am close to getting this working. I am trying to toggle Bluetooth via GUI scripting in System Preferences.

tell application "System Preferences"
activate
set current pane to pane "com.apple.preferences.Bluetooth"

tell application "System Events" to tell window "Bluetooth" of process "System Preferences"
click radio button "Settings" of tab group 1

if exists button "Turn Bluetooth On" then
tell button "Turn Bluetooth On"
click
end tell

else

tell button "Turn Bluetooth Off"
click
end tell
end if

end tell
end tell
quit application "System Preferences"

The problems I am getting, is that it does not activate Bluetooth. And it errors on the Turn Off click command. Please help!
 

Mr_Brightside_@

macrumors 68040
Sep 23, 2005
3,747
2,035
Toronto
tell application "Finder"
activate
select Finder window 1
select Finder window 1
set target of Finder window 1 to folder "Applications" of startup disk
select Finder window 1
select Finder window 1
open application file "System Preferences.app" of folder "Applications" of startup disk
select Finder window 1
end tell
that is as far as i got .... sorry i just recorded it.
 

GimmeSlack12

macrumors 603
Original poster
Apr 29, 2005
5,403
12
San Francisco
Yeah, I can't really say that is the way to do it Brightside. The recording feature is for something, I don't know what, but something.

Anyone else?
 

Unorthodox

macrumors 65816
Mar 3, 2006
1,087
1
Not at the beach...
Weeee. That was fun. I haven't scripted in such a long time....
*sigh*
Oh well.
Here you go! Just save it as a app and set it to a keyboard shortcut, or an F key.

tell application "System Preferences"
activate
end tell

tell application "System Preferences"
activate
set current pane to pane "com.apple.preferences.Bluetooth"

tell application "System Events"
if (UI elements enabled) then
tell process "System Preferences"
try
click radio button 1 of tab group 1 of window 1
click button 1 of tab group 1 of window 1

tell application "System Preferences"
quit
end tell

return 1
on error
return 2
end try
end tell
else
return 0
end if
end tell
end tell
 

Unorthodox

macrumors 65816
Mar 3, 2006
1,087
1
Not at the beach...
GimmeSlack12 said:
Well done Unorthodox. I really appreciate it. I will also be able to learn a bit more about Applescript from this. Cheers.

Glad it worked. But I gave you the wrong one. Opps!

I meant to give you this one. (includes cool aqua icon)

It's basically the same thing except it tells if there's an error.(GUI not enabled, no bluetooth detected)
So it's more user friendly.
Cheers! :)

on try_again()
set try_again2 to false
set error1 to false

tell application "System Events"
if (UI elements enabled) then

tell application "System Preferences"
activate
tell application "Finder"
set visible of every process whose visible is true and name is "System Preferences" to false
end tell

try
set current pane to pane "com.apple.preferences.Bluetooth"

on error
set error1 to true
beep
activate
display dialog "An error has occurred!
Please make sure your bluetooth module is properly installed." buttons {"PM Unorthodox", "Try again", "OK"} default button 3 with icon caution
if the button returned of the result is "PM Unorthodox" then
quit
tell application "Safari"
activate
open location "https://forums.macrumors.com/private.php?do=newpm&u=68509"
end tell
else
if the button returned of the result is "Try again" then
quit
set try_again2 to true
else
quit
end if
end if
end try
end tell

if error1 is false then
tell application "System Events"
tell process "System Preferences"
click radio button 1 of tab group 1 of window 1
click button 1 of tab group 1 of window 1
end tell
end tell

tell application "System Preferences"
quit
end tell
end if

else
activate
display dialog "You have to enable have GUI elements enabled to use this script." buttons {"Enable GUI", "OK"} with icon note default button 2
if the button returned of the result is "Enable GUI" then
tell application "AppleScript Utility"
activate
end tell
end if

end if
end tell

if try_again2 is true then
try_again()
end if
end try_again
try_again()
 

banania

macrumors newbie
Sep 8, 2006
3
0
Need Applescript to SMS via bluetooth Cell

Hi all,

I'm looking for a small applescript who can allow me sending sms message via bluetooth with my Filemaker Pro database.
Not for sms by internet but i want to be able to use my bluetooth cellphone connection for that like with the Mac Address Book.

someone can help me ?
 

hepcat72

macrumors newbie
Apr 23, 2010
14
0
Glad it worked. But I gave you the wrong one. Opps!

I meant to give you this one. (includes cool aqua icon)

It's basically the same thing except it tells if there's an error.(GUI not enabled, no bluetooth detected)
So it's more user friendly.
Cheers! :)

Great script! I customized it a little so that I could use it as an iCal alarm to turn off bluetooth at the end of the day so I can save the battery life of my magic mouse (and if I am still working, I added a 'snooze'). I never turn off my computer because I have so many concurrent projects running all the time. I'd set an alarm to remind me to turn off the mouse every day, but I rarely remembered even then, so with this alarm, I added a give-up time so if I don't respond to the dialog, it turns off bluetooth anyway.

I took out your link because you need a macrumors account to see it, which I didn't want to require if I shared the script with others).

To turn bluetooth on with a keyboard shortcut, I installed http://www.red-sweater.com/fastscripts/ and mapped a similar script to F13. Note: Keyboard shortcuts don't apparently work in 10.6.x for the script menu, bluetooth menu, or even the recent items menu - I tried for hours with multiple restarts - web searches corroborated my findings.

Here's the modified script:

Code:
on try_again()
	set response to (display dialog "Would you like to turn off bluetooth?
(to save the magic mouse battery life)" buttons {"Cancel", "Gimme an hour", "Turn Bluetooth Off"} with icon 1 default button 3 giving up after 600)
	
	if gave up of response is true or button returned of response is "Turn Bluetooth Off" then
		
		set try_again2 to false
		set error1 to false
		
		tell application "System Events"
			if (UI elements enabled) then
				
				tell application "System Preferences"
					activate
					tell application "Finder"
						set visible of every process whose visible is true and name is "System Preferences" to false
					end tell
					
					try
						set current pane to pane "com.apple.preferences.Bluetooth"
						
					on error
						set error1 to true
						beep
						activate
						display dialog "An error has occurred!
Please make sure your bluetooth module is properly installed." buttons {"Try again", "OK"} default button 2 with icon caution
						if the button returned of the result is "Try again" then
							quit
							set try_again2 to true
						else
							quit
						end if
					end try
				end tell
				
				if error1 is false then
					tell application "System Events"
						tell process "System Preferences"
							if value of checkbox 2 of window 1 is 1 then
								click checkbox 2 of window 1
								click button 1 of window 1
							end if
						end tell
					end tell
					tell application "System Preferences"
						quit
					end tell
				end if
				
			else
				activate
				display dialog "You have to enable have GUI elements enabled to use this script." buttons {"Enable GUI", "OK"} with icon note default button 2
				if the button returned of the result is "Enable GUI" then
					tell application "AppleScript Utility"
						activate
					end tell
				end if
				
			end if
		end tell
		
		if try_again2 is true then
			try_again()
		end if
	else if button returned of response is "Gimme an hour" then
		delay 3600
		try_again()
	end if
end try_again
try_again()

Rob
 
Last edited:

hepcat72

macrumors newbie
Apr 23, 2010
14
0
I recently started experiencing a problem with my scripts for toggling bluetooth. I have them set up to be activated via both ScriptSaver and FastScripts. Neither appears to be working right now and I get this error when the script tries to manipulate the System Preferences pane for bluetooth:

Error Number:System Events got an error: Can’t get window 1 of process "System Preferences". Invalid index.
-1719

Anyone know how to fix that? Since I had to set up via scriptsaver, it actually caused my screensaver to not activate all weekend and now I have burn-in on my monitor! It's not real bad, but when there are dark colors on the screen, I can see a burned in excel spreadsheet. Yikes! I didn't think monitors these days were subject to that kind of problem.

Anyway, I've been trying to debug this problem and cannot seem to figure it out.

Any help?

Thanks,
Rob
 

hepcat72

macrumors newbie
Apr 23, 2010
14
0
No longer works

Looks like some sort of system update added an alert window for whenever the bluetooth checkbox is unchecked in system preferences:

bluetooth_script_issue.JPG

For the purposes of google, the alert window says "If you turn bluetooth off you might not be able to control the computer. For example, you won't be able to use a bluetooth keyboard or mouse. Are you sure you want to turn Bluetooth off?"

I noticed that my mouse was still active upon unlocking my screen when it should have been off, so my mouse battery has been draining fast, especially over the weekends.

The alert window is a prudent improvement, but I could not figure out how to reliably dismiss it. I was able to dismiss it via the script by adding an 'ignore application responses' block with a 'keystroke space', but that wouldn't work via scriptsaver. And I could not figure out how to click the alert window button to "Turn Bluetooth Off" via the script.

So I found an alternative on this thread for a command line utility called blueutil:

Code:
-- put "blueutil" in ressources folder of this script saved as Script-Bundle or Application
set blueutilpath to quoted form of (POSIX path of (path to resource "blueutil"))
 
-- or start with this line if "blueutil" installed:
-- set blueutilpath to "usr/local/bin/blueutil"
 
set sb to last word of (do shell script blueutilpath & " status")
if sb is "on" then
  do shell script blueutilpath & " off"
else
  do shell script blueutilpath & " on"
end if
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.