概要
- シェルスクリプトとして利用
- 引数として
off
が与えられた場合のみ接続解除
- 引数として
- blueutilでBluetoothを有効化
-
networksetup -getinfo
で接続状況を確認- 既に接続しているのに接続しようとする、または未接続なのに解除しようとする場合、処理を実行しない
- GUIスクリプティングでMenuExtraをクリックし、接続をOn/Off
利用方法
$ osascript ~/bin/osascript/btconnect.scpt
$ osascript ~/bin/osascript/btconnect.scpt off
コード
btconnect.scpt
------------------------------------------
-- [Description]
-- Activate bluetooth with 'blueutil'.
-- Connect or disconnect bluetooth tethering of iPhone.
-- Reffer : http://d.hatena.ne.jp/zariganitosh/20100119/1263886959
------------------------------------------
property DeviceName : "MyiPhone" -- 親機で設定したデバイス名を指定
-- Process
on run argv
set MenuOpt to "ネットワークへ接続"
try
if (first item of argv) is "off" then set MenuOpt to "ネットワークから接続解除"
end try
-- return result
do shell script "/usr/local/bin/blueutil on"
repeat
if (do shell script "/usr/local/bin/blueutil status | cut -d' ' -f2") is "on" then exit
delay 0.2
end repeat
if (do shell script "networksetup -getinfo 'Bluetooth PAN' | grep -q 'Ethernet Address: (null)'; echo $?") is "0" then
set Porpose to "ネットワークへ接続"
else
set Porpose to "ネットワークから接続解除"
end if
if MenuOpt = Porpose then click_menu_extra("bluetooth/" & DeviceName & "/" & MenuOpt)
end run
-- Hundler
on split(sourceText, separator)
if sourceText = "" then return {}
set oldDelimiters to AppleScript's text item delimiters
set AppleScript's text item delimiters to {separator}
set theList to text items of sourceText
set AppleScript's text item delimiters to oldDelimiters
return theList
end split
on number_from(str)
try
str as number
on error
str
end try
end number_from
on click_menu_extra(menu_path)
if menu_path is "" then
error "menu_path が入力されていません。"
end if
set mp to split(menu_path, "/")
tell application "System Events"
tell process "SystemUIServer"
set frontmost to true
click (menu bar 1's first menu bar item whose attribute "AXDescription"'s value is (mp's item 1))
repeat with i from 2 to mp's length
click (result's menu 1's menu item (my number_from(mp's item i)))
end repeat
delay 0.1 --連続してメニューを操作する時、ひと呼吸必要
end tell
end tell
end click_menu_extra