Created
November 13, 2018 10:02
-
-
Save littlebusters/5e7a4ec46f2899a0d913b0cf7e868d60 to your computer and use it in GitHub Desktop.
システム環境設定にあるキーボードショートカットを設定するJXA ref: https://qiita.com/littlebusters/items/7833947cf97962f90e89
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[ | |
{ | |
"appName": "Sketch.app", | |
"shortcut": [ | |
{ | |
"menu": "Flatten Selection to Bitmap", | |
"key": "e", | |
"modifier": [ "command", "control", "shift" ] | |
} | |
] | |
} | |
] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"use strict"; | |
var app = Application.currentApplication(); | |
app.includeStandardAdditions = true; | |
// ローカライズ | |
var langList = app.doShellScript("defaults read NSGlobalDomain AppleLanguages | awk '{gsub(/[^a-zA-Z-]/,\"\");print}' | grep -v '^$'"); | |
var langs = langList.split('\r'); | |
var chooseJSONLabel = 'Please select a JSON file:'; | |
var shortcutLabel = 'App Shortcuts'; | |
var menuItemLabel = 'Menu Title:'; | |
var addButtonLabel = 'Add'; | |
switch (langs[0].substr(0, 2)) { | |
case 'ja': | |
chooseJSONLabel = 'JSONファイルを選択してください。'; | |
shortcutLabel = 'アプリケーション'; | |
menuItemLabel = 'メニュータイトル:'; | |
addButtonLabel = '追加'; | |
break; | |
} | |
var appList = app.doShellScript('ls /Applications').split('\r'); | |
// 設定ファイルを読み込んでJSONへパース | |
var chooseFile = app.chooseFile({ | |
withPrompt: chooseJSONLabel | |
}) | |
var json = JSON.parse(readFile(chooseFile)); | |
// 環境設定を起動してキーボードを選択し、ショートカットタブを表示する | |
var pref = Application('com.apple.systempreferences'); | |
pref.panes['com.apple.preference.keyboard'].anchors['shortcutsTab'].reveal(); | |
pref.activate(); | |
delay(2); | |
// 左のリストから「アプリケーション」を選択 | |
var sysEvent = Application('System Events'); | |
var prefKeyboard = sysEvent.processes['System Preferences'].windows[0]; | |
var rows = prefKeyboard.tabGroups[0].splitterGroups[0].scrollAreas[0].tables[0].rows(); | |
var shortcutPosition = 0; | |
for (let i = 0; i < rows.length; i++) { | |
console.log(rows[i].staticTexts[0].name()); | |
if (shortcutLabel === rows[i].staticTexts[0].name()) { | |
shortcutPosition = i; | |
break; | |
} | |
} | |
rows[shortcutPosition].select(); | |
// ショートカットを設定 | |
for (let i = 0; i < json.length; i++) { | |
let appName = json[i].appName; | |
let appExist = false; | |
appList.some(function (obj) { | |
if (appName === obj) { | |
appExist = true; | |
return true; | |
} | |
}); | |
let appSelected = false; | |
for (let j = 0; j < json[i].shortcut.length; j++) { | |
// [+]ボタンをクリックしてダイアログシートを表示 | |
prefKeyboard.tabGroups[0].groups[0].buttons[0].click(); | |
let settingSheet = prefKeyboard.sheets[0]; | |
// アプリケーションを設定 | |
if (!appSelected) { | |
if (appExist) { | |
settingSheet.popUpButtons[0].click().menus[0].menuItems[appName].click(); | |
} else { | |
// ポップアップの中にアプリがない場合 | |
let appListItems = settingSheet.popUpButtons[0].click().menus[0].menuItems.length; | |
settingSheet.popUpButtons[0].menus[0].menuItems[appListItems - 1].click(); | |
// アプリが選択されるまで待つ | |
while (!appExist) { | |
if(appName == settingSheet.popUpButtons[0].value()) { | |
appExist = true; | |
} else { | |
delay(2); | |
} | |
} | |
} | |
appSelected = true; | |
} | |
// メニュー項目を設定 | |
settingSheet.textFields[menuItemLabel].value = json[i].shortcut[j].menu; | |
// タブキーでフォーカスを移動 | |
sysEvent.keyCode(48); | |
// ショートカットキーを設定 | |
let pressKey; | |
switch (json[i].shortcut[j].key) { | |
case 'left': | |
pressKey = 123; | |
break; | |
case 'right': | |
pressKey = 124; | |
break; | |
case 'up': | |
pressKey = 125; | |
break; | |
case 'down': | |
pressKey = 125; | |
break; | |
default: | |
pressKey = json[i].shortcut[j].key | |
break; | |
} | |
setShortcut(pressKey, json[i].shortcut[j].modifier); | |
// 追加ボタンをクリック | |
settingSheet.buttons[addButtonLabel].click(); | |
} | |
appSelected = false; | |
} | |
// Helpers | |
function readFile(file) { | |
var fileString = file.toString(); | |
return app.read(Path(fileString)); | |
} | |
function setShortcut(key, mod) { | |
for (var i = 0; i < mod.length; i++) { | |
mod[i] = mod[i] + ' down'; | |
} | |
if (isNaN(key)) { | |
sysEvent.keystroke(key, {using: mod}); | |
} else { | |
sysEvent.keyCode(key, {using: mod}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment