Counting lines in Photoshop

I’m working on a Photoshop script that will generate a block of text at the top of a new document that will wrap over multiple lines, then place an image directly 36 pixels below it. What I can’t figure out is how to get Applescript/Photoshop to tell me how tall the wrapped lines of text are. This is a batch job, so in some cases, the text block will be short, but in others it might be several lines longer.

So I get the concept of the ‘paragraph text’ kind of text object that will wrap text blocks, but I can’t figure out how to place the image the exact spot below. Here’s the text part of my code:

tell application "Adobe Photoshop CS3"
	set doc to make new document with properties {width:500, height:500}
	set textLayer to make new art layer of doc with properties {kind:text layer}
	set kind of text object of textLayer to paragraph text
	set font of text object of textLayer to "Helvetica"
	set size of text object of textLayer to 21
	set width of text object of textLayer to 500
	set height of text object of textLayer to 200
	set position of text object of textLayer to {0, 0}
	set contents of text object of textLayer to "This is the block of text that will change length."
end tell


you can place images in Photoshop only with javascript

set The_File to POSIX path of (choose file)

tell application "Adobe Photoshop CS3"
	tell current document
		do javascript "placeFile(); function placeFile() {function cTID(s) { return app.charIDToTypeID(s); }; var desc = new ActionDescriptor(); desc.putPath( cTID('null'), new File( '" & The_File & "' )); desc.putEnumerated( cTID('FTcs'), cTID('QCSt'), cTID('Qcsa') ); var ldesc = new ActionDescriptor(); ldesc.putUnitDouble( cTID('Hrzn'), cTID('#Pxl'), 0.000000 ); ldesc.putUnitDouble( cTID('Vrtc'), cTID('#Pxl'), 0.000000 ); desc.putObject( cTID('Ofst'), cTID('Ofst'), ldesc ); executeAction( cTID('Plc '), desc, DialogModes.NO );}" show debugger on runtime error
	end tell
end tell

the image will be placed centered.
To move a layer, use this code

tell application "Adobe Photoshop CS3"
	tell current document
		translate layer "myLayer" delta x -100.0 delta y -100.0
	end tell
end tell

the values delta x and delta y are the x and y position relative to the image in pixels

Im not at my work mac at the moment but you should be able to use the “with placing” option from the “open” command in CS3&4 or something to that effect. Script listener is only required with CS2. You may be able to get the height/depth of your type contents by getting the bounds of the text layer.

Mark, CS3 has not place command or parameter at all, maybe CS4

Stefan, I don’t have it so I could not say for sure I thought it was CS3 may be its CS4.

	get bounds of textLayer

should be enough to get the height in pixels

This is from the CS3 AppleScript Guide. I can’t test so I don’t know how this works. As of CS3 you can get an Extended Version of Photoshop with additional scripting features too. But this looks like it belongs to the standard version.

open: Includes a boolean parameter smart object, which indicates whether to create a smart object around the document opened.