Skip to content

Instantly share code, notes, and snippets.

@pryorda
Last active December 17, 2019 22:42
Show Gist options
  • Save pryorda/c68b69b95244afc3c5487a047a2efa8a to your computer and use it in GitHub Desktop.
Save pryorda/c68b69b95244afc3c5487a047a2efa8a to your computer and use it in GitHub Desktop.
Creating KeyBoard Shortcuts that work on MAC

This is "the" process to create keyboard shortcuts from applescripts on MAC OS X

  1. Open Automator
  2. Click Service if the menu opens, otherwise File -> New -> Quick Action
  3. Set "Workflow receives" to "no input"
  4. In the left panel, click utilities -> Run AppleScript -> Copy and Paste one of the applescript from one of the attached files.
  5. Click File -> Save. Take note of what you saved the service as
  6. Go to System Preferences -> Keyboard -> Shortcuts -> Services and scroll all the way to the bottom. Your service should be here and you can set the key sequence.
  7. Open some application and test if the sequence works.
on getFrontAppPath()
set frontAppPath to (path to frontmost application) as text
set myPath to (path to me) as text
if frontAppPath is myPath then
try
tell application "Finder" to set bundleID to id of file myPath
tell application "System Events" to set visible of (first process whose bundle identifier is bundleID) to false
-- we need to delay because it takes time for the process to hide
-- I noticed this when running the code as an application from the applescript menu bar item
set inTime to current date
repeat
set frontAppPath to (path to frontmost application) as text
if frontAppPath is not myPath then exit repeat
if (current date) - inTime is greater than 1 then exit repeat
end repeat
end try
end if
return frontAppPath
end getFrontAppPath
tell application "Terminal"
set frontAppPath to my getFrontAppPath()
create window "/usr/local/bin/lpass show --sync=now --clip -q --password AD"
activate
delay 0.2
tell application frontAppPath to activate
end tell
set myScript to "/usr/local/bin/lpass show --sync=now --clip -q --password AD; exit"
tell application "Terminal"
activate
do script myScript
end tell
set isBusy to true
repeat until isBusy is false
tell application "Terminal"
tell window 1
set isBusy to busy as boolean --> Test if busy
end tell
end tell
delay 1 --> Check every second
end repeat
tell me to activate
# display dialog "Terminal is No Longer Busy!"
my displayDialog()
on displayDialog()
display notification "Password Copied" with title "LastPass CLI" sound name "Submarine"
end displayDialog
if application "iTerm" is running then
tell application "iTerm"
create window with default profile
activate
end tell
else
tell application "iTerm"
activate
end tell
end if
if application "Terminal" is running then
tell application "Terminal"
activate
end tell
else
tell application "Terminal"
do script ""
activate
end tell
end if
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment