Typing a keyboard shortcut using rb-appscript

360 Views Asked by At

I'm trying to convert the following script to Ruby using rb-appscript:

-- Runs the keyboard shortcut for the provided application name.
-- applicationName - The name of the application to run the keyboard shortcut on.
-- key - The key to press.  For example, this could be "n" or "q".
-- modifiersList - A list of modifiers for the keyboard shortcut.  This could be something like
-- { command down } or { command down, shift down, option down }.
on runKeyboardShortcut(applicationName, key, modifiersList)
    tell application applicationName to activate
    tell application "System Events"
      keystroke key using modifiersList
    end tell
end runKeyboardShortcut

Here's what I have so far:

def run_keyboard_shortcut(application_name, key, modifiers_list)
    Appscript.app.by_name(application_name).activate
    Appscript.app.by_name("System Events").keystroke(key)
end

How do I add the modifiers to the keystroke command?

1

There are 1 best solutions below

0
On BEST ANSWER

The solution is to do this:

def run_keyboard_shortcut(application_name, key, modifiers)
    Appscript.app.by_name(application_name).activate
    Appscript.app.by_name("System Events").keystroke(key, :using => modifiers)
end

run_keyboard_shortcut("Finder", "n", [ :command_down ])