Set custom icon for a folder via AppleScript?

Is it possible to set an icon for a folder using AppleScript only? I have a script that replaces the default Dropbox folder icon with a darker version that I made to match the darker icons that I use as default icons. The script uses a 64-bit build that I made of SetFileIcon from Hamsoft:

http://www.hamsoftengineering.com/codeSharing/SetFileIcon/SetFileIcon.html

But I would prefer to use pure AppleScript if possible. A web search produced no results, but I suspect that the experts here will know better. Many thanks for any help.

Assuming your icon is an icns file:

use framework "Foundation"
use framework "AppKit"
use scripting additions

set sourcePath to "/Users/myName/Desktop/myFile.icns"
set destPath to "/Users/myName/Desktop/myFolder/"
set imageData to (current application's NSImage's alloc()'s initWithContentsOfFile:sourcePath)
(current application's NSWorkspace's sharedWorkspace()'s setIcon:imageData forFile:destPath options:2)

:slight_smile:

1 Like

That is exactly what I hoped to learn, but could never have figured out by myself. Thank you.

This one have worked fine for me before (thanks @emendelson). But with MacOS 12 Monterey it stopped working :slightly_frowning_face:.
There is no error message. The icon for a folder just doesn’t change when running the exactly same script for the same folder as I did with Big Sur and earlier.

The script @ionah posted works here under 12.0.1. The image file can be any known image format (.jpg, .tif, etc).

My mistake. It didn’t work for me because the png to be used were missing (due to a preceding clean MacOS install).
This script does’t throw an error if the image file is missing, so I didn’t think in that direction. I’ll add that to my copy.
Thanks.