Duplicate custom icons between folders

I want to copy a custom icon from one to another folder and made this script.
However, Finder claims that file1 (invisable icon file in the first folder) cannot be found. File “.DS_Store”,also invisable, however CAN be found.

What am I doing wrong…?

tell application “Finder”
set aFold to choose folder “Select folder to copy icon”
set bFold to choose folder “Select folder to transfer to”
set aFold to aFold as string
set file1 to (aFold as string) & “.DS_Store”
set file2 to (aFold as string) & “Icon”
copy alias file1 to bFold
copy alias file2 to bFold
end tell


Eelco Houwink

As you ask to copy the custom icon applied to a given folder to an other folder you may use :

# Use a CLI available from : http://www.bluem.net/en/mac/cliclick/

# Edit these three instructions to fit your needs

set p2d to path to desktop as text
set theSource to (p2d & "source:") as alias
set theTarget to (p2d & "target:") as alias

tell application "Finder"
	select theSource
	tell me to set the clipboard to "" # an old habit
	tell application "System Events" to tell process "Finder"
		set frontmost to true
		keystroke "i" using {command down} # open infos window
		set infoWindow to window 1 --> "Infos sur source"
		tell infoWindow
			--class of UI elements -->{scroll area, button, button, button, image, static text}
			tell scroll area 1
				class of UI elements --> {image, static text, static text, static text, static text, scroll area, UI element, static text, static text, static text, static text, static text, static text, static text, static text, static text, static text, static text, checkbox, checkbox, UI element, static text, static text, static text, UI element, static text, text field, checkbox, UI element, static text, scroll area, UI element, static text, UI element, static text, static text, scroll area, button, button, menu button, checkbox, scroll bar}
				
				set {xLeft, yTop} to position of image 1
				set {w, h} to its size of image 1
				tell me to do shell script "/Users/admin/bin/cliclick c:" & xLeft + w div 2 & "," & yTop + h div 2
				delay 0.1
				keystroke "c" using {command down}
			end tell
		end tell
	end tell
	
	select theTarget
	
	tell application "System Events" to tell process "Finder"
		set frontmost to true
		keystroke "i" using {command down} # open infos window
		set infoWindow to window 1 --> "Infos sur target"
		tell infoWindow
			--class of UI elements -->{scroll area, button, button, button, image, static text}
			tell scroll area 1
				class of UI elements --> {image, static text, static text, static text, static text, scroll area, UI element, static text, static text, static text, static text, static text, static text, static text, static text, static text, static text, static text, checkbox, checkbox, UI element, static text, static text, static text, UI element, static text, text field, checkbox, UI element, static text, scroll area, UI element, static text, UI element, static text, static text, scroll area, button, button, menu button, checkbox, scroll bar}
				
				set {xLeft, yTop} to position of image 1
				set {w, h} to its size of image 1
				tell me to do shell script "/Users/admin/bin/cliclick c:" & xLeft + w div 2 & "," & yTop + h div 2
				delay 0.1
				keystroke "v" using {command down}
			end tell
		end tell
	end tell
end tell # Finder

CAUTION: the custom icon will disappear if you apply the command update to the target folder.

Just for info, the name of the custom icon file is not “Icon” but it’s “Icon” & return

I tried to run:

tell application "Finder"
	set aFold to choose folder "Select folder to copy icon"
	set bFold to choose folder "Select folder to transfer to"
	set aFold to aFold as string
	set file1 to aFold & ".DS_Store"
	set file2 to aFold & "Icon" & return
	try
		duplicate alias file1 to bFold # Here ".DS_Store" is not available
	end try
	duplicate alias file2 to bFold
end tell

The file whose name is “Icon” & return is correctly duplicated but the Finder doesn’t use it as icon.

Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) mercredi 31 juillet 2019 16:57:57

Here’s an AppleScriptObjC solution:

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 copy icon")
set bFold to POSIX path of (choose folder "Select folder to transfer to")
set ws to current application's NSWorkspace's sharedWorkspace()
set theImage to ws's iconForFile:aFold
ws's setIcon:theImage forFile:bFold options:0

There’s one potential down-side: the new image will be a tiny bit lighter than the original on a retina screen. The difference is pretty minor, but it accumulates if you repeatedly copy from one folder another.

Thank you, Shane, for nice solution. Goes to the library.

Thank you Shane.

Is it a way to achieve the same when the source is a file ?

I tried to use:

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

set aFile to POSIX path of (choose file "Select file to copy icon")
set bFold to POSIX path of (choose folder "Select folder to transfer to")
set ws to current application's NSWorkspace's sharedWorkspace()
set theImage to ws's iconForFile:aFile
ws's setIcon:theImage forFile:bFold options:0

but I get the generic icon because the Finder is set to display the pictures as icon.
May we use the content of the file to do the job?

Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) jeudi 1 août 2019 11:04:25

Sure:

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

set aFile to POSIX path of (choose file "Select file to copy icon")
set bFold to POSIX path of (choose folder "Select folder to transfer to")
set ws to current application's NSWorkspace's sharedWorkspace()
set theImage to current application's NSImage's alloc()'s initWithContentsOfFile:aFile
ws's setIcon:theImage forFile:bFold options:0

Thank you Shane.

I was afraid that the datas returned by

set theImage to current application’s NSImage’s alloc()'s initWithContentsOfFile:aFile

was not in the correct structure.

I assume that the icon installed this way will not resist to tell application “Finder” to update bfold.

Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) jeudi 1 aout 2019 16:28:50

That’s right. I don’t see any way around that Finder bug (other than everyone logging bugs and someone at Apple fixing it).