1

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.

theonlygusti
  • 11,032
  • 11
  • 64
  • 119
  • osascript does not understand `./` you have to pass the full (HFS! - colon separated) path. – vadian Dec 27 '16 at 15:40
  • @vadian how do I get the full path? Pretend I have no idea what directory the script is running in. (The final thing has to be portable.) – theonlygusti Dec 27 '16 at 15:40
  • I have no idea to do this in bash, on the AppleScript side you get the path to the current script with `path to me` – vadian Dec 27 '16 at 15:42
  • @vadian how does that get "injected" into the script? – theonlygusti Dec 27 '16 at 15:43
  • You need to [retrieve the path in bash](http://stackoverflow.com/questions/4774054/reliable-way-for-a-bash-script-to-get-the-full-path-to-itself), then pass `POSIX file thePath as alias` where `thePath` is the slash separated full path to the icon – vadian Dec 27 '16 at 15:46
  • @vadian you just told me that in applescript I can use `path to me`.... – theonlygusti Dec 27 '16 at 15:55
  • @vadian how do i use `path to me` to get the path of an image file within this directory? – theonlygusti Dec 27 '16 at 15:55
  • Once again, `path to me` works in a compiled (saved) AppleScript: `tell application "System Events" to set thePath to path of container of (path to me)`. `thePath` will contain the HFS path to the parent folder of the script. – vadian Dec 27 '16 at 15:58

2 Answers2

4

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.

user137369
  • 5,219
  • 5
  • 31
  • 54
0
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.

theonlygusti
  • 11,032
  • 11
  • 64
  • 119