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:
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.
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)
This one have worked fine for me before (thanks @emendelson). But with MacOS 12 Monterey it stopped working .
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.
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