set Icon to file/folder with Catalina "Bad CPU type in executable"

Hi,
Fortunately, Catalina didn’t break many of my applescripts.
But I had found a solution to copy the icon of a folder to another, using a shell script and now it doesn’t work anymore (error “Bad CPU type in executable”).

tell me
	set AppleScript's text item delimiters to ""
				set src to MyPath_src
				set src to POSIX path of src
				set dst to POSIX path of MyPath_dest
				set srcicon_ to quoted form of (src & "Icon" & return)
				set dst_ to quoted form of dst
				
				set sh1 to "ditto --rsrc --extattr " & srcicon_ & " " & dst_ -- copy icon file to dst
				tell application "Finder" to set sh2 to (POSIX path of (folder of (path to me) as string))
				set sh2 to sh2 & "SetFile -a C " & dst_ -- set custom icon flag of dst
				do shell script (sh1 & " && " & sh2)

end tell

It now gives me the error “Bad CPU type in executable”.
It seems that the “SetFile” executable is 32-bit, whereas Catalina is 64-bit only.

Is there another method to achieve this (copy icon from one folder to another) ?
Or is is possible to convert the “SetFile” executable in 64 bits easily ?

Thanks for your answer !

Hey,
I reply to myself as I’ve found the solution.

The original script was found on this page :
https://discussions.apple.com/thread/2057278

The command “SetFile” used is from Apple, so I just installed the new command line tools :
https://osxdaily.com/2014/02/12/install-command-line-tools-mac-os-x/

And the script now works using the new “SetFile” command in 64-bits (I guess).

I’m using the code below to attach the contents of a picture file as icon of a given folder.

use framework "Foundation"
use scripting additions

set sourcePath to choose file of type {"icns", "jpeg", "jpg", "png", "tiff"}
set target to (path to desktop as text) & "nouveau"

my setIcon(target, sourcePath)

to setIcon(fileRef, imagePath)
	set iconImage to current application's NSImage's alloc's initWithContentsOfFile:(POSIX path of imagePath)
	current application's NSWorkspace's sharedWorkspace's setIcon:iconImage forFile:(POSIX path of fileRef) options:0
end setIcon

Alas, it fails if sourcePath points to the file “Icon” & return stored in an other folder.

Maybe somebody will be able to edit it.

Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) mardi 31 mars 2020 22:26:23

I don’t think so. You shouldn’t read the icon file directly – instead, use this on the file showing the image:

set theImage to current application's NSWorkspace's sharedWorkspace's iconForFile:posixPath

Thank’s Shane

When I tested I was afraid because the icon didn’t appear immediately on the target folder but it did a few seconds later.

It behaves faster when the icon isn’t borrowed from a folder but from a file.

use framework "Foundation"
use scripting additions

-- set sourcePath to (path to desktop as text) & "beurk.txt" -- a text file with an icon
set sourcePath to (path to desktop as text) & "beurk" -- a folder
set target to (path to desktop as text) & "nouveau"

my setIcon(target, sourcePath)

to setIcon(targetPath, sourcePath) -- sourcePath may point to a file or a folder
	set iconImage to current application's NSWorkspace's sharedWorkspace's iconForFile:(POSIX path of sourcePath)
	current application's NSWorkspace's sharedWorkspace's setIcon:iconImage forFile:(POSIX path of targetPath) options:0
end setIcon

Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) mercredi 1 avril 2020 09:57:53

Right now I use the following script to change AppleScript app icons, and it seems to work fine. I obtained the ASObjC portion from another thread (which I can’t find now), and I wondered if there’s a better way to do the ASObjC part. Thanks.

use framework "Foundation"
use scripting additions

set theFile to POSIX path of (choose file)

set theIcon to POSIX path of (choose file of type {"icns", "jpeg", "jpg", "png", "tiff"})

set ws to current application's NSWorkspace's sharedWorkspace()
set theImage to current application's NSImage's alloc()'s initWithContentsOfFile:theIcon
ws's setIcon:theImage forFile:theFile options:0

That’s essentially the same code as Yvan provided above.