MacOS – How to get file path of user selected .icns file in applescript

applescriptfilemacospath

I'm writing an applescript in which I need the POSIX path to an .icns file that the user selects. For some reason though, it refuses to accept .icns as a valid file type.

This will work:

set Icon to (POSIX path of (choose file of type {"png"} with prompt "Select a .png file."))

But this won't:

set Icon to (POSIX path of (choose file of type {"icns"} with prompt "Select a .icns file."))

When the script is run, it won't let me choose any files, including .icns files.

Any ideas? Thanks.

Also, I'm running 10.8.5 if it helps, and if there is an easy way to convert .png files to .icns files with applescript, I'm open to that too.

Best Answer

Use {"com.apple.icns"} as the file type.

set Icon to (POSIX path of (choose file of type {"com.apple.icns"} with prompt "Select a .icns file."))

(Found by running "mdls myiconfile.icns")

kMDItemContentType             = "com.apple.icns"