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:
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.
- 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:
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
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:
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:
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:
See also at the end of the post below.
- Removed alternate Menu Title from the KM action, since it doesn’t seem to work (
- Added extended version with Actions menu.
- Added some of my findings from the post below.
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 (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
Matches any character that is a digit. It is a shorthand for the character class
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. —
1: Except when your only metacharacter is the
|, and there are no spaces in the string, it seems.
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:
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.
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.
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.