Code
#!/usr/bin/env osascript -l JavaScript
const config = {
app_name: "Sublime Text",
start_delay_ms: 0,
end_delay_ms: 0,
snippets: [
{ keys: `the `},
{ keys: `quick`, shift: true },
{ keys: ` brown fox`},
{ code: 76 },
{ keys: `jumps over the lzay dog` },
{ pause: 700 },
{ code: 123, option: true },
{ code: 123, shift: true, option: true },
{ pause: 100 },
{ keys: `lazy `},
{ code: 124, command: true },
{ code: 76 },
]
}
function get_current_app() {
return Application(Application("System Events").processes.whose(
{ frontmost: { '=': true } })[0].name())
}
function get_target_app(config) {
return Application(config.app_name)
}
function output_char(config, command_, option_, shift_) {
sleepchar()
const using = []
if (command_) { using.push("command down") }
if (shift_) { using.push("shift down") }
if (option_) { using.push("option down") }
config.target_app.activate()
config.sys.keystroke(config.char, { using: using })
}
function output_code(config, command_, option_, shift_) {
const using = []
if (command_) { using.push("command down") }
if (shift_) { using.push("shift down") }
if (option_) { using.push("option down") }
config.target_app.activate()
config.sys.keyCode(config.code, { using: using })
}
function output_keys(config) {
sleep(config.start_delay_ms)
config.keep_going = true
config.snippets.forEach((snippet) => {
sleepblock()
if (snippet.keys) {
snippet.keys.split("").forEach((char) => {
config.char = char
output_char(config, snippet.command, snippet.option, snippet.shift)
})
} else if (snippet.code) {
config.code = snippet.code
output_code(config, snippet.command, snippet.option, snippet.shift)
} else if (snippet.pause) {
sleep(snippet.pause)
}
})
sleep(config.end_delay_ms)
}
function type_stuff(config) {
config.sys = Application('System Events')
config.driver_app = get_current_app()
config.target_app = get_target_app(config)
output_keys(config)
config.driver_app.activate()
}
function sleepblock() {
sleep(Math.floor(Math.random() * 30) + 100)
}
function sleepchar() {
sleep(Math.floor(Math.random() * 40) + 20)
}
function sleep(milliseconds) {
const date = Date.now();
let currentDate = null;
do {
currentDate = Date.now();
} while (currentDate - date < milliseconds);
}
type_stuff(config)