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.
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:
[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:
[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:
Contextual menu in KM, action selected:
Contextual menu in KM, macro in macro list selected:
It still wonât work in Word, Numbers, etc. But you get the idea.
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.
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:
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:
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:
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:
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. 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:
-
String starts with either âEnableâ or âDisableâ; followed by one space.
-
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â). -
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.
Thanks so much for your detailed explantations.
This is a nice example for what I meant in the other thread when I said
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.