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
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.
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.
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
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.
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.
Right click on this .scptd-bundle to ShowPackageContents window, go to Resources folder of Contents folder.
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
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:
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:
Assuming you named the bundle “setCustomIconForFolder”
Store it inside Scripts folder of Library of user domain.
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.