Automating Changing a Folder Icon Color

Is there a way to automate changing folder color using AppleScript? I tried using ChatGBT but it kept running into errors.

The manual steps are as follows:

  1. First, open the folder that you want to change the color of.
  2. Right-click on the folder and select “Get Info” from the context menu.
  3. In the Info window that appears, click on the folder icon at the top-left corner of the window.
  4. From the menu bar, click on “Edit” and select “Copy” to copy the folder icon.
  5. Open the Preview app on your Mac and select “New from Clipboard” from the “File” menu.
  6. From the menu bar, select “Tools” and then “Adjust Color.”
  7. In the Adjust Color window, use the sliders to adjust the color of the folder icon as desired.
  8. Once you’re happy with the color, click on “File” and select “Save.”

I created a video here: How to Change Folder Color macOS (Monterey) 2023 - YouTube

AppleScript+Cocoa is more powerful than copy-and-paste method.

Set custom icon to selected folder script
http://piyocast.com/as/archives/899

Remove custom icon from selected folder script
http://piyocast.com/as/archives/901

2 Likes

How do I use them together? Is there a good tutorial on how to create the folders using the above languages?

Both scripts are AppleScript.
I don’t understand the meaning of “tutorial”.

(1) Do you want to read AppleScript language tutorial?
(2) Do you want to read or watch guidance video how to execute these script?
(3) Do you want to know about these AppleScript’s technical background (Cocoa Scripting)?
(4) Other

2 Likes

Judging from the name, the scripts are not meant to create folders, but to set/remove a custom icon from a selected folder. Makes sense, since you explicitly asked for changing folders, not for creating them.

Some steps from 1-8 can be automated, but others will require GUI scripting, making the script useless for other versions of Preview.

Instead, you will have to use a pure ASOC script without the participation of Preview and the clipboard. The following simple script is not what you are asking. It just gives a filled icon, but gives you some idea.

use AppleScript version "2.4"
use scripting additions
use framework "Foundation"
use framework "AppKit"

set aFold to POSIX path of (choose folder "Select folder to change icon")

set ws to current application's NSWorkspace's sharedWorkspace()
set anImage to ws's iconForFile:aFold

set aColor to current application's NSColor's colorWithRed:255 green:2 blue:255 alpha:1.0
anImage's lockFocus()
aColor's |set|()
set aRect to current application's NSMakeRect(0, 0, anImage's |size|()'s width(), anImage's |size|()'s height())
current application's NSBezierPath's fillRect:aRect
anImage's unlockFocus()

ws's setIcon:{anImage} forFile:aFold options:0

.
To replicate what you’re doing in Preview, a real ASOC script needs to change saturation, brightness, and so on. This is what ASOC can do, and maybe I or someone else will publish it.

You can select a harder or easier course.

(1) Harder course

Script: Remove custom icon from folder
Script: Get original icon image from folder
Script: Change icon image color by using CIFilter or other services or Adobe Photoshop/Pixelmator Pro (GUI Apps)
Script: Set custom colored image to target folder

(2) Easier Course (Recommend)

Create several colored folder icon by yourself
Put custom colored icons within your script bundle (ex. Resources folder)

Script: Set some custom colored icon image to target folder

There is a lot of custom color folder icons. You can find them via Google.

2 Likes

This “a” gave me the existence of wisdom. Though I don’t feel wisdom or intelligence with English language, this “a” is a shine of something good sense. >CC

1 Like

Can you provide a script for the easy course?

Is there anything I should do to add ASOC script? I get the error below when I just run it.

I’m a little lost here.

As I checked, the code works with most folders. But some folders stubbornly refuse to accept the new icon. So far I can’t figure out how Mac OSX makes the difference.

As I checked again, the error occurs with folders whose permissions is broken.

Restoring permissions with Disk Utility should help. Just reboot into recovery mode and repair permissions.

If this does not help with some folder, I recommend 1) create a new “Untitled Folder” folder using Finder, 2) copy the contents of the problematic folder to a new one, 3) delete the problematic folder after copying its name, 4) rename the new folder to the old name.

Almost this script.

http://piyocast.com/as/archives/899

This sample script use “choose file” command for sample usage.
You heve to modify this into each fixed image file include within the script bundle.

Contents/Resources/redfolder.png
Contents/Resources/bluefolder.png
Contents/Resources/yellowfolder.png
Contents/Resources/greenfolder.png
Contents/Resources/purplefolder.png
Contents/Resources/whitefolder.png
Contents/Resources/blackfolder.png

Please excuse my stupidity…

Where do I insert the image folders?

Step 1 : Make new AppleScript document & copy the script contents to it.
Step 2: Save new AppleScript document as “Script Bundle”. Not script.
Step 3: Click “Show or Hide bundle contents” button right edge of the toolbar button area on your window
Step 4: Drag & drop image files to “Resources” area
Step 5: Save the Script.
Step 6: You can get each image path by “path to resource” command.

  1. Download some icons from web in Mac .icns format.
  2. Save following script as script bundle (.scptd)
  3. Right click on this .scptd-bundle to ShowPackageContents window, go to Resources folder of Contents folder.
  4. Copy or drag the downloaded .icns files into Resources folder.

Now, you can use this script bundle running it any time.
.

-- Created 2015-10-21 by Takaaki Naganoya
-- Adapted 2023-4-25 by KniazidisR
-- 2015 Piyomaru Software

use AppleScript version "2.4"
use scripting additions
use framework "Foundation"
use framework "AppKit"

set appIconResources to ((path to me as text) & "Contents:Resources:") as alias
set icnsPosixPath to POSIX path of (choose file of type "com.apple.icns" default location appIconResources with prompt "Choose Icon File")
set folderPosixPath to POSIX path of (choose folder with prompt "Choose Folder")
setCustomIconForFolder(icnsPosixPath, folderPosixPath) of me

on setCustomIconForFolder(icnsPosixPath, folderPosixPath)
	set ws to current application's NSWorkspace's sharedWorkspace()
	set theImage to current application's NSImage's alloc()'s initWithContentsOfFile:icnsPosixPath
	ws's setIcon:theImage forFile:folderPosixPath options:0
end setCustomIconForFolder
1 Like

I created a script bundle with icon files inside the resource folder. Nothing happens except it says true at the end. Please watch the video I created:

The fileicon cli worked. Is there a way to custom label more than one file at a time?

You want create Automator workflow or app? When you run it from Automator the path to me is the path to Automator.app’s bundle instead of script bundle’s path. To avoid this behaviour:

  1. Assuming you named the bundle “setCustomIconForFolder”
  2. Store it inside Scripts folder of Library of user domain.
  3. The script, adapted for Automator:
-- Created 2015-10-21 by Takaaki Naganoya
-- Adapted 2023-4-25 by KniazidisR
-- 2015 Piyomaru Software

use AppleScript version "2.4"
use scripting additions
use framework "Foundation"
use framework "AppKit"
property scriptsPath : path to scripts folder from user domain as text

set appIconResources to (scriptsPath & "setCustomIconForFolder.scptd:Contents:Resources:") as alias
set icnsPosixPath to POSIX path of (choose file of type "com.apple.icns" default location appIconResources with prompt "Choose Icon File")
set folderPosixPath to POSIX path of (choose folder with prompt "Choose Folder")
setCustomIconForFolder(icnsPosixPath, folderPosixPath) of me

on setCustomIconForFolder(icnsPosixPath, folderPosixPath)
	set ws to current application's NSWorkspace's sharedWorkspace()
	set theImage to current application's NSImage's alloc()'s initWithContentsOfFile:icnsPosixPath
	ws's setIcon:theImage forFile:folderPosixPath options:0
end setCustomIconForFolder

.
By the way, in Automator you can create a great service for this task. Once created the service, you right-click directly on the folder (in the Finder), choose and start the service. It remains only to choose an icon.

I’m not sure what to do in this step. What is the library of user domain?

These are the only script folders I have: