Applescript to Toggle Bluetooth (code included)

Discussion in 'Mac Programming' started by GimmeSlack12, Aug 9, 2006.

  1. macrumors 603

    GimmeSlack12

    #1
    Alright, I think I am close to getting this working. I am trying to toggle Bluetooth via GUI scripting in 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!
     
  2. macrumors 65816

    Mr_Brightside_@

    #2
    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.
     
  3. macrumors 603

    GimmeSlack12

    #3
    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?
     
  4. macrumors 65816

    Unorthodox

    #4
    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.

     
  5. macrumors 603

    GimmeSlack12

    #5
    Well done Unorthodox. I really appreciate it. I will also be able to learn a bit more about Applescript from this. Cheers.
     
  6. macrumors 65816

    Unorthodox

    #6
    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! :)

     
  7. macrumors newbie

    #7
    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 ?
     
  8. macrumors 603

    GimmeSlack12

    #8
    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.
     
  9. hepcat72, Sep 15, 2011
    Last edited: Sep 15, 2011

    macrumors newbie

    #9
    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
     
  10. macrumors newbie

    #10
    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
     
  11. macrumors newbie

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

Share This Page