3

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)

GilbertOOl
  • 1,299
  • 2
  • 15
  • 27

2 Answers2

5

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
adamh
  • 3,222
  • 1
  • 20
  • 16
  • Thank you for your help! For this to work, i would have to copy and paste the .icns file in the parent folder automatically with the script, is it possible? – GilbertOOl Nov 24 '13 at 10:38
  • Sure no probz, or you could keep your .icns in a fixed place and just use an absolute path to it. The first 3 lines inside the tell are just dealing with the paths, the do shell script is the crucial part and you can pass in what ever paths you need to it. – adamh Nov 24 '13 at 11:00
  • Thanks a lot ! (see my profile, another question about Applescript ;-) ) – GilbertOOl Nov 24 '13 at 11:16
  • 1
    Project link is broken. This appears to be the same one: https://github.com/vasi/osxutils – Justin Putney Nov 08 '19 at 18:17
  • SWEET. I found the binary executable from here https://github.com/sveinbjornt/osxiconutils (archive.org: https://archive.org/details/osxiconutils) and was able to successfully change the icon of the very script running the code! Now I can set a cusom icon for my .scpt on other Macs without having to first package it as an .app or .dmg and sign it with certificates and expiration dates and GateKeeper whatever and all that vague annoying BS – velkoon Mar 02 '23 at 07:46
  • Never mind...Of course downloading the .zip of the script + seticon binary from the internet still makes Mac freak out due to the seticon binary inside...."developer cannot be verified" – velkoon Mar 02 '23 at 08:14
  • Sorry for the triple-post, but https://apple.stackexchange.com/a/198714/272654 I was able to make this guy's script an executable on-the-fly with the AppleScript (`chmod +x`) so the binary never gets flagged by GateKeeper, because it's downloaded as a plaintext file from the internet! woo! – velkoon Mar 02 '23 at 08:54
0

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

Ronald Hofmann
  • 1,390
  • 2
  • 15
  • 26