Skip to content

Instantly share code, notes, and snippets.

@ycatch

ycatch/key_macro.js

Forked from anonymous/key_macro.js
Last active Jun 14, 2017
Embed
What would you like to do?
Keyboard macro for Mac OS X by Javascript / Applescript > http://www.catch.jp/wiki/?keymacro4Mac
/* Copyright 2016 Yutaka Kachi
Easy keyboard macro for Mac OS X
NYSL - 煮るなり焼くなり好きにしろライセンス
入力するテキストは、code_linesに、配列として直接書く。
| 日本語変換ON/OFF
日本語変換中に利用できるキー
_ 変換する
$ 下向き矢印
> 右向き矢印
% delete
¥ TAB
= wait
^ 変換を確定する
*/
var code_lines = [
'/* hello world */',
'/* |konnnitiha^=sayounara^| world */',
'print("hello world")']
var browser = "Google Chrome"; // Set your browser "Safari"
var kotorie = false;
var key_delay = 0.04;
// activate browser
var app = Application(browser);
app.includeStandardAdditions = true;
app.activate();
delay(1);
var sys = Application("System Events");
// lines Loop
for (var j = 0, len = code_lines.length; j < len; j++) {
var code_char = code_lines[j].split("");
// Characters Loop
for (var i = 0, len2 = code_char.length; i < len2; i++) {
delay(key_delay);
if (code_char[i] == " ") {
// for disable kotorie
sys.keyCode(49); // スペースキー
delay(0.1);
} else if (code_char[i] == "|") {
// ことりえON/OFF
if (kotorie == true) {
kotorie = false;
sys.keyCode(102); //英数キー
} else {
kotorie = true;
sys.keyCode(104); //かなキー
}
} else if (code_char[i] == "_") {
if (kotorie == true) {
sys.keyCode(49); //スペースキー
} else {
sys.keystroke(code_char[i]);
}
} else if (code_char[i] == "$") {
if (kotorie == true) {
sys.keyCode(125); //下向き矢印キー、日本語変換の候補選択
} else {
sys.keystroke(code_char[i]);
}
} else if (code_char[i] == ">") {
if (kotorie == true) {
sys.keyCode(124); //右向き矢印キー
} else {
sys.keystroke(code_char[i]);
}
} else if (code_char[i] == "%") {
if (kotorie == true) {
sys.keyCode(51); //deleteキー
} else {
sys.keystroke(code_char[i]);
}
} else if (code_char[i] == "¥") {
if (kotorie == true) {
sys.keyCode(48); //TABキー
} else {
sys.keystroke(code_char[i]);
}
} else if (code_char[i] == "=") {
if (kotorie == true) {
delay(0.5); //ウェイト
} else {
sys.keystroke(code_char[i]);
}
} else if (code_char[i] == ",") {
// ことりえ変換
if (kotorie == true) {
sys.keyCode(43); //全角の「、」
} else {
sys.keystroke(code_char[i]);
}
} else if (code_char[i] == ".") {
// ことりえ変換
if (kotorie == true) {
sys.keyCode(47); //全角の「。」
} else {
sys.keystroke(code_char[i]);
}
} else if (code_char[i] == "^") {
// ことりえ確定
if (kotorie == true) {
delay(0.2);
sys.keyCode(76); //Enterキー
delay(0.3);
} else {
sys.keystroke(code_char[i]);
}
} else {
sys.keystroke(code_char[i]);
}
}
delay(0.5);
if (j < len -1) {
sys.keyCode(76); // Enterキー
delay(0.8);
}
}
@ycatch

This comment has been minimized.

Copy link
Owner Author

@ycatch ycatch commented Jan 26, 2016

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment