Skip to content

Instantly share code, notes, and snippets.

@rngtng
Created October 28, 2010 18:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save rngtng/652031 to your computer and use it in GitHub Desktop.
Save rngtng/652031 to your computer and use it in GitHub Desktop.
AppleScript to create KeyboardShortcuts easily
----------------------------------------------------------------------------------------------------
--
-- Script to automate creation of Keyborad shortcuts
--
----------------------------------------------------------------------------------------------------
GUIScripting_status()
tell application "System Preferences"
activate
reveal anchor "shortcutsTab" of pane id "com.apple.preference.keyboard"
end tell
set {leftArrow, rightArrow} to {ASCII character 28, ASCII character 29}
set {upArrow, downArrow} to {ASCII character 30, ASCII character 31}
-------------------------------- DEFINE YOUR SHORTCUTS HERE --------------------------------
createShortcut("Safari", "Select Next Tab", {control down, rightArrow})
createShortcut("Safari", "Select Previous Tab", {control down, leftArrow})
createShortcut("Utilities/Terminal", "Select Next Tab", {control down, rightArrow})
createShortcut("Utilities/Terminal", "Select Previous Tab", {control down, leftArrow})
createShortcut("TextMate", "Next File Tab", {control down, rightArrow})
createShortcut("TextMate", "Previous File Tab", {control down, leftArrow})
createShortcut("TextMate", "Search Project With AckMate...", {command down, "F"})
----------------------------------------------------------------------------------------------------
tell application "System Preferences" to quit
----------------------------------------------------------------------------------------------------
----------------------------------------------------------------------------------------------------
on createShortcut(app_path, menu_text, keystokes)
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_path & ".app"
keystroke return
keystroke return
delay 1
keystroke menu_text
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
end createShortcut
#source: http://www.macosxautomation.com/applescript/uiscripting/index.html
on GUIScripting_status()
-- check to see if assistive devices is enabled
tell application "System Events"
set UI_enabled to UI elements enabled
end tell
if UI_enabled is false then
set result to display dialog "This script utilizes the built-in Graphic User Interface Scripting architecture of Mac OS x which is currently disabled." & return & return & "Please click 'OK' to provide your password to activate GUI Scripting" with icon 1 buttons {"OK", "Enable without password"} default button 1
if button returned of result = "Enable without password" then
tell application "System Preferences"
activate
set current pane to pane id "com.apple.preference.universalaccess"
display dialog "Activate GUI Scripting by selecting the checkbox \"Enable access for assistive devices\" in the Universal Access preference pane." with icon 1 buttons {"OK"} default button 1
delay 3
end tell
else
enabledGUIScripting(true)
end if
end if
end GUIScripting_status
#source: http://www.peachpit.com/blogs/blog.aspx?uk=Using-AppleScript-to-enable-GUI-Scripting-Five-AppleScript-Tips-in-Five-Days
on enabledGUIScripting(switch)
tell application "System Events"
activate
set UI elements enabled to switch
return UI elements enabled
end tell
end enabledGUIScripting
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment