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.

I made it so I could call the applescript with variables for iconfile and target file but I get an error -1763? Anyone know how to get rid of that?

osascript: couldn't save changes to script /Users/jwsmite/Desktop/dot_dmg_maker/iconscript.applescript: error -1763.

osascript myscript.scpt myicon.icns targetfolder/

myscript.scpt

use framework "Foundation"
use framework "AppKit"
use scripting additions
on run argv
	set sourcePath to (item 1 of argv)
	set destPath to (item 2 of argv)
	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)
end run

It does what it’s meant to I just don’t know why I get this error print out too

Hello @jwsmite

looking at one of the beautiful Error-Code Lists found here you’ll get the information:

–2763 No result was returned for some argument of this expression.

I think that’s the Key here…

Greetings from Germany

Tobias