Dialog Box AppleScript With Specific Icon

Hello,

I was looking for an Applescript script to display with a KM macro a Dialog Alert with a custom icon.

Here is the result, but this alert dialog is not displayed in the foreground which I would like it to be!...

Could you help me?...

1 – Here is the script I found on the web:

use framework "Cocoa"
use scripting additions

property NSThread : a reference to current application's NSThread
property NSImage : a reference to current application's NSImage
property NSAlert : a reference to current application's NSAlert
property app_icon : "/System/Library/CoreServices/CoreTypes.bundle/Contents/Resources/MultipleItemsIcon.icns"

if not (NSThread's isMainThread()) as boolean then
   display alert "This script must be run from the main thread, or by pressing ⌃⌘-R while interactive in Script Editor." buttons {"Cancel"} as critical
   error number -128
end if

set alert_icon to NSImage's alloc()'s initByReferencingFile:app_icon
set myAlert to NSAlert's alloc()'s init()
tell myAlert
   its setIcon:alert_icon
   its setMessageText:"I am message text"
   its setInformativeText:"I am informative"
   its addButtonWithTitle:"OK"
   its addButtonWithTitle:"Cancel"
end tell
set button to myAlert's runModal()
return

2 – And here is how I adapted it to my needs:

use framework "Cocoa"
use scripting additions

property NSThread : a reference to current application's NSThread
property NSImage : a reference to current application's NSImage
property NSAlert : a reference to current application's NSAlert
property app_icon : "Users/————— mypath —————/myicons/Keyboard Maestro.icns"

if not (NSThread's isMainThread()) as boolean then
   display alert "This script must be run from the main thread, or by pressing ⌃⌘-R while interactive in Script Editor." buttons {"OK"} as critical
   error number -128
end if

set alert_icon to NSImage's alloc()'s initByReferencingFile:app_icon
set myAlert to NSAlert's alloc()'s init()
tell myAlert
   its setIcon:alert_icon
   its setMessageText:"Keyboard Maestro"
   its setInformativeText:"Information A : 0123456789

Information B : abcdefghijklmnopqrstuvwxyz"
   its addButtonWithTitle:"OK"
end tell
set button to myAlert's runModal()
return

NB: strangely, the script does not work in Script Editor, but it works perfectly in a Keyboard Maestro macro!...

Thanks for your help... :slightly_smiling_face:

1 Like

The main advantage of the script you found is that it uses the frontmost app's icon in the dialog. If you only want to use KM's, could you just use a standard display dialog? Set the first line's alias to wherever you've stored your icon file:

image

Thank you very much @Nige_S, that will help me a lot. :slightly_smiling_face:
I understand this doesn't work with Display Alert.

These scripts work just fine in the Script Editor if you follow the instructions – i.e. run on the main thread using Ctrl-Cmd-R.

They do not work perfectly in Keyboard Maestro, because so far we've found no means to force this sort of dialog into the foreground (which is highly frustrating).

If you want to run this kind of UI code using Keyboard Maestro you'll have to use an alternative display mechanism.

1 Like

Wow!... Thank you for this information... but I have doubts about my skills with these new tools. I'll look at it anyway...

NB: And thank you for re-qualifying my post: it is now much more readable.

Thank you again @Nige_S, here is what gives my latest version (I think I finally understood the principle of dialog boxes [not simple])... :wink:

set iconPath to alias "Macintosh HD:Users:————— mypath —————:myicons:Finder.icns"
set logicielNom to "TITLE"
set userNom to "0123456789"
set passwordNumero to "abcdefghijklmnopqrstuvwxyz"
display dialog "Information n°1 : " & userNom with icon iconPath with title logicielNom buttons {"Cancel", "Copy"} cancel 1 default button 2
set the clipboard to userNom
display dialog "Information n°2 : " & passwordNumero with icon iconPath with title logicielNom buttons {"Cancel", "Copy"} cancel button 1 default button 2
set the clipboard to passwordNumero

Quick tip -- one of the reasons Mac users are more productive than those using "certain other platforms" is User Interface consistency. One of those consistencies is that the default button of a dialog is the right-most (on a left-to-right language system) and "Cancel" the left-most.

So if you feel a little itch at the back of your brain every time this dialog pops up -- you know why, and how to fix it!

Oops!... I didn't realize it at all. Thank you again @Nige_S for your vigilance (it is corrected)... :+1:

1 Like

Hello Chris (@ccstone)

While you’re right that forcing ASObjC-Code based dialogs running on the main thread is only possible outside of KM - what is about putting that code into an AppleScript-Bundle like you do it with Script-Libraries ?

@tempo could compile his ASObjC written Alert in a file with the .scptd extension and probably run that compiled version from his Macro ?

Greetings from Germany

Tobias

@Nr.5-need_input
“Ccould compile his ASObjC written Alert in a file with the .scptd extension and probably run that compiled version from his Macro ?”
Hello @Nr.5-need_input, unfortunately I don't have all his skills, sorry.

At that point I'd probably give up and use swiftDialog instead -- if only so I could include videos in my dialogs :wink:

1 Like

Hello @Nige_S

Why ?? If I am right based on the knowledge I have the Alert will be forced to run on the mainthread and the macro is paused until the script in its bundle is fully executed.

And - by the way - with a little bit extra work and learning skills you could probably build up dialogs containing videos or gifs putting all together with html code into a single bundle - maybe a really complex challenge though - but I think it could be possible. :wink::sunglasses:

Greetings from Germany

Tobias

Hello @tempo

We are all here to help or get the help we need - right ?

If I am right with what I said then the only things you would have to do is save that script code using Script-Editor or better using Script Debugger as Script-Bundle and set the Execute AppleScript Action to run a Script from Path and put the path to your saved Script-Bundle in that action.

But - and here is the thing - at the moment I am not able to test this out by my self and I that’s the reason why I asked Chris to dig in wether this could really work.

We’re speaking about KM Engine here that has to run the code on the mainthread - and this is only possible involving another script runner process or by bundling up the code. - let’s wait until Chris is chiming in …

Greetings from Germany

Tobias

Because swiftDialog is really cool, especially from an Admin's perspective :wink: If you haven't already, take a look at it -- here's a video of it being used in conjunction with the Jamf Pro MDM along with an accompanying blog post.

Not to mention that we're all supposed to be embracing SwiftUI and avoiding Cocoa (which is sooo 2010s).

And yes -- there's a certain amount of tongue-in-cheek in both my original suggestion and this response...

Thank you @Tobias for all your attention, it's much appreciated.

I'm not a programmer, just an old graphic designer who was looking for an alternative (locally on my mac) to 1Password (which really annoys me to push me to buy a monthly subscription that I absolutely do not want). So I'm looking to build pretty interfaces, pleasing to the eye and practical to present software information, web services, accounts and web access... simply by invoking a “Trigger Macro by Name”.

On the other hand, I find this research in (small) programming quite playful, especially when in the end, it works. I did some ActionScript programming with Adobe Flash a long time ago and I remember some difficulties with the syntax: it hasn't changed today!...

So, nothing extraordinary, but very pleasant to implement... :slightly_smiling_face:

@Nige_S - yes you’re right - the Cocoa Framework is very old and probably not the best thing using it’s API‘s but even though the fact is that sometimes the old API‘s are still relevant and - just mentioning this here - an Alert or even an Enhanced Alert is also possible to write using the AppKit Framework on its own without the Cocoa Framework involved.

I‘ll save that for later - when I decide to learn swift I will have some stuff to play with :wink:

If you look you'll see it's built with SwiftUI, so using "modern" dialog widgets -- but it's all controlled via the command line, which is why Admins love it. So you wouldn't have to do any learning at all to use it. Install, script, and go!

While I'm a great one for reinventing the wheel, I'll again suggest you look at the free alternatives to 1Pass. At work we're covering another dept and have just been handed a KeePass database and I am pleasantly surprised how easy is to use via MacPass, plus there are command line options for automation. (I haven't dug that deeply yet, so a suggestion rather than recommendation.)

Haaaaaaa!... Interesting!... Thanks for the tip @Nige_S, I heard about KeePass without ever going any further: I'll take a few minutes and see if it could suit me... and if my 1Password base could be integrated with KeePass (what I seem to remember)...

You’re welcome, @tempo - I always try my very best to help - if I think my help coul be relevant.

Please use my name without the @ in front of it or better my username to tag me … when your reply is not linked to yours.

I am still learning a lot of basics on programming Computers/Macs … but it’s a lot of fun to me - I love to learn this stuff.

I come from the Metal industry where I programmed CNC Milling Machines.

If you are searching for a password manager without to pay an Abo maybe a tool like Web-Confidential is something for you ?

I use it for a couple of days now and it is great.

1Password is dropping support for privat users so I have to switch to another Password Manager after being nearly 20 years lucky customer and user. It is a shame but life goes on …

Before I go to much off topic here - let’s get back to the topic.

If you want to build your own custom GUI based on your workflow you’ll have to dig into the programming languages you’re given. That’s what I have to say … or you have to tinker with what you get as a finished product.

But if you’re willing to learn how it all works with help just ask here in the forum.

You can learn a lot from the folks here, trust me ..

Greetings from Germany

Tobias (aka Nr.5-need_input)