Skip to content

Instantly share code, notes, and snippets.

@vitorgalvao
Last active March 16, 2023 14:30
Show Gist options
  • Star 28 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save vitorgalvao/6663126 to your computer and use it in GitHub Desktop.
Save vitorgalvao/6663126 to your computer and use it in GitHub Desktop.
Make right-clicking on OSX accessible via a keyboard shortcut
(*
A [native solution][1] exists but it suffers from a major flaw: it right-clicks where the cursor is, not what on the selection.
This code addresses that limitation, though it only works on Finder windows and not the Desktop.
You can install it as a [Finder Service, and later asign it a keyboard shortcut][2].
[1]: http://stackoverflow.com/questions/9171613/right-click-shortcut-for-mac-lion-os
[2]: http://www.macosxautomation.com/services/learn/tut01/index.html
*)
tell application "Finder"
if not frontmost then tell me to error "Finder is not the frontmost app"
set selected_items to the selection
if selected_items is {} then tell me to error "Nothing selected!"
set on_desktop to (insertion location as alias) is (path to desktop folder)
end tell
tell application "System Events" to tell process "Finder"
if on_desktop then
tell group 1 of scroll area 1 to tell (image 1 whose value of attribute "AXFilename" is the name of (item 1 of selected_items)) to perform action "AXShowMenu"
else
tell value of attribute "AXFocusedUIElement" to perform action "AXShowMenu"
end if
end tell
@tonykastaneda
Copy link

I thought I had found a solution for a keystroke to right-click but for some reason, this command will not work in the download folder. Even after a computer restart. Alas, the dream of a no-mouse mac experience shall not be. Awesome work though hopefully, someone will figure this out system-wide.

@vitorgalvao
Copy link
Author

this command will not work in the download folder.

I see no reason why that would be the case (it’s just another Finder directory, it’s not special), neither can I reproduce (it works fine for me).

@tonykastaneda
Copy link

How are you executing the command? I'm using Raycast

@tonykastaneda
Copy link

Wierd it works everywhere else in column view but on in list view in my downloads folder

@vitorgalvao
Copy link
Author

Through Alfred. The Workflow has worked fine for eight years.

@tonykastaneda
Copy link

If anyone is having problems with the script there seems to be a conflict with using the tab view in finder with Raycast (macOS 11.4). I haven't checked if the script works in Script Kit but as mentioned in the thread above Alfred seems to be working solidly.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment