set icon of folder x to icon of folder y

Dear Forum,

I am definitely not an AS evangelist, but I would like to be able to change/set by script a folder icon.

Here is what I have:

  1. there is the original folder with a custom icon placed on my NAS (APF connected)
  2. An alias of that folder is located on my desktop. After a restart of either my iMac (11.3.1) or my NAS (QNAP TS 4.5.3 Pro) the folder doesn’t show the custom icon anymore, but only the generic blue folder icon.

I can manually correct this by copy’n-paste or drag’n-drop but I wonder if there is aa way to do this by a script.

Here is what I thought might work but obviously doesn’t:

tell application “Finder”
activate
set originalFolder to “afp://myNAS(AFP)._afpovertcp._tcp.local/foldername” – path to original folder
set aliasFolder to “/Users/iMac/Desktop” – path to alias folder
set icon of aliasFolder to icon of originalFolder
end tell

What am I doing wrong? Is it possible at all?

Thanks for your time and help,
Ralf

Model: iMac 27" 2019, 11.3.1 (Big Sur)
AppleScript: 2.7 (Editor 2.11 (225)
Browser: Safari 14.1 (16611.1.21.161.6)
Operating System: macOS 10.14

Setting icon property of the folder of Finder.app isn’t available yet. You can use instead NSWorkspace class of AsObjC:


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

set originalFolder to "afp://myNAS(AFP)._afpovertcp._tcp.local/foldername" -- path to original folder
set aliasFolder to "/Users/iMac/Desktop/foldername" -- path to alias folder
set ws to current application's NSWorkspace's sharedWorkspace()
set theImage to ws's iconForFile: originalFolder
ws's setIcon:theImage forFile: aliasFolder options:0