Open Context Menu at Point and Click a Menu Item in There

What is the current status on opening the Context Menu at point and click a Menu Item in there?

I found lots of older forum threads suggesting a rather complicated method involving clicking a Found Image.

Any better way to do so? And if not, is there some canonical doco on this method or what is a good forum thread to be guided by?

Any pointer much appreciated!

4 Likes

Hi @halloleo, for the Finder context menu I use this AppleScript:

2021_02_09_Support_1

I found this:

Since I use a German macOS, you would have to change the word "teilen" to "share" or the corresponding word of your macOS.
This also applies to the term of action (messages by Message etc.)

07)iMessage <691F 210209T095900>.kmmacros (4,4 KB)

tell application "Finder"
	set sel to get the selection
	
	if (sel is {}) then
		log "Nothing selected! Can't proceed"
		return
	end if
	
	set parentPath to (container of item 1 of sel) as alias
	set desktopPath to (path to desktop folder)
	if desktopPath is equal to parentPath then reveal sel
	
end tell

tell application "System Events"
	tell process "Finder"
		set teilenButton to (first button whose description is "Teilen") of toolbar 1 of window 1
		ignoring application responses
			click teilenButton
			delay 0.1
		end ignoring
	end tell
end tell

do shell script "killall System\\ Events"

tell application "System Events"
	click (menu item "Nachrichten") of menu 1 of teilenButton
end tell

But it can be done a little easier and more efficiently. If you use the BetterTouchTool app, you can set up access to actions in the context menu with this:

2021-02-09_10-03-30

This works wonderfully in any app. You can also integrate this BetterTouchTool action into your KM macro via an AppleScript.

4 Likes

Ah, very interesting. I have BetterTouchTool but I hadn't realised it could do this. Thanks @appleianer

Especially good that it can also be incorporated into a Keyboard Maestro Macro but...

How would I do that? Is there a generic AppleScript? Thanks!

1 Like

@Zabobon in 3 hours I'll be back at the Mac and then I'll give you an explanation and also a KM macro to it

1 Like

Thank you @appleianer. No hurry. It was cheeky of me to ask. Very grateful.

@Zabobon here is a short video on how to use the KM macro :point_down:

Here is the macro for it:

05)UUID AS <B85F 210209T131219>.kmmacros (24,1 KB) 05)UUID AS <B85F 210209T131219>

Would you also be interested in the workflow of inserting KM macro AppleScript into BTT and running KM macros via touch gesture.... has saved me a lot of shortcuts :wink:

3 Likes

Wonderful. Thank you @appleianer . I will have fun playing with this today!

Of course I would be interested :smiling_face:︎. But maybe it should be in a new thread as this is beginning to deviate from @halloleo's original question?

1 Like

Hi @appleianer! This sounds pretty impresive - not sure though if I want to add with BTT another tool in the mix...

So, KM has still no built-in solution?

1 Like

@halloleo unfortunately in KM there are limited possibilities to work with gesture control (mouse, trackpad).
I prefer to work in KM with macro groups in palette form. To not have to remember so many shortcuts, I work a lot with BetterTouchTool gestures.

Here's an example of how I create my screenshots and edit them in Snagit, all without a shortcut or other trigger from within KM.... so only touch gestures :wink:

Video :point_down:

BetterTouchToll is, for its capabilities, very affordable and is regularly developed by the developer.

Have a look at the documentation.

https://docs.folivora.ai

There are also many tutorials to find. You can test BTT for 45 days.

If you use a Setapp subscription, BTT would be included there as well.

@Zabobon I sent you a direct message.

Hi @halloleo - if you find an AppleScript that invokes the context menu of a specific App you can run that AppleScript from Keyboard Maestro.

Or you can use "right-click at Found Image" (which can work well - although I know you wanted solutions without that element).

Or, if you are prepared to move the mouse curser to the point where a contextual menu can be invoked, you can use Keyboard Maestro to right-click and then jump to the item you want by having Keyboard Maestro type its name and have Keyboard Maestro press "enter" to open the item.

But basically, no Keyboard Maestro can't simply access and run a contextual menu item. It would be great if it could.

1 Like

Hey @halloleo,

This is too vague.

The contextual menu on macOS is not a universally accessible thing with a devoted API provided by Apple.

Different applications may require different techniques.

And no – unfortunately there is no canon.

If you're more specific about what you want to do, we'll be better able to help.

-Chris

That's exactly what I need (and that's what I meant with "Open Context menu at point" in my initial description - sorry if this was not clear...) So I will try:

Thanks for this!

Thanks Chris. See this reply above for details.

1 Like

Hi @halloleo

Here is an example to get you started. It is for the Music App and replicates the context menu for a selected track. The mouse pointer needs to also be over the selected item for this to work.

If you wanted to adapt this to your own use, you would change the name of the Playlist to one of your own. But this example is really just to show the concept - the same basic approach would work in any Mac app.

One thing I have found - it can be useful to put very short pauses in to the Macro (as in the example) just to help the Mac OS catch up with the Macro.

Context Menu in Music App - Example for Forum.kmmacros (35.1 KB)

3 Likes

Here's an example @halloleo & @Zabobon of how I do it with BetterTouchTool:

2021_02_10_Support_1

Insert only the context actions into the BTT action and that's it. Now you can choose a shortcut, a touch gesture or insert the BTT action via an AppleScript into your KM macro.

@peternlewis that would be a very good feature for one of the next KM versions.

1 Like

Thanks @Zabobon. That's in principle what I now tried as well. It works and I'm happy enough with it.

However I am aware the method is very brittle or even dangerous: Biggest problem is that the macro does not know which menu item in the context menu finally is the highlighted one before the Simulated Keystroke return fires the menu item action off:

Say, you invoke your macro by accident on a mouse position where the context menu does not contain an entry "Add to Playlist" but an entry "Erase the harddisk" :wink: and somehow the highlight ends finally up on this entry, then Simulated Keystroke return might have devastating effect...

It would be great if we can read which menu item is the highlighted one, before teh macro presses return.

@appleianer What happens in BTT when you specify a context menu item which isn't there? Does then by accident can another be fired as with the above described KM method?

@halloleo in both the BTT example, and the KM example from @Zabobon, you should limit your workflow to the program in which it is to be executed.

In my BTT example I have entered the exact name of the context menu actions. If something is no longer present during execution (in my Gif = KM Forum,) then it looks like this:

2021_02_11_Support_1

@Zabobon has used the same method in his macro. KM needs the pauses between the actions to write the context action correctly. If you should consider this and the context menu action is no longer present, KM shows you an error message.

If you don’t include the last “Return” Action in the Macro (or disable it) the Macro will stop at that point with the item highlighted and you can choose to press Return yourself to select the currently highlighted item on the context menu (or not).

In fact, this is how I would suggest you set up these kind of Macros so you can test them to see that they are doing what you intend before simply firing them off - for the reasons you mention.

When building Macros I often put long pauses (3 seconds or more) after key points just so I can see everything is doing what I expect. Once I’m happy the Macro is robust I remove the pauses or make them much shorter.

With a Macro that goes more than one level into a Context Menu (as in the Music Macro included in my earlier post) I would first just build it to select the first item in the Context Menu (without firing that item with a Return Key Action). Then I would go on to get it to the item in the next level of the menu. And all the time with those long pauses.

Even with the finished Macro I might leave pauses just so I can be reassured it is doing what I want each time I fire it.

And as @appleianer has pointed out - if the item doesn’t exist the Macro would just end with an error. So, it’s not that dangerous.