Go Back   MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old Aug 9, 2006, 03:09 PM   #1
GimmeSlack12
macrumors 603
 
GimmeSlack12's Avatar
 
Join Date: Apr 2005
Location: San Francisco
Applescript to Toggle Bluetooth (code included)

Alright, I think I am close to getting this working. I am trying to toggle Bluetooth via GUI scripting in System Preferences.

Quote:
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!
__________________
iMac 24" iBook G4 iPhone 4 iPod 80Gb
GimmeSlack12 is offline   0 Reply With Quote
Old Aug 9, 2006, 03:23 PM   #2
Mr_Brightside_@
macrumors 65816
 
Mr_Brightside_@'s Avatar
 
Join Date: Sep 2005
Location: below Ash Tree Lane
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.
__________________
All over the ship, dancing through the wreckage of the Pfhor computer core, DURANDAL WAS LAUGHING
Here is the toil of that house, and the inextricable wandering
Mr_Brightside_@ is offline   0 Reply With Quote
Old Aug 9, 2006, 07:38 PM   #3
GimmeSlack12
Thread Starter
macrumors 603
 
GimmeSlack12's Avatar
 
Join Date: Apr 2005
Location: 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?
__________________
iMac 24" iBook G4 iPhone 4 iPod 80Gb
GimmeSlack12 is offline   0 Reply With Quote
Old Aug 9, 2006, 09:25 PM   #4
Unorthodox
macrumors 65816
 
Unorthodox's Avatar
 
Join Date: Mar 2006
Location: 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.

Quote:
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
__________________
Advertise your site, software, or service in this space and be seen over 100,000ish times per day by your target audience. Click here for info.
Unorthodox is offline   0 Reply With Quote
Old Aug 10, 2006, 06:57 AM   #5
GimmeSlack12
Thread Starter
macrumors 603
 
GimmeSlack12's Avatar
 
Join Date: Apr 2005
Location: San Francisco
Well done Unorthodox. I really appreciate it. I will also be able to learn a bit more about Applescript from this. Cheers.
__________________
iMac 24" iBook G4 iPhone 4 iPod 80Gb
GimmeSlack12 is offline   0 Reply With Quote
Old Aug 10, 2006, 05:36 PM   #6
Unorthodox
macrumors 65816
 
Unorthodox's Avatar
 
Join Date: Mar 2006
Location: Not at the beach...
Quote:
Originally Posted by GimmeSlack12
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!

Quote:
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()
__________________
Advertise your site, software, or service in this space and be seen over 100,000ish times per day by your target audience. Click here for info.
Unorthodox is offline   0 Reply With Quote
Old Sep 8, 2006, 06:15 PM   #7
banania
macrumors newbie
 
Join Date: Sep 2006
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 ?
banania is offline   0 Reply With Quote
Old Sep 8, 2006, 06:19 PM   #8
GimmeSlack12
Thread Starter
macrumors 603
 
GimmeSlack12's Avatar
 
Join Date: Apr 2005
Location: San Francisco
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.
__________________
iMac 24" iBook G4 iPhone 4 iPod 80Gb
GimmeSlack12 is offline   0 Reply With Quote
Old Sep 15, 2011, 01:18 PM   #9
hepcat72
macrumors newbie
 
Join Date: Apr 2010
Quote:
Originally Posted by Unorthodox View Post
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 by hepcat72; Sep 15, 2011 at 01:59 PM.
hepcat72 is offline   0 Reply With Quote
Old Apr 30, 2012, 09:10 AM   #10
hepcat72
macrumors newbie
 
Join Date: Apr 2010
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 is offline   0 Reply With Quote
Old Dec 3, 2012, 09:09 AM   #11
hepcat72
macrumors newbie
 
Join Date: Apr 2010
No longer works

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

Click image for larger version

Name:	bluetooth_script_issue.JPG
Views:	13
Size:	31.5 KB
ID:	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 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
hepcat72 is offline   0 Reply With Quote

Reply
MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Similar Threads
thread Thread Starter Forum Replies Last Post
Quick WiFi / Bluetooth Toggle? paulie6978 iOS 7 4 Jun 10, 2013 04:37 PM
General: A Question Regarding SBSettings' BlueTooth Toggle The Mad Hatter Jailbreaks and iOS Hacks 2 Feb 21, 2013 07:42 AM
iPhone 5 - Unable to Toggle Bluetooth/Wifi off zachnelson iPhone 0 Sep 21, 2012 03:21 PM
Bluetooth toggle app for iOS 6? Pared iOS 6 3 Sep 5, 2012 10:28 AM
Target Display toggle only works with Apple Bluetooth Keyboard? kylera iMac 3 Jun 29, 2012 05:56 AM

Forum Jump

All times are GMT -5. The time now is 12:44 PM.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps

Mobile Version | Fixed | Fluid | Fluid HD
Copyright 2002-2013, MacRumors.com, LLC