custom icon in dialog

1.4k views Asked by At

I have a file in my current directory Icon.png.

How do I make this the icon for an applescript dialog?

I have tried

$ osascript -e 'display dialog "Hey" with icon file "./Icon.png"'
0:54: execution error: File file ./Icon.png wasn’t found. (-43)

So how can I get a local image and use it as the icon in a dialog?

I am happy to convert the image to a .icns if necessary.

2

There are 2 answers

0
minseong On
tell application "System Events" to display dialog "{0}" with icon file (path of container of (path to me) & "Icon.png")

Worked for me. I had to save it in its own file, and I replaced the {0} with the message that needed to be in the dialog at runtime.

0
user137369 On

As mentioned in the comments, by default AppleScript will not understand a POSIX path, and you need to give it the full one, not a relative one.

osascript -e "display dialog \"Hey\" with icon POSIX file \"${PWD}/Icon.png\""

AppleScript requires double quotes and you need them surrounding the code so bash can interpret ${}, which is why there are so many \".

No need to convert your icon to .icns. AppleScript will gladly take your .png.