GilbertOOl
GilbertOOl

Reputation: 1309

Change icon of folder with AppleScript?

i've create an AppleScript very helpful to me and i wish if it is possible to automatically change the folder icon.

This script is very simple, it create one folder, then create one empty text file in the same folder.

Here is the script:

tell application "Finder"
    set newfolder to make new folder with properties {name:My Folder}
    make new file at newfolder with properties {name:"read_me.txt"}
end tell

Is it possible to automatically change the folder icon?

(I own my custom folder icon (.icns) in the same folder as the script, of course)

Upvotes: 3

Views: 4777

Answers (2)

Ronald Hofmann
Ronald Hofmann

Reputation: 1430

My Solution in 2021, using SF-Symbols

-- Help
-- folder_path  : The folder whose icons will be changed (multiple selection in Finder possible)

-- icon_path    : The Icon path
-- archiv_path  : The Icon path for a folder named "Archiv"

-- Presets here as used by me
property folder_path : "/Users/ronny/Desktop/osxutils-master"
property icon_path : "/Volumes/Development/Developer/Xcode/LibPool/Icons/Folder.icns"
property archiv_path : "/Volumes/Development/Developer/Xcode/LibPool/Icons/Archiv.icns"

-- Frameworks and Additions
use framework "Foundation"
use framework "AppKit"
use scripting additions

-- Create list (array) with selected items
-- Be carefull, every icon will be changed

tell application "Finder"
    set theListe to selection as list
end tell

repeat with i in theListe

    set aName to name of i
    log (aName)

    set destPath to POSIX path of (i as text)

    if aName is equal to "Archiv" then
        set sourcePath to archiv_path
    else
        set sourcePath to icon_path
    end if

    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 repeat

enter image description here

Upvotes: 0

adamh
adamh

Reputation: 3292

Heres a solution that uses a command line utility "seticon" found in this package: https://github.com/vasi/osxutils

It works on the assumption your script, icns file and new folder are all in the same directory.

tell application "Finder"
    set parent_path to ((the container of the (path to me)) as text)
    set target_folder_path to quoted form of POSIX path of (parent_path & "my folder")
    set icon_path to quoted form of POSIX path of (parent_path & "icon.icns")
    do shell script "/usr/local/bin/seticon -d " & icon_path & " " & target_folder_path
end tell

Upvotes: 5

Related Questions