Change Folder Image

Get help. Get answers. Let others lend you a hand.

Moderator: Mr_Noodle

Change Folder Image Fri Apr 14, 2023 10:02 am • by DoogieWall
Good morning:

I'm trying to create a hazel rule/action that will monitor a folder and then change the folder image for any subfolder created within the main folder. The folder structure is as follows: Top Folder/Year/Month/Day.

The rule fires but the apple script fails. The apple script works when ran through the Script Editor on my Mac. I think the issue is setting the destination path in the script. Below is the script that I am using. The inputAttibutes is Path Name. I have also tried using the "theFile" keyword but neither work.

framework "Foundation"
framework "AppKit"
scripting additions

set sourcePath to "/Users/UserName/Dropbox/CLLC/Icons/yosemiteicns/yose06a.icns"
set destPath to item 1 of inputAttibutes
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)

Any advice would be appreciated. Thanks for reading my post.
DoogieWall
 
Posts: 6
Joined: Thu Jun 27, 2019 3:28 pm

Re: Change Folder Image Mon Apr 17, 2023 3:42 am • by Mr_Noodle
Is this an embedded script? If so, I'm not sure if you can specify frameworks/scripting additions. Embedded scripts are actually within a handler where that may not be allowed.

Try doing an external script and see if that fares any better.
Mr_Noodle
Site Admin
 
Posts: 11250
Joined: Sun Sep 03, 2006 1:30 am
Location: New York City

Re: Change Folder Image Mon Apr 17, 2023 4:15 pm • by DoogieWall
Thank you for your suggestion. I created the following stand along apple script. Modified the hazel action to use the external script. When it runs I get the error below the code block.

Code: Select all
use framework "Foundation"
use framework "AppKit"
use scripting additions

on hazelProcessFile(theFile, inputAttributes)
   sourcePath to "/Users/DouglasWall/Dropbox/CLLC/Icons/yosemiteicns/yose06a.icns"
   set destPath to theFile
   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 hazelProcessFile


Error Log entry:

2023-04-17 16:06:05.575 hazelworker[7730] 2016: Rule Change Icon matched.
2023-04-17 16:06:05.576 hazelworker[7730] [Error] AppleScript failed: Error executing AppleScript on file /Users/douglaswall/Dropbox/CLLC/Citizens/2016.
2023-04-17 16:06:05.576 hazelworker[7730] OSAScript error: {
NSLocalizedDescription = "*** -[BAGenericObjectNoDeleteOSAID sourcePath]: unrecognized selector sent to object <BAGenericObjectNoDeleteOSAID @0x60000132ee00: OSAID(13) ComponentInstance(0x810001)>";
NSLocalizedFailureReason = "*** -[BAGenericObjectNoDeleteOSAID sourcePath]: unrecognized selector sent to object <BAGenericObjectNoDeleteOSAID @0x60000132ee00: OSAID(13) ComponentInstance(0x810001)>";
OSAScriptErrorAppAddressKey = "<NSAppleEventDescriptor: [0x0,2 \"hazelworker\"]>";
OSAScriptErrorAppNameKey = hazelworker;
OSAScriptErrorBriefMessageKey = "*** -[BAGenericObjectNoDeleteOSAID sourcePath]: unrecognized selector sent to object <BAGenericObjectNoDeleteOSAID @0x60000132ee00: OSAID(13) ComponentInstance(0x810001)>";
OSAScriptErrorMessageKey = "*** -[BAGenericObjectNoDeleteOSAID sourcePath]: unrecognized selector sent to object <BAGenericObjectNoDeleteOSAID @0x60000132ee00: OSAID(13) ComponentInstance(0x810001)>";
OSAScriptErrorNumberKey = "-10000";
OSAScriptErrorRangeKey = "NSRange: {0, 0}";

Any other suggestions? Did I do correctly what you had originally suggested?
DoogieWall
 
Posts: 6
Joined: Thu Jun 27, 2019 3:28 pm

Re: Change Folder Image Tue Apr 18, 2023 2:12 pm • by Mr_Noodle
You'll have to debug the script yourself but keep in mind that "theFile" is an alias, not a path, so you may need to convert it.
Mr_Noodle
Site Admin
 
Posts: 11250
Joined: Sun Sep 03, 2006 1:30 am
Location: New York City

Re: Change Folder Image Wed Apr 19, 2023 10:03 am • by DoogieWall
Thanks for all your help. I got my applescript sorted out and it is listed below and works like a lucky charm for anyone else wanting hazel's help to change folder images:

Code: Select all
use framework "Foundation"
use framework "AppKit"
use scripting additions

on hazelProcessFile(theFile, inputAttributes)
   set sourcePath to "/Users/DouglasWall/Dropbox/CLLC/Icons/yosemiteicns/yose06a.icns"
   set destPath to POSIX path of theFile
   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 hazelProcessFile
DoogieWall
 
Posts: 6
Joined: Thu Jun 27, 2019 3:28 pm


Return to Support