Keyboard Shortcut to Right Click in Mac OS X

Is it really possible to invoke a context menu using a keyboard shortcut instead of clicking the right mouse button in OS X? Please not that I don't want to depend on the position of the mouse cursor.

3 Likes

Hmm, the right-click menu is called “contextual menu” for a reason: The content of that menu depends on where you are clicking (that’s the context).

The context-agnostic menus of an app are usually located in the main menu bar.

You’ll notice the difference when you are trying to access menus via UI Scripting: The menu bar menus are (usually) easily accessible, the contextual menu not so much, because it is built “on demand”, after a right-click at a certain location.

For example, in a Finder window right-click a file, right-click an item in the side bar, right-click the title bar. You’ll get three very different contextual menus.

Can you give an example of what you are after? For which application?

Is it really possible to invoke a context menu using a keyboard shortcut instead of clicking the right mouse button in OS X?

I once searched for a solution and found the following script in this forum. Not sure if it might help you. I use it for a selected file in a finder window without a relation to the current mouse position.

But it does not work for me within KM i.e. to right-click a selected action.

Well, if it’s just about right-clicking independently of where the mouse cursor currently is (= anywhere), then this should serve:

38-pty-fs8
[test] Right-Click at Cursor Position.kmmacros (2.4 KB)

But the contextual menu you get naturally depends on the cursor position in the moment you trigger the macro.


If it’s about invoking the contextual menu on a specific element or of a focused element without moving the cursor there, then you have to use UI scripting, as attempted here.

This is the same script that @Hans-Peter_Henkel has posted as screenshot above. It works in many applications, if an element, for example a word in a text field, is focused (selected).

But for UI scripting usually it’s easier and preferable to get a the same menu item from the regular menu bar menu – if it’s there.

Thanks. I found a script very similar to yours that works great with finder. However, I am looking for a script that works on any apps. For example, if I am typing some notes via textedit, I want to be able to right click via keyboard shortcut.

Hi Tom, I am looking for help with creating a script that would invoke the contextual menu on a specific element or of a focused element without moving the cursor. I did try a script similar to @Hans-Peter_Henkel provided, but unfortunately it only works with Finder.

It seems the screenshot @Hans-Peter_Henkel provided shows the script from this post. I played around with this script and it works in many apps, to a varying degree:

  • Finder, Mail (not in side bar), Bear, Quiver, TaskPaper, EagleFiler, …
  • In Text entry areas only: TextEdit, iA Writer, Safari, Things, Notes, Stickies, Keyboard Maestro, …
  • Works not in Pages, Numbers, Excel, Word, BBEdit, …

Here the script as KM macro, only slightly modified:

24-pty-fs8
[test] Invoke Contextual Menu on Focused Element.kmmacros (3.5 KB)


It can be for different reasons that it doesn’t work in all applications.

Most likely: The focused element does not support AXShowMenu and you have to target a different element. But other reasons are possible, of course.

So, to make the script work with more apps, you’d have to work on a trial and error basis: Each time you encounter an app where it doesn’t work, try to find the appropriate way to generate the contextual menu and add it conditionally to your script.


For example, with the following modification the script will now also generate the contextual menu in KM when an action or a macro in the macro list is selected (not only when the cursor is inside a text area, as with the original script):

tell application "System Events"
  set frontApp to name of first process whose frontmost is true
  tell application process frontApp
    try
      set theSelection to value of attribute "AXFocusedUIElement"
      tell theSelection
        set defaultAction to perform action "AXShowMenu"
        if defaultAction is missing value then
          tell (first group whose selected is true) to perform action "AXShowMenu"
        end if
      end tell
    on error errMsg number errNum
      display alert errMsg & return & return & errNum
    end try
  end tell
end tell

[test] Invoke Contextual Menu on Focused Element (Extended).kmmacros (3.8 KB)

Contextual menu in KM, selection or cursor in text entry area:

50-pty-fs8

Contextual menu in KM, action selected:

29-pty-fs8

Contextual menu in KM, macro in macro list selected:

06-pty-fs8


It still won’t work in Word, Numbers, etc. But you get the idea.

2 Likes

Wow, Tom - thanks again.

Your adaption of this script gives me the ability back to disable/enable any macro or action in KM with a single shortcut. This worked for me in the previous version (8) of KM because there was a kind of icon in the upper right corner of the window or so (I don't remember exactly). With the changed UI in version 9 I wasn't able to do it anymore because I couldn't get it working with the changing menu entries (Enable…/Disable…). But now it works like before.

So great, thanks again. :+1::wink:

Enable:Disable Macro or Action.kmmacros (5.1 KB)

Wow. Thank you so much seriously! Your extended version works great, it even works on textedit!

For this you don’t need that script. Just set the shortcut in System Preferences > Keyboard Shortcuts > App Shortcuts:

35-pty-fs8

Since only one of the menu items is available at a time, you can assign the same shortcut.

This doesn’t work for multiple actions/macros though, because the menu item name changes from “Enable Action” to “Enable n Actions” (where n is the number of selected actions or macros).


If you want a shortcut for enabling/disabling single as well as multiple actions/macros at a time, then you can do it with the Select or Show a Menu Item KM action, which accepts Regular Expressions:

23-pty-fs8

Enable:Disable One or More Actions:Macros in KM.kmmacros (26.7 KB)

The “Enable/Disable Action” menu item appears in the View menu only when an action itself is selected (mouse click on action), but not when the cursor is inside a text are of an action.

In the latter case the menu item appears only in the Actions menu. To make it work also for these cases, you can add a second action:

56-pty-fs8
Enable:Disable One or More Actions:Macros in KM (ver 2).kmmacros (28.2 KB)

Note the setting in the Gear menu of the first action:

28-pty-fs8

If I haven’t missed any other competing menu items that start with “Enable” or “Disable”, and as long as Peter does not add any, you can also get away with a simplified RegEx:

^(?:Enable|Disable)

See also at the end of the post below.


Edit (2020-02-17):

  • Removed alternate Menu Title from the KM action, since it doesn’t seem to work (View|Actions).
  • Added extended version with Actions menu.
  • Added some of my findings from the post below.

Thanks Tom.

The first one I know but don't like because these settings are not sync'd between different devices and I need to set them again i.e. after a new OS install etc.. But the second example targeting more than one menu item alternately is again like magic for me. :wink: And of course a very good example for other scenarios. Thanks!

I think, it does get transferred if you use the migration assistant when setting up a new Mac.

But, indeed, they should make it sync via iCloud, as they already do with the text replacements in System Preferences > Keyboard > Text.


This is a nice example for what I meant in the other thread when I said

The RegEx used here…

^(?:Enable|Disable) (?:\d+ )?(?:Macro|Action)

… is pretty simple:

^
Normally: An anchor that marks the beginning of the string. Here: In the context of this specific action it also tells KM that the following is a RegEx (KM Wiki). That is, it is obligatory in the special case of an RegEx in KM Menu actions[1] (It is not obligatory in any “standard” RegEx use-cases.)

|
A simple alternation (“or”).

()
The parentheses define a group. The ?: is optional and has no grammatical meaning; it says that the group is not a capturing group, just a “normal” group for logic grouping, which we need for the alternations here.
For understanding the RegEx just fade out the ?:.

\d
Matches any character that is a digit. It is a shorthand for the character class [0-9] .

+
A quantifier that means ‘one or more of the preceding item’.

?
Another quantifier, which means ‘zero or one of the preceding item’; in other words: it makes the preceding item optional.


So, the expression for the Menu Item reads like this:

  1. String starts with either “Enable” or “Disable”; followed by one space.

  2. Then comes a sequence of any number of digits followed by one space. This sequence is optional (the ? after the group), because it appears only in the plural form (“Disable 2 Actions” vs “Disable Action”).

  3. Finally, either the string “Macro” or “Action”.

The optionally following plural s is irrelevant, because we do not match until the end of the string ($); so we can ignore it.

That’s all. Magic gone.


Footnotes:

1: Except when your only metacharacter is the |, and there are no spaces in the string, it seems.


PS:

I checked the KM menus for competingly named menu items in the View and Action menu. It seems the only one is “Hide Disabled Maco Groups” in View.

So, you could get away with an even simpler expression:

^(?:Enable|Disable)

Or even

^Disable|Enable

With the latter the order is important: ^Enable|Disable would trigger the “Hide Disabled Maco Groups”.

All the expressions also work for macro groups, BTW.


PS 2:

I just noticed that the View|Actions for the menu title doesn’t work. It seemed to work, because the “Enable/Disable Action” redundantly also appears in the View menu, if an action is “properly” selected, i.e., you have clicked the action and your cursor is not inside a text area of the action.

I’ll edit my previous post accordingly.

2 Likes

Thanks so much for your detailed explantations.

This is a nice example for what I meant in the other thread when I said

Macro Palette | "Available when a focused window…"

With pretty basic [RegEx] knowledge you’ll be able to cover many common situations.

That's correct.

I will definitely investigate a bit more here as soon as I have the chance. It will take some time because a few days ago I didn't know about regex at all and also my English is not the best (to say the least) which slows down the process of understanding.

Anyway, I really appreciate your great help and explantations! Thanks again. :+1: