How do i show a tooltip when executing macros?

is there some way I could temporarily show a small dialog or tooltip near the mouse pointer/cursor that will stay for a brief time, or until the mouse pointer moves?

MacOS notifications are too size-limited, and can easily go unnoticed, and won't display when do not disturb is active. but I haven't found any other way to display some text in a small pop-up like a tooltip without pulling focus away from either a text field or other window/selection in the frontmost app.

can anyone please tell me how to do this? My goal is to be able to use tooltips to give visual feedback when executing macros (either the TriggerValue and name of the macro, or something like showing copied text in the clipboard after pressing ⌘+c)

Yes, most likely this is very doable using Custom HTML Window action along with some Javascript inside it to keep the window alive for a certain period of time or until the mouse moves. What exactly do you want to be the trigger to remove the tooltip? Time? Mouse activity?

In my opinion it can also be done using the Display Progress Bar action, although that wouldn't be as flexible or as pretty, but at least you could avoid javascript. I'm pondering this method right now. EDIT: This idea won't work because the Progress Bar window appears to be unmovable. So now I'm working on the previous idea.

1 Like

Okay! I got a basic version working (which took a half hour, since I rarely use this action.) This will use the Custom HTML Prompt action to create a window by the name "abc123456" and position it next to your mouse (MOUSEX(),MOUSEY()) and let you use your own text (in this case "My Pop-up Prompt") and then close it when the user moves the mouse.

I haven't yet figured out how to "hide" the Close button, but if I can't figure that out, someone else will tell us.

This is just a starting point. You also wanted the window to close after a period of time (I'm not sure why, as moving the mouse should be adequate) and you may also want to use a variable as your message (I'm not sure why, as you can just use this code in any macro you want.)

Make sure you use these settings on the Custom action. Of course, you can change those actions if you want.

image

There's something else that I couldn't figure out yet. It does indeed grab the focus, but only until the user moves the mouse, so it's not too serious. But perhaps I can figure out how to fix that. I need a bit more time.

1 Like

Thank you for the response, @Airy !

Ideally, I'd prefer the following behavior for tooltip closing:

  • for the 1st second it is displayed, moving the mouse won't have an impact.
  • after 1 second, the tooltip will remain until there's another call for one, or until the mouse moves more than 20 pixels (x or y) from its current position.
  • polling/watching for mouse movement while a tooltip is displayed shouldn't be too frequent - maybe just one check every 150-200ms.

I can probably figure all of this other stuff out on my own. The hard part is just finding a way to show a tooltip window regardless of whichever app is currently frontmost. it'll usually be for macros i use in finder or sublime text. but there will be some global macros that use it across all apps (lile ⌘+c to copy text).

Darn! I was really excited to see someone replied and seemed to have a solution for this issue (i've posted about it in other forums a few times in the last couple of years). But with the HTML/Java tooltip window stealing focus from the current window/text field, i might as well just use the native KM action to display text in a window as i've been doing up to this point.

please let me know if you figure out how to hide the unnecessary UI elements for this (the close button, window frame, etc), and/or if you find a way for the process to launch in the background so it doesn't steal focus.

also, yes, I do intend to make this feature into an abstraction (a script action that opens & runs the script file), which can then be inserted into any KM macro via kb shortcut (currently doing this with a couple other actions i frequently use for debugging, or to cut down on repetitive/redundant steps when creating new macros). that way, as i'm able to modify/improve the tooltip function over time, i won't need to repeatedly update the command/script in every macro that uses it - just once in the script file that's called by the action.

thanks again!

I can give it another shot with your updated, clearer requirements.

I'm not sure why you call that "hard", since that's exactly what the macro I wrote (above) does. My macro shows a tooltip window regardless of which app is currently frontmost. So maybe I don't understand your point here.

What if it took the focus for only a tiny fraction of a second and returned it to the frontmost app after that? Would that be ok? The user would barely notice. I think I can do that, but I don't think this action can pop-up without ever taking the focus.

I tried very hard to use the KM Progress Bar window as the tooltip window, since it doesn't ever take focus, but I couldn't find a way to control the location of that window. But there are others on this forum who might know how to do that.

Is this because you are worried about performance? I would rather wait to see if the performance is bad before trying to optimize a solution. Functionality first, optimization second. I don't think performance will be a problem here.

Do you want the tooltip to stay stationary, or follow the mouse? (I've already tried following the mouse, and it's not very smooth, so I don't like it.)

I'm wondering how and when you are going to call this macro. I don't see the big picture here. I'd like to know what is the maximum amount of text you may place in a window. Believe it or not, sizing a window to fit the text is an EXTREMELY difficult task to get 100% right.

Anyway, I'll start to update the macro now. Let's see how much closer I can get to your requirements. (EDIT: I've solved at least two of your problems. But the focus issue is proving to be very difficult. I may explain why later.)

Wow, that's a difficult requirement. If one copy of the pop-up macro is already running, you want the second one to somehow trigger the first one to stop what it's doing, clean up, and then let the second copy take over. In other words, you want the first one "polling" to see if another copy of the macro is attempting to start up. I like a challenge, so I'm pondering this, but it's very tricky.

I was never able to make an HTML window come up in the background; I just reactivate the app that was frontmost right after my HTML window appears.

I don't believe KM can do it, so it'd probably take AppleScript manipulation, but I'm not sure it's even a window that's visible to AppleScript (the answer to that is above my pay grade :)).

-rob.

1 Like

It respects the "Next Engine Window" action, so try that (with suitable calculations to get it near the pointer). It is, however, limited in how much text it can display so probably isn't suitable as a tooltip proxy.

1 Like

Doh! I thought I'd read somewhere that it didn't do that; guess I need to add that to my bookmarks! :slight_smile:

-rob.

Understood. Your advice is always sage. But I haven't received an answer to my question "how much text could the tip contain?" I'm leaning towards modifying my solution to use this window, since it does handle focus quite nicely.

I may have said it before, but KM needs a lot more actions for displaying information. This is a case in point.

Okay @mz94, I took two steps back and three steps forward. I've changed the display mechanism from Custom HTML to Progress Bar. Even though you are limited to one line of text, this is a fairly interesting solution. It does several of the things you requested. Most importantly, it seems to do precisely what you want with regard to focus. It also closes if you move the mouse more than 20 pixels away.

I'm not saying it's perfect, but it's a valid attempt, and it can be improved to meet a few more of your requirements if you like it so far.

Furthermore, even though it is limited to one line of text, I can improve it so that if it's more than one line of text, it can "roll" through the window like news flashes scroll leftwards on light-bulb signs.

Tooltip at Mouse Location Macro (v11.0.3)

Tooltip at Mouse Location.kmmacros (58 KB)

3 Likes

I'm not sure why you call that "hard", since that's exactly what the macro I wrote (above) does. My macro shows a tooltip window regardless of which app is currently frontmost. So maybe I don't understand your point here.

my point was specifically regarding showing a macOS-native tooltip - the kind you see when hovering over things in Finder or other systems apps, which don't pull focus away from the current window/selection/text field. apparently, this is very difficult or nearly impossible to do without hacking something for it into the apps themselves (adding code hooks via API that'd allow Keyboard Maestro to send data to the app to display a tooltip using the same functionality as the app does natively).

What if it took the focus for only a tiny fraction of a second and returned it to the frontmost app after that? Would that be ok? The user would barely notice. I think I can do that, but I don't think this action can pop-up without ever taking the focus.

aha! now you see why i said it's the hard part. :smile:
therein lies the difficulty, and the key difference between showing a tooltip and showing any other type of window near the mouse cursor: the loss of focus on the current window/activity.

and FWIW, i can probably list at least 5-10 cases i've had where losing focus at a point where I'd want to display a tooltip would be problematic, and result in some amount of frustrating tedium.

and that is the reason i'm still searching for a viable solution to this problem (showing tooltips in conjunction with KBM macros running) after over 2 years.

Is this because you are worried about performance? I would rather wait to see if the performance is bad before trying to optimize a solution. Functionality first, optimization second. I don't think performance will be a problem here.

not a problem for performance of KBM per se. But on any given day, i'm generally using some very hungry apps/processes (professional video/audio work, AI algorithms, etc), so i try to build optimization into everything i do based on each thing's scope/performance requirements. and for this non-critical, semi-cosmetic, asymmetrical functionality, it would be perfectly adequate to check and compare the mouse cursor location with the value saved for it when the macro starts - at most - 5-6 times per-second.

in fact, i actually already have an abstraction set up for this very thing that i could pretty easily add into the tooltip macro. it just repeats a loop indefinitely, comparing the value of one passed variable to another, waiting for a condition (passed as a 3rd argument) to be validated, and returns true and ends the loop once it is. i also added a 4th optional argument to change the frequency (time delay between each iteration through the loop).

Do you want the tooltip to stay stationary, or follow the mouse? (I've already tried following the mouse, and it's not very smooth, so I don't like it.)

definitely want it to have a static location near the mouse cursor when it is called - just like a regular tooltip.

Believe it or not, sizing a window to fit the text is an EXTREMELY difficult task to get 100% right.

actually, i've already solved this issue as well. :smiling_face:

basically, i have an applescript that uses objective c to draw and size a window to the font & character width and row height of a given text within a text object attached to it that looks almost the same as native macOS tooltip (minus the drop shadow, and the animated fading in/out).

there's just one minor problem with this script: it'll only work when i run it within the Script Editor app! I've tried numerous ways of getting it to work from within a KBM macro to display the tip window in the current context of the frontmost application. all attempts have failed.

so again, the hard part - the main issue i'm [still] seeking a solution for here (as indicated in the title of this thread) - is the inability to display an actual, honest to goodness tooltip when running a KBM macro.

Wow, that's a difficult requirement. If one copy of the pop-up macro is already running, you want the second one to somehow trigger the first one to stop what it's doing, clean up, and then let the second copy take over. In other words, you want the first one "polling" to see if another copy of the macro is attempting to start up. I like a challenge, so I'm pondering this, but it's very tricky.

actually, this is the standard functionality for tooltips. utilizing the tooltip class with an empty string, or some other string, causes any existing tooltip to close and/or reopen with the text of the new string at the current relative mouse cursor position respectively.

i also have this working with objective c via applescript. but unfortunately, it seems the problem lies in osascript being used by KBM to run applescripts. so i still can't get my pseudo-tooltip working outside of script editor.

with AutoHotkey on PC, there's a simple function to use to show a tooltip. it'll work within just about any other Windows app, and it doesn't steal focus - not even for a millisecond. it's just a shame Keyboard Maestro and macOS don't share a method to implement the same functionality. :confused:

If you are able to program in Objective C then you are way ahead of me. Try my second solution and let me know if you like it. It solves your focus problem perfectly. But in other ways, it's not ideal.

1 Like

I've already figured this out as well. i can get (via mouse hover and return of the ui path to an element, including its window ID, upon telling the element to click) and move just about any window via an applescript i wrote, which will run from KBM macros, and even works with command palettes, and the untitled Keyboard Maestro engine windows the app uses to display text in a window.

seriously, i've been cracking away at this issue periodically for at least 2 years. I've learned a lot and have come a long way in this quest. yet still, none of the questions i've posted anywhere have yielded any viable solution for showing a tooltip from KBM macros.

at this point, i guess i'm [not so] secretly hoping that, if i just keep asking about it and seeking a solution for this problem, eventually, maybe @peternlewis will step up to the plate and knock this one of the park. :slightly_smiling_face:

cool! i never thought to try using a progress bar like this.

and while it's not exactly a tooltip, this will suffice for now to solve at least a few of the issues i've been itching for tooltip access to help resolve.

one of the big ones is having an easily/quickly identifiable verification that KBM actually is receiving the shortcuts i press, and executing any number of macros properly. because, at many points in my work, performance issues may be causing hangs/interruptions of the KBM process. when this occurs, it can be very frustrating, wondering if a macro that has no easy-to-see/hear impact is actually executing, or if a temporary process hang, loss of app focus, or some other issue caused it to fail.

i won't be holding my breath. but i will be keeping my fingers crossed for a KBM action to access and use the macOS tooltip functionality. but in the meantime, this will help to resolve at least some of my headaches in a simple and efficient way.

thank you, @Airy , and everyone else who jumped in to offer some help with this!

it'll work, at least for now. thanks again!

and regarding objective c, i don't really know it very well either. what i was able to do with it was mainly the result of trial & error, Googling for different pieces of the puzzle, then putting them together, modifying, and customizing it until i was able to get it all working reliably.

You are welcome.

Oh, well if that's one of the goals, may I suggest opening a terminal window and entering this into the window (replace "myname" with your username, and I'm assuming your KM folder is in the standard location)

tail -f /Users/myname/Library/Logs/Keyboard\ Maestro/Engine.log | grep Execute

Every time a macro triggers, you will see its name. This is much easier than creating a pop-up.

Given that "MacOS notifications are too size-limited..." -- probably more than the "Display Progress" dialog will allow.

This is no general "macOS tooltip functionality". There's an API for tooltips, but it's up to applications to use it rather than being something you can call on from anywhere.

Could KM implement "tooltips" that could be populated, positioned, and controlled within a macro, displayed by the KM Engine? That question's beyond my pay-grade :wink:

Meanwhile, if you want an alternative to the Progress Dialog approach you should take a look at SwiftDialog. Customisable dialogs with lot of options, including size and position. You can use Markdown in the text and update the dialog on the fly, without closing and re-opening it.

And, perhaps importantly for you, there's an "always on top" option so while it will (like most dialogs) grab focus on spawn you can easily immediately reactivate your app yet still have the dialog float above it.

It certainly ain't a "proper" tooltip -- but I think it's the closest you'll get for now, short of writing your own app to do it!

yes, but this would also require diverting focus from fullscreen apps i'm using for my work, and is also slower than the immediacy of seeing a tooltip as soon as j press a shortcut.

however, i do have a KBM macro bound to a shortcut to open the engine log in console with a keystroke. :slightly_smiling_face:

yes, true. there are times where i will append several items to an array before processing everything in the array. in those cases, I'd want to be able to see a tooltip that would list the full content of the array as i'm adding to it to verify that every item I'm trying to append has successfully been appended.

yes, i understand. what i was thinking is that, if the focused app does in fact have the toolTip API included, and uses it to display tooltips within the app, and the app is scriptable, in the same way i can use an AppleScript and system events to communicate with apps to get and set variables, preferences, properties, and other data within an app, it should, in theory, be possible to set the text of the toolTip property (which most macOS native apps should be/are using, e.g. Finder, System Preferences, Script Editor, Safari, and most others) in any app that includes it with an AppleScript or JavaScript via system events. according to the toolTip developer documentation, setting the toolTip property to a string will display a toolTip with that string. i'm figuring this should be possible in the same way that the system pastebin (the clipboard) is implemented and supported, and can be invoked and manipulated between apps via AppleScript.

if i were to write an app and include the tooltip API so my app could display tooltips, i would need to be using my app for it to display a tooltip. otherwise, my app would need to steal focus from the frontmost app in order to display a tooltip within my app.

I'll have to look into SwiftDialog as you suggested. but, rather than having its window constantly floating at some position on top of whatever else i'm doing, i could probably script something to reposition its window to the mouse cursor for a few seconds (or until the cursor moves 20 pixels), and then move it to a position outside of the screen's display area (thus making it appear to be hidden). then, most of the time, it wouldn't be visible. but when i wanted it to show me something as feedback/debug of whichever KBM macro i'm having a problem with, it could functionally appear to work the same way as a toolTip.

actually, now i'm wondering if i might just be able to do the same thing using the dialog window that KBM engine spawns to display text. i guess that depends on whether or not i can get the ID of that window and change the text displayed within it rather than creating a new instance of it. does anyone know if this is possible?