Change icon of folder with AppleScript?

2020-07-25 09:50发布

问题:

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)

回答1:

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


标签: applescript