Copy icon from one bundle to another?

I have a script that creates another script. After the new script bundle is created I would like to copy a custom icon that I put in the original bundle to the new bundle. I tried just a basic duplicate command


	set newIconPath to ((path to me) & "Contents:Resources:AppletIcon:") as alias
	set iconDestination to (file_path & "Contents:Resources:") as alias
tell application "Finder"
		duplicate (every file of folder newIconPath) to folder iconDestination with replacing
	end tell

but that didn’t work. The error dialog show’s

my custom icon is just called applet.icns and I want it to replace the default icon in the resources folder.

Any Ideas?

Thanks,
Mark

Going on the same lines a the other script. (as you can see I prefer shell.)
I made a folder inside the Contents/Resources named Appleticon

The one thing about doing an Icon like this (AFAIK) is it does not show the icon until you duplicate the app or re-launch finder


if bugcheck then say "saved"
	set newIconPath to ((path to me) as string)
	set newIconPath to (POSIX path of newIconPath) & "Contents/Resources/Appleticon/applet.icns" as string
	set iconDestination to (file_path & "/Contents/Resources/" as string)
	do shell script "cp " & "\"" & newIconPath & "\"" & " " & "\"" & iconDestination & "\""

Whether you use Finder or a shell script, Mark, it might help if we look at what’s going on here “ particularly in relation to that first statement in your script:

set newIconPath to ((path to me) & "Contents:Resources:AppletIcon:") as alias

By default, the path to command returns an alias:

The statement then goes on to concatenate the alias with a string:

When AppleScript concatenates items of different classes, the result is usually a list:

Finally, the statement attempts to coerce this list to an alias. Unless a list is a single-item list containing a valid path, such a coercion is not possible “ and a failure occurs:

The trick is to specify the result of the path to command as Unicode text, so that the text concatenation can complete the path successfully. In fact, when using Finder to specify objects by class (file, folder, etc.), coercion of a path to alias shouldn’t be necessary.

The following suggestion (which assumes the variable file_path represents an alias reference) demonstrates some of these points:

(* script assumes that file_path is an alias *)

set newIconPath to (path to me as Unicode text) & "Contents:Resources:AppletIcon:"
set iconDestination to (file_path as Unicode text) & "Contents:Resources:"
tell application "Finder"
	duplicate files of folder newIconPath to folder iconDestination with replacing
	update file_path
end tell

mark hunte,

I’ve been learning some shell and I like it also I just don’t know it very well.:frowning:

I’ve noticed that also. If you get info on the file, highlight the icon and hit delete the icon will show up immediately. If I can’t find a better method I may try some UI scripting at the end to do it that way. May not be the smoothest approach but should get the job done.

Kai,

Thanks for that info, makes much more sense now. I figured it was something along those lines and after reading your post and looking at the error again all I can think is DUH! The error is showing me two lists. :stuck_out_tongue:

Thanks again for the help,
MT

Um… have you tried running the example I posted, Mark?

kai,

I didn’t see it until after mark hunte’s example. The “update” command works perfect! I did stick with the shell method of copying the icon since I already had it in place.

Thanks a bunch,

MT

Glad to see you found the ‘buried treasure’, Mark. :wink:

Incidentally, since it copies/moves files silently, I’d probably go for a shell approach in this situation, too. Finder just can’t resist announcing each such operation with a triumphant ‘thunk’, which can become a little irritating “ especially if there are a number of files involved.

Hi Kai,

Thanks for reminding me of ‘update’, I did not realise it affected things like icons.

You can add the update in shell also using

/usr/bin/osascript -e 'tell application "Finder" to update items of finder windows'