Tuesday, October 25, 2016

#1 2007-08-17 11:33:12 am

lunixninja
Member
From: Cork, Ireland
Registered: 2007-03-19
Posts: 16
Website

Running a App if a phone is found in Bluetooth

Hey,  lunixninja here.
I want to know how to do something with Applescript and bluetooth. Im still just a student so, it would be great if someone could help me.
If Applescript finders a bluetooth phone/device with the name "12345" , It will run a program

Heres the script to turn on Bluetooth if needed:

Applescript:




-- Check the current bluetooth status and turn it on if necessary.
tell application "System Events" to tell the front menu bar of process "SystemUIServer"
   set menu_extras to value of attribute "AXDescription" of menu bar items
   repeat with x from 1 to the length of menu_extras
       if item x of menu_extras is "bluetooth menu extra" then exit repeat
   end repeat
   tell menu bar item x
       click
       tell 2nd menu item of front menu
           if name ends with "Off" then
               -- Current status is on; it says "turn bluetooth off"
               set last_bluetooth_setting to "on"
           else if name ends with "On" then
               -- Current status is off; it says "turn bluetooth on"
               set last_bluetooth_setting to "off"
               click
           end if
       end tell
       -- Unclick if it was on (do nothing but make the menu disappear)
       if last_bluetooth_setting is "on" then
           click
       end if
   end tell
end tell


Thanks very much
lunixninja

Last edited by lunixninja (2007-08-17 11:58:14 am)


Filed under: System

Offline

 

#2 2007-08-17 12:11:30 pm

StefanK
Member
From: St. Gallen, Switzerland
Registered: 2006-10-21
Posts: 11340
Website

Re: Running a App if a phone is found in Bluetooth

Hi,

as far as I know this is not possible with AppleScript,
because there is no way to detect in the background if a device is connected


regards

Stefan

Offline

 

#3 2007-08-17 12:18:59 pm

lunixninja
Member
From: Cork, Ireland
Registered: 2007-03-19
Posts: 16
Website

Re: Running a App if a phone is found in Bluetooth

Surely you could use maybe like iSync to do what I want to do,
like this:
   1. Turn on Bluetooth if necessary.
   2. Run iSync.
   3. Wait for iSync to finish, then quit it.
   4. Turn Bluetooth off if it was initially off.

Applescript:

-- Before you use this script, launch /Applications/AppleScript/AppleScript Utility.app and check "Enable GUI Scripting."
-- The Bluetooth menu item must be turned on in the Bluetooth system preferences pane.

-- Check the current bluetooth status and turn it on if necessary.
tell application "System Events" to tell the front menu bar of process "SystemUIServer"
   set menu_extras to value of attribute "AXDescription" of menu bar items
   repeat with x from 1 to the length of menu_extras
       if item x of menu_extras is "bluetooth menu extra" then exit repeat
   end repeat
   tell menu bar item x
       click
       tell 2nd menu item of front menu
           if name ends with "Off" then
               -- Current status is on; it says "turn bluetooth off"
               set last_bluetooth_setting to "on"
           else if name ends with "On" then
               -- Current status is off; it says "turn bluetooth on"
               set last_bluetooth_setting to "off"
               click
           end if
       end tell
       -- Unclick if it was on (do nothing but make the menu disappear)
       if last_bluetooth_setting is "on" then
           click
       end if
   end tell
end tell

-- Do the sync and wait for it to finish
tell application "iSync"
   synchronize
   repeat while (syncing is true)
       delay 5
   end repeat
   set syncStatus to sync status
   
   if syncStatus = 2 then
       -- Success
       quit
   else
       if syncStatus = 3 then
           set syncStatus to "completed with warnings"
       else if syncStatus = 4 then
           set syncStatus to "completed with errors"
       else if syncStatus = 5 then
           set syncStatus to "last sync cancelled"
       else if syncStatus = 6 then
           set syncStatus to "last sync failed to complete"
       else if syncStatus = 7 then
           set syncStatus to "never synced"
       end if
       display dialog "syncStatus: " & syncStatus
       syncStatus
   end if
end tell


-- Set the bluetooth status to what it was before.
tell application "System Events" to tell the front menu bar of process "SystemUIServer"
   set menu_extras to value of attribute "AXDescription" of menu bar items
   repeat with x from 1 to the length of menu_extras
       if item x of menu_extras is "bluetooth menu extra" then exit repeat
   end repeat
   tell menu bar item x
       click
       if last_bluetooth_setting is "off" then
           -- Turn it off again
           click 2nd menu item of front menu
       else
           -- Just close the menu
           click
       end if
   end tell
end tell


Filed under: System, ISync

Offline

 

#4 2007-08-17 01:35:28 pm

StefanK
Member
From: St. Gallen, Switzerland
Registered: 2006-10-21
Posts: 11340
Website

Re: Running a App if a phone is found in Bluetooth

I wrote:

as far as I know this is not possible with AppleScript

Update: here is a way to detect if a Bluetooth device is connected

Applescript:

BTdevice_connected("K750i")

on BTdevice_connected(theDevice)
   try
       set theData to do shell script "system_profiler SPBluetoothDataType"
       set {TID, text item delimiters} to {text item delimiters, theDevice}
       set theData to text item 3 of theData
       set text item delimiters to "Connected: "
       set theData to word 1 of text item 2 of theData
       set text item delimiters to TID
       return theData is "Yes"
   on error
       return false
   end try
end BTdevice_connected


regards

Stefan

Offline

 

#5 2007-08-17 01:37:14 pm

mark hunte
Member
From: Hello, London Calling
Registered: 2006-03-20
Posts: 1030
Website

Re: Running a App if a phone is found in Bluetooth

AFAIK,

when you connect with a BT device, it gets added to a favourites list.
Which is always displayed in the result window. Even if the device is not found..


maybe you should look at a app with bluetooth proximity detection.

Like Salling clicker or blue phone elite, which can run AS on detection.

Last edited by mark hunte (2007-08-17 01:44:11 pm)


Tap,tap,tap , hello , testing 1..2,  1...2,
http://www.markosx.com/thecocoaquest/

Offline

 

#6 2007-08-17 05:01:33 pm

Adam Bell
Administrator
From: Nova Scotia, Canada
Registered: 2005-10-04
Posts: 4653

Re: Running a App if a phone is found in Bluetooth


iMac running OS X 10.11.6

Offline

 

Board footer

Powered by FluxBB

[ Generated in 0.043 seconds, 8 queries executed ]

RSS (new topics) RSS (active topics)