LoginSignup
0
2

More than 5 years have passed since last update.

スリープ解除時にBluetoothテザリング自動接続する最新版

Posted at

maxOS Sierra 10.12.6 前提。
記録用。雑。

  • sleepwatcher
    • スリープ解除時に自動実行
  • bash
    • sleepwatcherから実行
    • 利用可能なWi-Fiネットワークを判定、なければBluetoothテザリングに接続
  • Automator
    • GUIスクリプティングでBluetoothテザリングに接続

sleepwatcher

~/Library/LaunchAgents/de.bernhard-baehr.sleepwatcher-20compatibility-localuser.plist
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    <key>Label</key>
    <string>de.bernhard-baehr.sleepwatcher</string>
    <key>ProgramArguments</key>
    <array>
        <string>/usr/local/sbin/sleepwatcher</string>
        <string>-V</string>
        <!-- <string>-s /Users/[ユーザ名]/Documents/scripts/sleepwatcher/before_sleep</string> -->
    <string>-w ~/bin/bash/afterwake.sh</string>
    </array>
    <key>RunAtLoad</key>
    <true/>
    <key>KeepAlive</key>
    <true/>
</dict>
</plist>

bash

sleepwatcherで実行する。

afterwake.sh
#!/bin/bash
# automatic connect to Bluetooth tethering
cd "$(dirname $0)"
osascript -e 'display notification "Run" with title "afterwake.sh"'
./networkautoconnect.sh
networkautoconnect.sh
#!/bin/bash
# @(#) Automatic connect to wireless network.
# @(#) If prefered Wi-Fi network is available, wait to connect.
# @(#) Else, or can't connect to prefered Wi-Fi network, connect to Bluetooth tethering.
# @(#) Usage: $0

#
# Initialize variables / functions
#
readonly SCRIPTNAME="$(basename "$0")"
Notice () { # if run in terminal, echo error message
  if tty >& /dev/null;then
    echo "$SCRIPTNAME:" "$*" 1>&2
  else
    osascript -e "tell application \"System Events\" to display notification \"$*\" with title \"$SCRIPTNAME\""
  fi
}

#
# Process
#

# Wi-Fi / Bluetooth on
networksetup -setairportpower en0 on
blueutil on >& /dev/null

# If run without terminal (=afterwake), wait few seconds.
if ! tty >& /dev/null;then
  sleep 5
fi
# if no prefered Wi-Fi network, connect bluetooth tethering
if ! /System/Library/PrivateFrameworks/Apple80211.framework/Versions/Current/Resources/airport -s | sed 1d | grep -q $(networksetup -listpreferredwirelessnetworks en0 | sed -e 1d -e 's/^[:blanc:]*//' | xargs -n1 echo -n " -e");then
  Notice "No Prefered Wi-Fi network."
  open ~/bin/Automator/btconnect.app
else
  Notice "Prefered Wi-Fi network is available. Wait to connect."
  # Wait to check connection to prefered Wi-Fi network.
  if ! tty >& /dev/null;then
    sleep 60
  fi
  if ! networksetup -getinfo 'Wi-Fi' | grep -q 'IP address:'; then
    Notice "Can't connect to prefered Wi-Fi network, then connect to Bluetooth tethering."
    open ~/bin/Automator/btconnect.app
  fi
fi

Automator

macOSでは、コンピュータの制御を許可したアプリケーションからしかGUIスクリプティングを実行できない。
プレーンテキストのシェルスクリプトはアプリケーションではないので、ターミナルやスクリプトエディタから実行するか、Automatorでアプリケーション化する必要がある。
今回は、Bluetoothテザリングに接続する処理だけアプリケーション化して実行する。

スクリーンショット

osascript -l JavaScript -e 'IPHONE = "mmiPhone";se = Application("System Events");ui = se.processes["SystemUIServer"];bt = ui.menuBars[0].menuBarItems().find(function(m){return m.description() == "bluetooth"});bt.click();iphone = bt.menus[0].menuItems[IPHONE]; iphone.click();iphone.menus[0].menuItems["ネットワークへ接続"].click();'

mmiPhoneのとこはiPhoneのデバイス名に合わせて。

0
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
2