@Ken Thomases のソリューションはおそらく最も堅牢ですが、コマンドライン ユーティリティを作成する必要があります。
残念ながら、非 GUI スクリプト シェル スクリプト / AppleScript ソリューションはオプションではありません。現在選択されている入力ソース (キーボード レイアウト) を反映するファイルを更新することは可能ですが、システムは変更を無視します。*.plist
~/Library/Preferences/com.apple.HIToolbox.plist
ただし、次のGUI スクリプト ソリューション(これに基づく) は、まだ目に見えるアクションを含んでいますが、私のマシンでは堅牢でかなり高速です(約 0.2 秒)。
(インストールされているレイアウトを順番に切り替えたいだけの場合は、システム環境設定で定義されているキーボード ショートカットを使用するのがおそらく最善の策です。このソリューションの利点は、特定のレイアウトをターゲットにできることです。)
コメントに記載されている前提条件に注意してください。
# Example call
my switchToInputSource("Spanish")
# Switches to the specified input source (keyboard layout) using GUI scripting.
# Prerequisites:
# - The application running this script must be granted assisistive access.
# - Showing the Input menu in the menu bar must be turned on
# (System Preferences > Keyboard > Input Sources > Show Input menu in menu bar).
# Parameters:
# name ... input source name, as displayed when you open the Input menu from
# the menu bar; e.g.: "U.S."
# Example:
# my switchToInputSource("Spanish")
on switchToInputSource(name)
tell application "System Events" to tell process "SystemUIServer"
tell (menu bar item 1 of menu bar 1 whose description is "text input")
# !! Sadly, we must *visibly* select (open) the text-input menu-bar extra in order to
# !! populate its menu with the available input sources.
select
tell menu 1
# !! Curiously, using just `name` instead of `(get name)` didn't work: 'Access not allowed'.
click (first menu item whose title = (get name))
end tell
end tell
end tell
end switchToInputSource