Skip to content

Instantly share code, notes, and snippets.

@IOIO72
Last active September 1, 2020 11:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save IOIO72/d79de6d6c4945f4eae33d0286f3665f2 to your computer and use it in GitHub Desktop.
Save IOIO72/d79de6d6c4945f4eae33d0286f3665f2 to your computer and use it in GitHub Desktop.
Alfred Workflow JavaScript Snippets
// Activate App by name
function run(argv) {
const appname = argv[0];
const app = Application(appname);
app.activate();
}
// Get frontmost app name
function run(argv) {
const system = Application("System Events");
const process = system.processes.whose({ frontmost: {'=': true } })[0];
return `${process.title()}.app`;
}
-- Set this property to true to always open in a new window
property open_in_new_window : false
-- Handlers
on new_window()
tell application "iTerm" to create window with default profile
end new_window
on new_tab()
tell application "iTerm" to tell the first window to create tab with default profile
end new_tab
on call_forward()
tell application "iTerm" to activate
end call_forward
on is_running()
application "iTerm" is running
end is_running
on has_windows()
if not is_running() then return false
if windows of application "iTerm" is {} then return false
true
end has_windows
on send_text(custom_text)
tell application "iTerm" to tell the first window to tell current session to write text custom_text
end send_text
-- Main
on alfred_script(query)
if has_windows() then
if open_in_new_window then
new_window()
else
new_tab()
end if
else
-- If iTerm is not running and we tell it to create a new window, we get two
-- One from opening the application, and the other from the command
if is_running() then
new_window()
else
call_forward()
end if
end if
-- Make sure a window exists before we continue, or the write may fail
repeat until has_windows()
delay 0.01
end repeat
send_text(query)
call_forward()
end alfred_script
// Type Text into App
const sys_events = Application("System Events");
// More keycodes can be added. Keycode reference:
// https://github.com/vitorgalvao/alfred-workflows/tree/master/KeyCodes
const key_codes = {
"→": 124,
"←": 123,
"↑": 126,
"↓": 125,
"⏎": 36, // Return
"⇥": 48, // Tab
"⌫": 51, // Delete
"⎋": 53 // Escape
};
const modifiers = {
"⌘": "command down",
"^": "control down",
"⌥": "option down",
"⇧": "shift down"
};
function press(hotkey) {
const using = [];
while (hotkey.length > 1) {
if (modifiers[hotkey[0]] == undefined) {
throw new Error(hotkey[0] + " is not a recognized modifier key");
};
using.push(modifiers[hotkey[0]]);
hotkey = hotkey.slice(1);
};
if (key_codes[hotkey] != undefined) {
sys_events.keyCode(key_codes[hotkey], {using: using});
} else {
sys_events.keystroke(hotkey.toLowerCase(), {using: using});
};
};
function type(text) {
for (let i=0; i < text.length; i++) {
sys_events.keystroke(text[i]);
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment