Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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

This comment has been minimized.

Copy link

@tonykastaneda tonykastaneda commented Aug 4, 2021

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

@vitorgalvao

This comment has been minimized.

Copy link
Owner Author

@vitorgalvao 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

This comment has been minimized.

Copy link

@tonykastaneda tonykastaneda commented Aug 4, 2021

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

This comment has been minimized.

Copy link
Owner Author

@vitorgalvao vitorgalvao commented Aug 4, 2021

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

This comment has been minimized.

Copy link

@tonykastaneda tonykastaneda commented Aug 4, 2021

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

This comment has been minimized.

Copy link

@tonykastaneda tonykastaneda commented Aug 4, 2021

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

This comment has been minimized.

Copy link
Owner Author

@vitorgalvao vitorgalvao commented Aug 4, 2021

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

This comment has been minimized.

Copy link

@tonykastaneda tonykastaneda commented Aug 4, 2021

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

This comment has been minimized.

Copy link
Owner Author

@vitorgalvao vitorgalvao commented Aug 4, 2021

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

This comment has been minimized.

Copy link

@tonykastaneda tonykastaneda commented Aug 4, 2021

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

@tonykastaneda

This comment has been minimized.

Copy link

@tonykastaneda tonykastaneda commented Aug 4, 2021

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

@vitorgalvao

This comment has been minimized.

Copy link
Owner Author

@vitorgalvao vitorgalvao commented Aug 4, 2021

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

@tonykastaneda

This comment has been minimized.

Copy link

@tonykastaneda tonykastaneda commented Aug 5, 2021

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