(AppleScript) Display alert with image?

I am creating a fake virus script and I was going to add more but I kept playtesting. Eventually, I realized what I wanted to do was create a "display alert" with a message AND icon.

Here is the code so far:

display alert "The application ''Safari'' wants to acces your password." message "If you trust this application, enter your password in the following message."
display dialog "Password goes here" default answer "••••••••" 
tell application "Finder"
	activate
	make new Finder window
end tell

I wanted to figure out the way to add an icon without getting an error, but, is that really possible? Please help.

By the way, in case you were wondering, the actual script has the bullets, they're Unicode characters (u+2022) and I decided I should have them as a "hide" effect. It would also help if I learned how to actually have a real hide effect where you would enter text and it looks like the Unicode bullets.



iMac 21.5″, macOS 10.15

Posted on Dec 14, 2020 8:07 PM

Reply
Question marked as Top-ranking reply

Posted on Dec 15, 2020 11:51 AM

And programmatically adding a custom icon to display alert. This will change with the appearance mode.



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


5 replies
Question marked as Top-ranking reply

Dec 15, 2020 11:51 AM in response to VikingOSX

And programmatically adding a custom icon to display alert. This will change with the appearance mode.



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


Dec 15, 2020 11:16 AM in response to Zurcrescred556

The display Alert does not support custom icons from AppleScript, but it can be done via AppleScript/Objective-C. Otherwise, with AppleScript, display dialog does have icon support via its with icon clause:



use scripting additions

property app_icon : "/System/Library/CoreServices/CoreTypes.bundle/Contents/Resources/MultipleItemsIcon.icns"

display dialog "some sample text" with title "Dialog with Custom Icon" with icon POSIX file app_icon as alias
return


Dec 15, 2020 11:47 AM in response to Zurcrescred556

> It would also help if I learned how to actually have a real hide effect where you would enter text and it looks like the Unicode bullets


Dictionaries are your friend.


The display dialog command has a few optional parameters. As Viking noted, there is the with icon parameter to include an icon (either a standard OS icon, or your own image) in the dialog. There is also a ' hidden answer' parameter that will mask the value entered:


display dialog "Enter your password:" default answer "abcd1234" with hidden answer


This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

(AppleScript) Display alert with image?

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.