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.