LoginSignup
2
2

More than 5 years have passed since last update.

内蔵・外付けキーボード判定でKarabinerを起動・終了させるAppleScript

Last updated at Posted at 2016-05-28

自室用にMajestouchを購入したのを機にSeilとKarabinerを導入したけど、
メイン機がMacBookProなので外で作業するときにいちいち上記のソフトを起動終了するのが面倒になったので、さくっとスクリプトを組んだ。
キーボードの製品IDを予めシステムプロファイラから確認して登録しておくだけで起動と終了をスイッチしてくれるやつです。

kbchecker.scpt
set keyboard_id to "0x4545"

set KB to missing value
try
    set KB to paragraphs of (do shell script "system_profiler SPUSBDataType -detaillevel mini | grep " & keyboard_id)
end try

set pl to missing value
tell application "System Events"
    -- プロセス取得
    set pl to name of every process
end tell

if KB is equal to missing value then
    -- キーボードがない時はkarabinerとseilを終了
    try
        do shell script "pkill Seil"
        do shell script "pkill Karabiner"
    on error
        display alert "起動していない状態なので終了します。"
        return
    end try

    display alert "Quit OK"
    return
else
    tell application "Seil" to run
    tell application "Karabiner" to run

    display alert "Launch OK"
    return
end if

後はアプリケーションとして保存してDockとログイン時リストに登録しておくだけで、内蔵と外付けとでうまいこと切り替えることが出来ます。

追記:
karabiner終了時にそもそも起動していなかった時の例外処理を入れていなかったので入れました。

2
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
2
2