Saturday, October 21, 2017

#1 2017-08-09 03:54:48 pm

Joy
Member
From:: South Tirol
Registered: 2008-07-04
Posts: 510
Website

Get Bluetooth devices

big_smile
Reading plists is a difficult task, i did it only twice.
How to get "device_title" from:

Applescript:

do shell script "system_profiler -xml SPBluetoothDataType"

I need to parse the registered devices, preferred method using System events, would be easier for me to learn how to do it myself next time... Thanks

Last edited by Joy (2017-08-09 03:57:13 pm)

Offline

 

#2 2017-08-09 06:45:56 pm

Shane Stanley
Member
From:: Australia
Registered: 2002-12-07
Posts: 5158

Re: Get Bluetooth devices

This will give you an AppleScript record you can then parse normally:

Applescript:

use AppleScript version "2.4" -- Yosemite (10.10) or later
use framework "Foundation"
use scripting additions

set theText to (do shell script "system_profiler -xml SPBluetoothDataType")
set theText to current application's NSString's stringWithString:theText
set theData to theText's dataUsingEncoding:(current application's NSUTF8StringEncoding)
-- convert to Cocoa object
set {theThing, theError} to current application's NSPropertyListSerialization's propertyListWithData:theData options:0 |format|:(missing value) |error|:(reference)
if theThing is missing value then error (theError's localizedDescription() as text) number -10000
-- we don't know the class of theThing for coercion, so...
set listOfThing to current application's NSArray's arrayWithObject:theThing
set theRecord to item 1 of (theThing as list)


Shane Stanley <sstanley@myriad-com.com.au>
www.macosxautomation.com/applescript/apps/

Online

 

#3 2017-08-09 08:28:40 pm

Shane Stanley
Member
From:: Australia
Registered: 2002-12-07
Posts: 5158

Re: Get Bluetooth devices

Also, if all you want is the name of Bluetooth devices, there's no need to go to all that effort:

Applescript:

use AppleScript version "2.4" -- Yosemite (10.10) or later
use framework "Foundation"
use framework "IOBluetooth"
use scripting additions

set allNames to ((current application's IOBluetoothDevice's recentDevices:0)'s valueForKey:"nameOrAddress") as list
set pairedNames to ((current application's IOBluetoothDevice's pairedDevices())'s valueForKey:"nameOrAddress") as list


Shane Stanley <sstanley@myriad-com.com.au>
www.macosxautomation.com/applescript/apps/

Online

 

#4 2017-08-10 05:24:02 am

Joy
Member
From:: South Tirol
Registered: 2008-07-04
Posts: 510
Website

Re: Get Bluetooth devices

Wow, splendid, Shane!
So far so good, thereafter i tried to complete my script by adding :

Applescript:

tell application "Finder" to set sel to item 1 of (get selection) as text
#set sel to quoted form of POSIX path of sel
#do shell script "open -a 'Bluetooth File Exchange' " & sel
tell application "Bluetooth File Exchange"
   send file sel to device "Redmi"
end tell

If i use the simple shell script i can submit, but have to choose my device, the applescript support for  "Bluetooth file exchange" seems broken... (At least on 10.10.5 )

Offline

 

#5 2017-08-10 06:26:47 am

Shane Stanley
Member
From:: Australia
Registered: 2002-12-07
Posts: 5158

Re: Get Bluetooth devices

Bluetooth File Exchange's dictionary says says "send file list of file". That suggests something more like:

Applescript:

tell application "Finder" to set sel to item 1 of (get selection) as «class furl»
tell application "Bluetooth File Exchange"
   send file {sel} to device "Redmi"
end tell


Shane Stanley <sstanley@myriad-com.com.au>
www.macosxautomation.com/applescript/apps/

Online

 

#6 2017-08-10 06:57:58 am

Nigel Garvey
Moderator
From:: Warwickshire, England
Registered: 2002-11-20
Posts: 4412

Re: Get Bluetooth devices

Shane Stanley wrote:

Bluetooth File Exchange's dictionary says says "send file list of file".


With version 4.4.6 in El Capitan, and presumably on Joy's Yosemite system, it's:

send
   [file file] : The file to send.
   [to device text] : The device to send the file to.



But it could indeed be worth trying a file specifier or a list thereof.

That's another piece of wince-inducing Apple dictionary English to add to my collection.  wink


NG

Offline

 

#7 2017-08-10 07:19:33 am

Shane Stanley
Member
From:: Australia
Registered: 2002-12-07
Posts: 5158

Re: Get Bluetooth devices

Nigel Garvey wrote:

With version 4.4.6 in El Capitan, and presumably on Joy's Yosemite system, it's:

send
   [file file] : The file to send.
   [to device text] : The device to send the file to.




Interesting. It says the same thing in Script Editor here too (v5.0.5) now that I check, but is says list of file in Script Debugger. So I had a look in the sdef, and it says:

Applescript:

<parameter name="file" code="btFs" type="file" optional="yes" list="yes" description="The file to send.">

So I'm not sure why SE is not indicating a list. Not that the sdef proves anything in terms of actual behavior.


Shane Stanley <sstanley@myriad-com.com.au>
www.macosxautomation.com/applescript/apps/

Online

 

#8 2017-08-10 07:47:51 am

Shane Stanley
Member
From:: Australia
Registered: 2002-12-07
Posts: 5158

Re: Get Bluetooth devices

Shane Stanley wrote:

So I'm not sure why SE is not indicating a list.



I checked the rules for sdef. The list attribute only applies to type elements -- parameters don't have a list attribute. So technically the sdef entry makes no sense.

Script Debugger is assuming that the existence of the list attribute means the author intends the parameter to be a list. Who knows the reality.


Shane Stanley <sstanley@myriad-com.com.au>
www.macosxautomation.com/applescript/apps/

Online

 

#9 2017-08-10 09:22:49 am

Joy
Member
From:: South Tirol
Registered: 2008-07-04
Posts: 510
Website

Re: Get Bluetooth devices

Nigel Garvey wrote:

With version 4.4.6 in El Capitan, and presumably on Joy's Yosemite system, it's:

send
   [file file] : The file to send.
   [to device text] : The device to send the file to.


I confirm. Tried out lists or single items using hfs or posix path, nothing worked. The script dict of "Bluetooth File Exchange" is a fake.
At this point, i prefer to ignore applescript and use shell instead

Applescript:

do shell script "open -a 'Bluetooth File Exchange' "& sel

..whereas sel has to be a list or single item in quoted posix path format...

Thanks you both for your insights!

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)