Convert image to PNG then add to icon

I’ve collected some previous scripts together, and of course I can’t get them to work. No errors just not behaving as I hoped.

The photoshop part works, but I need it to paste itself to the original file that was opened.
Like before I don’t think I am reference the original file correctly.

(*

PASTE AS ICON.
                                                        

*)

set tempFolder to (path to pictures folder as text)
tell application "Adobe Photoshop CS5.1"
	-- I remove the command activate, Photoshop stay in background
	set ruler units of settings to pixel units
	try
		
		set origName to name of current document
		
		set myPNGOptions to {class:PNG save options, interlaced:false}
		tell current document
			--If the quick mask mode has been left on then delete the channel Quick Mask
			if (quick mask mode) then delete channel ¬
				"Quick Mask"
			--If the Layer is incorrectly labeled with Original Layer it needs renaming to original Image
			if (exists layer "Original Layer") then ¬
				tell layer "Original Layer" to set name to "Original Image"
			
			(delete layer "Original Image")
			set current layer to layer "BACKGROUND" of layer set "Dodge and Burn"
			delete current layer
			merge visible layers
			trim basing trim on top left pixel with top trim, right trim, left trim and bottom trim
			
			resize image height 1024
			save in (tempFolder & "temporaryPNG") as PNG with options myPNGOptions without copying
			close saving no
			
		end tell
	end try
end tell




tell application "Finder"
	
	set tImage to (tempFolder & "temporaryPNG.png")
	my setIcon((result as alias)'s POSIX path, tImage)
end tell



on setIcon(f, imagef)
	(*
        string f : POSIX path of target file or folder
        string imagef : POSIX path of image file
    *)
	set rb to "require 'osx/cocoa'
include OSX
raise ArgumentError, %Q[Usage: $0 <file> <image file>] unless ARGV.length == 2
file, imagef = ARGV
image = NSImage.alloc.initWithContentsOfFile(imagef)
NSWorkspace.sharedWorkspace.objc_send(
    :setIcon, image,
    :forFile, file,
    :options, 0)
"
	do shell script "/usr/bin/ruby -e " & rb's quoted form & " " & f's quoted form & " " & imagef's quoted form
end setIcon








the setIcon routine says “string imagef : POSIX path of image file”, so you need a POSIX path for both parameters.
In this case you can pass the same parameter twice

set tImage to POSIX path of tempFolder & "temporaryPNG.png"
setIcon(tImage, tImage)

The Finder is not needed at all