Can the Display Time of Brief Text Display Notifications be Changed? (with sidetrack to setting the icon image for AppleScript notices)

The thread Display Text Large - Can the Duration of the Display Be Changed? points out how the "Set Action Delay" action can be changed to "Set Large Text Display Delay", to control how long large text is displayed.

I would like to do the same for the "Display Text "" Briefly" action, which shows the same kind of banner as AppleScript "display notification". The default time seems to be five or six seconds, and in my particular usage, sometimes that is annoyingly long.

Can the Display Notification delay time be changed?

I looked for doc about the AppleScript display notification and it seems to be a system setting that has a minimum of five seconds -- if you change it to less than five, you get five.


UPDATE: I changed the title to reflect an interesting, somewhat related, sidetrack that came up later in this thread.

Nyet.

Keyboard Maestro sends the notification to the system and has no control after that.

If you want precise control then you'll have to use an alternative mechanism like the Custom HTML Prompt action.

I do seem to recall a Unix command line notification tool that could set a whole bunch of parameters, but I couldn't find it on a cursory Google search.

This works for me.

Chris

Thanks @ChrisQ,

I found that previously, and found other documentation that says that the minimum value is 5 and if you set a value less than 5, you'll get 5. And 5 is too long for my purposes, I end up counting the seconds until it disappears.

You said it works for you. Did you get a value of 2 or 3 to work? That would be cool. Maybe that discouraging doc that I read is out of date. I hope so.

Thanks.

Can you get by with an AppleScript that closes the notification after 2 or 3 seconds? One problem is that the close button isn't available unless the pointer is over the notification, which may mess up your plans...

Hey! What happened?! Am I imagining something?

I distinctly recall someone (unfortunately I have forgotten who) posting here with a link to another thread about doing notifications with HTML, including short duration, transparency, and rounded corners. They included a video demonstrating their macro, an image of the macro in KBM, and a downloadable version of it. I recall the macro said, "Hello, World!" for the displayed text while the video said, "Text Here" or something like that.

I was ready to mark it as the solution as soon as I got a chance to test it. And before I could download it, it has disappeared.

To the author: could you please upload it again?

To everyone: did you happen to snag a copy that you could upload?

Thanks.

This maybe?

TIP: Custom HTML Prompt to Display Text Large

2 Likes

I wanted to slightly edit the Macro I uploaded. Here it is again. It is based on @martin's really good Tip as also linked to by @mrpasini. If you scroll down that thread, you will see how the original Macros can be adapted re fonts, round corners, positioning, size, time shown on screen etc.

The version I made for myself (here) allows the time the Prompt stays visible to be set by a Variable, has the font as Helvetica and has rounded corners.

There are two versions, one that appears in the top right of screen and one that appears in the center of screen.
Display Text with HTML Prompt - Top Right - Custom Timer @martin v2.00.kmmacros (7.3 KB)
Display Text with HTML Prompt - Center Window - Custom Timer @martin v2.00.kmmacros (7.3 KB)

Click to Show Image of Macro

Display Text Large with HTML Prompt - Top Right - Custom Timer @martin

3 Likes

I tried the defaults command -- it won't honour values < 5 seconds, at least on Ventura.

If you do want to continue with Notfications and can stand a fraction of a second of mouse movement (there's no click, so no focus change) then this demo should get you started.

Close Notification.kmmacros (4.6 KB)

Image

You'll need to change the mouse coordinates to suit your setup (there's probably a way to get useful numbers on the fly, but I didn't get that far), and if you aren't on Ventura you might need to change the "path" to the "Close" button...

2 Likes

Hey Folks,

This is not fancy at all, but it has the virtue of being pretty simple to use.

  • A number of the options are defaults, but I have included all in the example.
  • Giving up time is integer only, so the minimum time is 1 second.
    • You can hit the Escape key though.
  • AppleScript's Display Dialog command has similar features.

-Chris

--------------------------------------------------------
# Auth: Christopher Stone
# dCre: 2022/12/17 07:40
# dMod: 2022/12/17 07:40 
# Appl: Standard Additions Osax & System Events
# Task: Display an Alert Dialog with a Timeout.
# Libs: None
# Osax: None
# Tags: @Applescript, @Script, @System_Events, @Display, @Alert, @Dialog, @Timeout
--------------------------------------------------------

set appAlias to path to frontmost application as text

(*
Alert types:

critical
informational
warning
*)

set alertType to informational
set mainText to "Alert Dialog!"
set messageText to "Secondary Text"
set buttonValueList to {"Cancel", "Other", "OK"}
set defaultButton to "OK"
set cancelButton to "Cancel"
set givingUpAfterTimeInSeconds to 1

try
   
   tell application "System Events"
      set resultAlertReply to display alert mainText ¬
         message messageText ¬
         as alertType ¬
         buttons buttonValueList ¬
         default button defaultButton ¬
         cancel button cancelButton ¬
         giving up after givingUpAfterTimeInSeconds
   end tell
   
   tell application appAlias to activate
   
on error errMsg number errNum
   tell application appAlias to activate
end try

--------------------------------------------------------
3 Likes

Just messing around, and found you can easily feed in Named Clipboard images as Display Dialog icons by writing the Named Clipboard out to a temp file (or, of course, you could maintain a folder of images and pick from them) -- it'll take PNGs, regardless of what the dictionary says.

AS Dialog with Icon.kmmacros (4.0 KB)

Image

1 Like

Would you post a sample of what this looks like? This sounds like fun.

It's in the demo, above, using a Named Clipboard called "Alert Icon 1" -- create that, pop any old image onto it, run the macro.

I did think about getting the icons of the currently-active app so you could have contextual KM dialogs, but there's no consistency to icon file paths beyond someApp.app/Contents/Resources/ -- @ccstone, have you got a way of programmatically getting that path for any app?

I reckon.

--------------------------------------------------------
# Auth: Christopher Stone <scriptmeister@thestoneforge.com>
# dCre: 2016/05/02 03:54
# dMod: 2022/12/18 18:59
# Appl: AppleScriptObjC
# Task: Finding the .icns File of an Application Given Its Bundle ID.
# Libs: None
# Osax: None
# Tags: @Applescript, @Script, @ASObjC, @System_Events, @Read, @Keys, @Plist, @Smile, @BBEdit
--------------------------------------------------------
use framework "Foundation"
use scripting additions
--------------------------------------------------------

# Bundle IDs for testing:

# Test apps:
# set bundleID to "com.stairways.keyboardmaestro.editor"
# set bundleID to "com.stairways.keyboardmaestro.engine"
# set bundleID to "com.latenightsw.ScriptDebugger5"
# set bundleID to "com.latenightsw.ScriptDebugger8"
set bundleID to "com.apple.calculator"
# set bundleID to "com.apple.iWork.Pages"

--------------------------------------------------------

# Find Application's info.plist file using its Bundle ID.

set theURL to current application's NSWorkspace's sharedWorkspace()'s URLForApplicationWithBundleIdentifier:bundleID
set appInfoPlistPath to (POSIX path of (theURL as text)) & "Contents/Info.plist"

--------------------------------------------------------

# Getting keys from applation's info.plist file.

set theDict to current application's NSDictionary's dictionaryWithContentsOfFile:appInfoPlistPath
set cfBundleIconFileName to (theDict's valueForKeyPath:"CFBundleIconFile") as text

--------------------------------------------------------

# Locating the application's .icns file.

set appIconFilePath to ((theURL as text) & "Contents:Resources:" & cfBundleIconFileName)

tell application "System Events"
   
   if not (exists of disk item ((theURL as text) & "Contents:Resources:" & cfBundleIconFileName)) then
      set appIconFilePath to appIconFilePath & ".icns"
   end if
   
   return POSIX path of disk item appIconFilePath
   
end tell

--------------------------------------------------------
1 Like

While searching for info on Notifications, I found this comment about KBM on Apple Insider:

How to make Mac notifications show longer, or leave faster | AppleInsider

Working around the limitations

One issue with notifications is that they distract you by appearing. A second is that they either require you to dismiss them, or they can require you to select from a drop down menu.

If you have an app such as Keyboard Maestro, you could programme a keystroke to dismiss a notification for you. It would require Keyboard Maestro to move your cursor to precisely the right spot on the screen, wait half a second for the Close or Clear box to appear, then clicking for you.

That would work until you moved to a different Mac, say going from your office iMac to your home MacBook Air. Then the position for the cursor would be different because of the smaller screen, but you could make a second copy of the Keyboard Maestro macro.

You could program it to do all of the above and then recognize when a notification is showing an Options button. Keyboard Maestro could click in that button and select which ever option you've previously decided.

All of this is possible, and you're not going to do any of it. Much more sensible and easily straightforward is to use the heavy-handed Do Not Disturb.

While I don't think that their approach is the best, it's interesting to see where KBM is getting publicity and what people think it's good for.

Thanks for the upload, @Zabobon!

I was finally able to install this and try it it out.

I really like it. Thanks!

Since you're familiar with how this works, I have a couple of questions. Or if @martin or anyone else can chime in, I would appreciate it.

I see there is a variable cleanup at the end, after a delay of 2 seconds. Is there an advantage to this method over naming the variables as "Local_*"?

I'm concerned about both the delay and the globalness of the variables because I intend to use this when I am switching desktops and it's very likely that I would type a ⌃→ twice in a row in less than two seconds. (That's one of the reasons that I want a notification to disappear faster than five seconds.) Doing that would trigger a second instance of the notification, with different text, before the first one had finished. That seems problematic.

When I get the chance to try it out, I'll try making all the variables Local and removing the cleanup process at the end and see how that works. Before I do, if there's a reason for it being the way it is that I'm not recognizing, I'd appreciate knowing.

Thanks

I have updated the two Example Macro's in my post to version 2.00 to work with Local Variables so, there is no cleanup needed at the end.

(To use Local variables in HTML prompts the HTML Action gear menu has to be set to not run Asynchronously. I only just discovered this. It was not set this way in the original macros and every time I had tried a local variable they had failed. Anyway, fixed now.)

3 Likes

Hi @August,

In the sample macro, I used a global variable Text to Display because, in a real case usage, the variable is supposed to be set in another macro, then this macro is executed to display the variable content. Using a global variable is more convenient to use, especially for those who are not familiar with Instance Variables or action:Execute a Subroutine or do not have KM10 (Subroutine is only available with KM10+).

Another advantage of a global variable is that you can still see the variable value after the macro is completed (if the variable is not deleted). For notification use, I would assume that the variable value contain only very few words and will only take up minimal bytes.

Nevertheless, if you do not wish to see the variable value at all, and/or if you would like to delete the variables anyway at the end, using local/instance variables would be a better choice.

1 Like

Just to add to this, if you do have KM10+, using this as a Subroutine works really well to call it from other Macros.

And the Action to call it:

3 Likes

I finally got around to testing this as a subroutine and found something buggy with the parameter names in the subroutine.

I had to change the parameter names to LOCAL__TextToDisplay and LOCAL__TimerDuration.

As shown above, I got no text displaying and the timer countdown showed "NaN". The window never went away and I had to manually cancel the macro from the KBM Status Menu.

I have just recently learned that KBM is very forgiving about capitalization and spaces in variable names, but apparently the HTML window.KeyboardMaestro.GetVariable() function is not as flexible.