Connect to bluetooth device via QS

150 views
Skip to first unread message

Afshin

unread,
Jul 2, 2013, 9:17:21 PM7/2/13
to blacktree-...@googlegroups.com
Hi all, 

I'm wondering if there's a way to connect to a bluetooth device, i.e. iPhone, via QS?  

Thanks! 
-Afshin

Patrick Robertson

unread,
Jul 2, 2013, 9:30:32 PM7/2/13
to blacktree-...@googlegroups.com
Unfortunately not at the moment, it would require a new plugin.

I've been meaning to add a script to the wiki which allows you to turn bluetooth on/off, but haven't got round to it yet. Here it is.

It requires you to install blueutil (and Growl), but once they're installed, just run the script to toggle Bluetooth on/off


Script:

property blueutilPath : "/usr/local/bin/blueutil"


-- Check the current bluetooth status and turn it on if necessary.
if execBlueutil("") is "0" then
execBlueutil("1")
tell application "Growl"
set the allNotificationsList to {"Bluetooth Setting"}
set the enabledNotificationsList to {"Bluetooth Setting"}
notify with name "Bluetooth Setting" title "Bluetooth On" description "Bluetooth has been enabled." application name "AppleScript - Bluetooth" icon of file (path to me)
end tell
else
-- Set the bluetooth status to what it was before.
execBlueutil("0")
tell application "Growl"
set the allNotificationsList to {"Bluetooth Setting"}
set the enabledNotificationsList to {"Bluetooth Setting"}
notify with name "Bluetooth Setting" title "Bluetooth Off" description "Bluetooth has been disabled." application name "AppleScript - Bluetooth" icon of file (path to me)
end tell
end if


on execBlueutil(command)
set res to do shell script blueutilPath & " power " & command
if res contains "Error" then
display dialog res
quit
end if
return res
end execBlueutil




--
You received this message because you are subscribed to the Google Groups "Quicksilver" group.
To unsubscribe from this group and stop receiving emails from it, send an email to blacktree-quicks...@googlegroups.com.
To post to this group, send email to blacktree-...@googlegroups.com.
Visit this group at http://groups.google.com/group/blacktree-quicksilver.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Afshin Haghgoo

unread,
Jul 2, 2013, 10:30:16 PM7/2/13
to blacktree-...@googlegroups.com, Afshin Haghgoo
Thanks Patrick!  Though I'm having trouble getting this script to run properly.  I've installed Growl and blueutil, but each time I hit 'compile', I get a syntax error message saying, Expected end of line, etc. but found “"”.

If I try to run the script I get the following error message in the result box: error "Usage: /usr/local/bin/blueutil [status|on|off]" number 1

You or anyone else know how to resolve this? 

-A

--
Afshin Haghgoo

You received this message because you are subscribed to a topic in the Google Groups "Quicksilver" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/blacktree-quicksilver/dzPo4KBXpH4/unsubscribe.
To unsubscribe from this group and all its topics, send an email to blacktree-quicks...@googlegroups.com.

Patrick Robertson

unread,
Jul 3, 2013, 11:02:33 AM7/3/13
to blacktree-...@googlegroups.com
Be sure when you copy/paste the AppleScript that there are no line breaks

In fact, you made me get round to adding the Bluetooth script to the wiki - the whole reason why I added file attachments yesterday to the wiki… ;-)

Seems like the blueutil I originally linked to is an 'old' one. Download the one linked to on that wiki page and you should be good to go

Afshin Haghgoo

unread,
Jul 7, 2013, 10:48:18 AM7/7/13
to blacktree-...@googlegroups.com, Afshin Haghgoo
Yep the code that you put on the wiki helped.  And the error that I got from running the script was solved by installing the new version and also overwriting the link via "brew link --overwrite blueutil"

Now all that's needed is a way to connect to pre-linked devices that's listed in the bluetooth app via QS.  But for now I'm immensely grateful for this script!!  Thank   you Patrick for writing this and writing the wikipage to host it!  Very gracious and very much appreciated!  

Regards,
Afshin

--
Afshin Haghgoo

Afshin Haghgoo

unread,
Jul 7, 2013, 11:31:11 AM7/7/13
to blacktree-...@googlegroups.com, Afshin Haghgoo
To get the Growl notifications working, I had to add lines to register the script with Growl as indicated on the support page for AppleScript on Growl's website: 

property blueutilPath : "/usr/local/bin/blueutil"

-- Check the current bluetooth status and turn it on if necessary.
if execBlueutil("") is "0" then
execBlueutil("1")
tell application "Growl"
set the allNotificationsList to ¬
{"Bluetooth Setting"}
set the enabledNotificationsList to ¬
{"Bluetooth Setting"}
register as application ¬
"AppleScript - Bluetooth" all notifications allNotificationsList ¬
default notifications enabledNotificationsList

--
Afshin Haghgoo

Afshin Haghgoo

unread,
Jul 7, 2013, 1:25:11 PM7/7/13
to blacktree-...@googlegroups.com, Afshin Haghgoo
Re: connecting to bluetooth device, I found the following script made specially to connect to the iPhone via bluetooth.  But I'm having trouble making it work as I'm a complete novice when it comes to applescripts.  If anyone can spare some time to just get the script to connect to the iPhone or whatever bluetooth device, I'd be immensely grateful.  The turning off of WiFi and turning bluetooth on/off is not necessary.  -A.

--  Created by Chad Carney on 3/8/12.
--
-- iPhone Connect

TurnBluetoothOn()
TurnWifiOff()
CleanUp()

on Bluetooth()
set AppleScript's text item delimiters to "."
set btooth to "Bluetooth"
set netWorkPane to "com.apple.preference.network"
set winNetwork to "Network"


tell application "System Preferences"
set current pane to pane netWorkPane
end tell


tell application "System Events" to tell process "System Preferences"
set theRow to row 1 of table 1 of scroll area 1 of window winNetwork whose value of static text 1 contains btooth
select theRow --clicks the bluetooth row
--If Bluetooth is already connected, the button will say Disconnect, so we don't want to turn it off:
try
click (button 1 of group 1 of window winNetwork whose title is "Connect")
end try
end tell
end Bluetooth


on TurnWifiOff()
set AppleScript's text item delimiters to "."
set wifi to "Wi-Fi"
set winNetwork to "Network"
set netWorkPane to "com.apple.preference.network"


tell application "System Preferences"
set current pane to pane netWorkPane
end tell


tell application "System Events" to tell process "System Preferences"
--Click on Network in the left scroll area:
set theRow to row 1 of table 1 of scroll area 1 of window winNetwork whose value of static text 1 contains wifi
select theRow
-- use the try block in case wi-fi is already off, then it moves on w/ no error message:
try
click button "Turn Wi-Fi Off" of group 1 of window winNetwork
end try
end tell
end TurnWifiOff


on TurnBluetoothOn()
set AppleScript's text item delimiters to "."
set btooth to "Bluetooth"
set winBtooth to "Bluetooth"
--NOTE:  Notice that there is an "s" in preference for Bluetooth.  That will mess you up!:
set bToothPane to "com.apple.preferences.Bluetooth"


tell application "System Preferences"
set current pane to pane bToothPane
end tell


--See if Bluetooth is off.  If it is turn it on:          
tell application "System Events" to tell process "System Preferences"
set chkBoxState to value of checkbox "On" of window winBtooth
if chkBoxState = 0 then
click checkbox "On" of window winBtooth
end if
end tell
end TurnBluetoothOn


on CleanUp()
tell application "System Preferences"
quit
end tell
end CleanUp
--
Afshin Haghgoo

Afshin Haghgoo

unread,
Jul 7, 2013, 3:39:47 PM7/7/13
to blacktree-...@googlegroups.com, Afshin Haghgoo
I've modified the script that Patrick provided and combined it to the previous script which I've stripped down to just connect to the iPhone or primary bluetooth device.  I've also just added a script which only disables bluetooth, which is just the second half of the script that Patrick provided.

-- Enable Bluetooth and Connect to iPhone

property blueutilPath : "/usr/local/bin/blueutil"

-- Turn on bluetooth.
if execBlueutil("") is "0" then
execBlueutil("1")
tell application "Growl"
set the allNotificationsList to ¬
{"Bluetooth Setting"}
set the enabledNotificationsList to ¬
{"Bluetooth Setting"}
register as application ¬
"AppleScript - Bluetooth" all notifications allNotificationsList ¬
default notifications enabledNotificationsList
notify with name ¬
"Bluetooth Setting" title ¬
"Bluetooth is On & iPhone Connected" description ¬
"Bluetooth has been enabled with iPhone tethered." application name "AppleScript - Bluetooth" icon of file (path to me)
end tell
end if

on execBlueutil(command)
set res to do shell script blueutilPath & " power " & command
if res contains "Error" then
display dialog res
quit
end if
return res
end execBlueutil

-- Connect Device
tell application "System Preferences"
activate
set AppleScript's text item delimiters to "."
set current pane to pane "com.apple.preference.network"
set winNetwork to "Network"
set btooth to "Bluetooth"


tell application "System Events" to tell process "System Preferences"
set theRow to row 1 of table 1 of scroll area 1 of window winNetwork whose value of static text 1 contains btooth
select theRow --clicks the bluetooth row
--If Bluetooth is already connected, the button will say Disconnect, so we don't want to turn it off:
try
click (button 1 of group 1 of window winNetwork whose title is "Connect")
end try
end tell
tell application "System Preferences"
quit
end tell
end tell


--------------------------------------------------------------------------------------------------


-- Disable Bluetooth

property blueutilPath : "/usr/local/bin/blueutil"

-- Turn off Bluetooth.
execBlueutil("0")
tell application "Growl"
set the allNotificationsList to ¬
{"Bluetooth Setting"}
set the enabledNotificationsList to ¬
{"Bluetooth Setting"}
notify with name ¬
"Bluetooth Setting" title ¬
"Bluetooth Off" description ¬
"Bluetooth has been disabled." application name "AppleScript - Bluetooth" icon of file (path to me)
end tell

on execBlueutil(command)
set res to do shell script blueutilPath & " power " & command
if res contains "Error" then
display dialog res
quit
end if
return res
end execBlueutil


--
Afshin Haghgoo

Patrick Robertson

unread,
Jul 7, 2013, 8:30:40 PM7/7/13
to blacktree-...@googlegroups.com
Good to see people developing new scripts. Feel free to add it to the wiki ;-)

If you would like an account, just let me know what email address and username you want to use

Afshin Haghgoo

unread,
Jul 7, 2013, 10:42:56 PM7/7/13
to blacktree-...@googlegroups.com, Afshin Haghgoo
Thank you Patrick!  I'm humbled especially with such a rudimentary script.  'Afshin' would be great and I'll use the same email that I use to send these messages.  Thanks again! 

In the mean time I'll be casually searching for ways to make this script neater and hopefully connect to bluetooth devices via the command line just like the blueutil utility.  

--
Afshin Haghgoo

Patrick Robertson

unread,
Jul 7, 2013, 11:07:32 PM7/7/13
to blacktree-...@googlegroups.com
Done!

It's good to see members of the QS community contributing back, and it's always good to share these contributions.
Feel free to make any improvements/changes to the original script at http://qsapp.com/wiki/Toggle_Bluetooth_(AppleScript) as well

Cheers

1.61803

unread,
Jul 8, 2013, 9:18:28 AM7/8/13
to blacktree-...@googlegroups.com, Afshin Haghgoo
On Monday, July 8, 2013 4:42:56 AM UTC+2, Afshin wrote:
In the mean time I'll be casually searching for ways to make this script neater and hopefully connect to bluetooth devices via the command line just like the blueutil utility.

Check this one — you don't need blueutil but MouseTools or Cliclick to move (or move and click) and the bluetooth menubar item enabled.
It saves the mouse position, does the clicking to turn bt on and connect OR turn bt off and puts your cursor back to where it was.

tell application "System Events" to tell process "SystemUIServer"

set {x, y} to paragraphs of (do shell script "MouseTools -location")

tell (first menu bar item of menu bar 1 whose value of attribute "AXDescription" is "bluetooth")

click

if exists menu item "Bluetooth: Off" of first menu then

click menu item "Turn Bluetooth On" of first menu

delay 0.5

click

set {m, n} to position of menu item "MyPhone" of first menu

do shell script "MouseTools -x " & (m + 10) & " -y " & (n + 10)

delay 0.1

do shell script "MouseTools -x " & (m - 10) & " -y " & (n + 10)

click menu item "Connect to Network" of first menu of menu item "db" of first menu

--do shell script "MouseTools -x " & (m - 10) & " -y " & (n + 10) & " -leftClick"

else

click menu item "Turn Bluetooth Off" of first menu

end if

end tell

do shell script "MouseTools -x " & x & " -y " & y

end tell


Just change MyPhone to the name of your device. This is GUI scripting so tweak the delays as needed. Clicking certain menus with MouseTools is somewhat unreliable for me — that's why I used click instead. I probably could have nested the first menu if I got clicking the parent element working. Maybe an AS guru chimes in.
Reply all
Reply to author
Forward
0 new messages