Bogdan
Bogdan

Reputation: 15

Script for adding new KeyboardShortcuts on MacOs (Leopard)

Is it possible to add KeyboardShortcuts in MacOs (Leopard) using shell or other programmatic way? Basically, something to automate the steps of opening Keyboard&Mouse in SystemPreferences, selecting the last tab "KeyboardShortcuts", Clicking "+" to add a new one and filling the info. Thank you

Upvotes: 1

Views: 104

Answers (1)

William Niu
William Niu

Reputation: 15853

The following AppleScript should do the trick, with 3 variables:

  1. app_name: name of an application that you want to assign the shortcut to, e.g. Safari
  2. menu_title: exact menu name to execute
  3. keystrokes: the actual shortcut

tell application "System Preferences"
    activate
    set current pane to pane "com.apple.preference.keyboard"
end tell

tell application "System Events"
    tell process "System Preferences"
        tell window "Keyboard"
            click button 3 of tab group 1
            tell sheet 1
                click pop up button 1
                click last menu item of menu 1 of pop up button 1
                keystroke "/Applications/" & app_name & ".app"
                keystroke return
                keystroke return
                delay 1

                keystroke menu_title
                keystroke tab

                keystroke last item of keystokes using rest of reverse of keystokes
                delay 1

                click button "Add"
            end tell
        end tell
    end tell
end tell

The code is referencing the following site: http://www.rngtng.com/2010/10/29/applescript-to-create-keyboard-shortcuts/

Upvotes: 2

Related Questions