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

Is it possible to do both desktop and finder in one script?

@vitorgalvao
Copy link
Author

vitorgalvao commented Aug 4, 2021

Is it possible to do both desktop and finder in one script?

That I’m aware, it is not possible to do on the Desktop at all. It used to be, but Apple has crippled this functionality more and more with each OS release. Perhaps they’ve since made it possible, but I wouldn’t count on it.

@tonykastaneda
Copy link

I found a script that allows the action to be performed on the desktop. I’m not too familiar with AppleScript as a whole I was wondering if it’s possible to merge the 2 somehow? Here is the script that allows selected items on the desktop to bring up the context menu

tell application "Finder"
set sel to get the selection

if (sel is {}) then
    log "Nothing selected! Can't proceed"
    return
end if

set target_item_name to the name of (item 1 of sel) 

end tell

tell application "System Events"
tell process "Finder"
tell group 1 of scroll area 1
set target to the first image whose value of attribute "AXFilename" is target_item_name
tell target to perform action "AXShowMenu"
end tell
end tell
end tell

@vitorgalvao
Copy link
Author

If you had an alternative, you should’ve started with that!

Thank you for that code, but where did you get it from?

It seems to work, so I’ll take a look into merging them later.

@tonykastaneda
Copy link

I found it in an extremely old stack overflow post if I find the page in my history I’ll paste it here so you can read the thread. Thanks 🙏

@tonykastaneda
Copy link

Not to sure about the reliability as you mention apple changes a lot from OS release to OS release. But it seems to have been posted back in 2015 here: https://stackoverflow.com/a/29406336

@vitorgalvao
Copy link
Author

Updated. The new code works in both the Desktop and the a Finder window. Though not if said window has the Desktop open. Yet unsure if we could check for that use case, but it feels rare enough.

@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