Photoshop, Layers, and "with data"

I’ve got a basic understanding of Applescript, and have scoured the web and the Photoshop Applescript Guide. All I want to do is add an image into a new document as a layer. Here’s what I have tried:

on open these_items
	set first_item to item 1 of these_items
	repeat with i from 1 to the count of these_items
		set this_item to (item i of these_items)
		set the item_info to info for this_item
		if folder of the item_info is true then
			-- do nothing
		else if (alias of the item_info is false) then
			tell me to process_item(this_item, i)
		end if
	end repeat
end open

on process_item(theFile, isFirst)
	tell application "Adobe Photoshop CS3"
		activate
		open theFile
		set docRef to current document
		set docN to the name of docRef
		set docW to the width of docRef
		set docH to the height of docRef
		set docR to the resolution of docRef
		set docM to the mode of docRef
		set docB to the bits per channel of docRef
		if isFirst is 1 then
			global layerDoc
			set layerDoc to make new document with properties ¬
				{width:docW, height:docH, resolution:docR, mode:docM}
			set bits per channel of layerDoc to docB
			set background layer of layer 1 of layerDoc to false
		end if
		set newLayer to make new art layer at beginning of layerDoc with properties {name:docN} with data docRef ***
		close docRef saving no
	end tell
end process_item

*** This was my best guess at how to do this, but it does not work. Plugging in an alias doesn’t work either. I can’t find anything about the “with data” parameter except this from the Photoshop Applescript Guide:
“[with data anything] Any data needed for creation that is not a property.” What the @#$% does that mean?

Model: MacBookPro
Browser: Firefox 2.0.0.11
Operating System: Mac OS X (10.4)

Can someone at least give me an idea of what a “with data” field would normally have in it?

Why aren’t you just setting the background layer of layer 1 to false and save in new file? or have I misunderstood your intentions?

I’m not sure what that feature is for but my guess would be to import text not an image. You should be able to copy over the layer that you want from the source document to the new document with both of them open. I forget the exact syntax but it is not that hard to do. The main thing that can trip you up is having the correct document active when you copy the layer over, using duplicate if I recall. When I have time later I will see if I can get you some sample code.

This is what I want to do.

Can anyone give me any code for moving a layer from one document to another in Photoshop?

Something I’m trying after a tip on another forum on how to do it in Javascript:


// docRef is the file the user has open when the script starts
var docRef = app.activeDocument;

// docLogos is the 2nd file that the script opens
var docLogos = open(logofile);

// duplicate the layer "logo" into the users document
docLogos.artLayers[logo].duplicate(docRef);

// close the 2nd document
docLogos.close(SaveOptions.DONOTSAVECHANGES);

In AppleScript?:


open theFile
set docRef to current document
set layerDoc to make new document
duplicate layer 1 of docRef to layerDoc
close docRef saving no

I have yet to try it, but should this work?

Sorry it’s been crazy at work this week. What you have should work, here is some simple code that works as a reference:

tell application "Adobe Photoshop CS2"
	set x to document 1
	set y to document 2
	duplicate layer 1 of x to y
end tell

Which is document 1, 2, 3 … Does it go by order opened?

Yes I PS is keeping a list of documents in the order that they were opened or created. You could also reference the document by name and probably a few other properties that are document specific. Current Document returs the active document.

Maybe not a final version (it might need some dummy-proofing), but here’s what I have come up with:


on open these_items
	set item_count to the count of these_items
	-- the loop
	repeat with i from 1 to item_count
		set this_item to (item i of these_items)
		set the item_info to info for this_item
		if (alias of the item_info is false) and (folder of the item_info is false) then
			set continue_loop to my process_item(this_item, i)
		end if
		-- error reporting
		if continue_loop is false then
			tell me to activate
			display dialog "You must drop only image files." giving up after 30
			return
		end if
	end repeat
end open

on process_item(theFile, theNumber)
	tell application "Adobe Photoshop CS3"
		if theNumber is 1 then
			-- make the first file the bottom layer
			global layerDoc
			activate
			try
				open theFile showing dialogs never
			on error
				return false
			end try
			set layerDoc to the current document
			set docN to my trimExtension(name of layerDoc)
			set the name of the background layer of layerDoc to docN
		else
			-- after the first file
			try
				open theFile showing dialogs never
			on error
				return true
			end try
			set docRef to the current document
			set docN to my trimExtension(name of docRef)
			-- the new layer
			duplicate the background layer of docRef to layerDoc
			close docRef saving no
			set the name of the layer 1 of layerDoc to docN
		end if
		return true
	end tell
end process_item

on trimExtension(fileName)
	set dot to offset of "." in fileName
	set newname to text 1 thru (dot - 1) of fileName
	return newname
end trimExtension