PDA

View Full Version : Applescript to Toggle Bluetooth (code included)




GimmeSlack12
Aug 9, 2006, 03:09 PM
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_@
Aug 9, 2006, 03:23 PM
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
Aug 9, 2006, 07:38 PM
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
Aug 9, 2006, 09:25 PM
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 (http://www.shadowlab.org/Software/software.php?sign=Sprk), or an F key. (http://www.zarkonnen.com/projects/xkeys.php)

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

GimmeSlack12
Aug 10, 2006, 06:57 AM
Well done Unorthodox. I really appreciate it. I will also be able to learn a bit more about Applescript from this. Cheers.

Unorthodox
Aug 10, 2006, 05:36 PM
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 (http://www.box.net/public/v9pm4ote09). (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 "http://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
Sep 8, 2006, 06:15 PM
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 ?

GimmeSlack12
Sep 8, 2006, 06:19 PM
Banania let me direct you to this Forum:
http://bbs.applescript.net/index.php

It is an AppleScript forum and they tend to help out with stuff fairly quickly. If not there is a bunch of threads that you can search that might help you out with your Endeavor. Hope it helps.

hepcat72
Sep 15, 2011, 01:18 PM
Glad it worked. But I gave you the wrong one. Opps!

I meant to give you this one (http://www.box.net/public/v9pm4ote09). (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:

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

hepcat72
Apr 30, 2012, 09:10 AM
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
Dec 3, 2012, 09:09 AM
Looks like some sort of system update added an alert window for whenever the bluetooth checkbox is unchecked in system preferences:

381299

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 (https://discussions.apple.com/thread/3179367?start=0&tstart=0) for a command line utility called blueutil (http://www.frederikseiffert.de/blueutil/):

-- 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