• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

How to get all layer coords, sizes, and move them?

New Here ,
Jul 18, 2007 Jul 18, 2007

Copy link to clipboard

Copied

I just downloaded the SDK and it seems like a vast amount of information for something pretty simple that I need to do.

I need to get all the sizes and coordinates/location refs of all layers (in reference to the document they are placed in) move them, and to output it all to txt/xml.

Basically I'm making an image/sprite packer plug-in similar to this but integrated will photoshop: http://homepage.ntlworld.com/config/imagepacker/

I'll probably end up using an action/scripting for the non-essential parts so all I need is the layer/movement refs for working with my packing algorithms.

Any help would be very much appreciated, even if it is just pointing out where to find this info in the SDK's PDFs, or kicking some code my way ;), it would be great.
TOPICS
SDK

Views

835

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe
Explorer ,
Jul 18, 2007 Jul 18, 2007

Copy link to clipboard

Copied

James_Caldwell@adobeforums.com wrote:
> I need to get all the sizes and coordinates/location refs of all layers (in reference to the document they are placed in) move them, and to output it all to txt/xml.

You can do all of this easily enough with JavaScript. If you have large numbers
of layers (>>500) it may slow a bit if you code via the DOM, but you can always
drop down to lower-level code if you really need every last bit of performance
and want to stay in JS.

-X

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Jul 18, 2007 Jul 18, 2007

Copy link to clipboard

Copied

Yes, but does javascript support FIO streams for saving/reading txt files in photoshop?

The only way I thought this would be possible was if there was some way to script custom log files, but it sounded too hackish to me.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Jul 18, 2007 Jul 18, 2007

Copy link to clipboard

Copied

James_Caldwell@adobeforums.com wrote:
> Yes, but does javascript support FIO streams for saving/reading txt files in photoshop?

The JavaScript in Adobe CS* products has fairly good File class supporting IO
plain ASCII, UTF, binary, and other encodings. I have libs/APIs for doing binary
IO for stuff like BE/LE words and IEEE754 floating point.

Search for the JavaScript Tools Guide for CS3 on the adobe site for docs on the
File and Folder classe. It's probably under devnet/bridge/

-X

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Jul 19, 2007 Jul 19, 2007

Copy link to clipboard

Copied

Okay, I'm looking into scripting right now, but if I wanted this plug-in to be 7.0 compatible I would need to use C++ because Bridge was introduced with CS2, right?

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Jul 19, 2007 Jul 19, 2007

Copy link to clipboard

Copied

James_Caldwell@adobeforums.com wrote:
> Okay, I'm looking into scripting right now, but if I wanted this plug-in to be 7.0 compatible I would need to use C++ because Bridge was introduced with CS, right?
No.
First off, scripts aren't plug-ins. It's a completely different programming model.
Second, the docs for File/Folder in CS3 are in Tools Guide which is provided
with Bridge (which is provided with PSCS3). File/Folder are available is PSCS3
with or without Bridge.
Third, you are fine going back to PS7. That's when scripting was introduced.
There are some quirks (BINARY encoding is not directly support, though I do have
a workaround), but it is easy enough to write scripts that are compatible from
PS7 through to CS3 provided you don't need any UI. If you need UI, you can only
go back to PSCS.

-X
--
for photoshop scripting solutions of all sorts
contact: xbytor@gmail.com

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Jul 19, 2007 Jul 19, 2007

Copy link to clipboard

Copied

Right C++ is for the plug-in and scripts are run in the PS 7.0 with a plug-in, but I was trying to decide which route to go.

I have the SDK, but I'll just go with javascript for now.

I don't need any UI, if worst comes to worst I can just make different scripts most likely.

BTW, I can't test this at the moment as I don't have PS here, but maybe you can let me know if I'm doing anything wrong so far:

In javascript:

//====================================================

//Retrieving Layer data:

// ref acitve doc's top layer:
var my_layer = app.activeDocument.Artlayers[0];

var my_layer_bounds = my_layer.bounds; // bounds returns an array of x, y, w, h

var my_layer_height = my_layer_bounds[3] //ref the layer height?

var my_string = "" + my_layer_height; //put the height into a string?

//move the layer 100 pixels up and 100 pixels to the right:
my_layer.translate(100, 100);

//File Writing:

//create a new fileObj named file_name.txt:
var my_file = new fileObj("file_name.txt");

//write 3 strings on 3 lines:
my_file.write(my_string ("string_2" , "string_3"));

my_file.close();

//=====================================================

Also, doesn't Adobe have a free IDE for scripting Photoshop? Do you know where I can get/find it?

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Jul 19, 2007 Jul 19, 2007

Copy link to clipboard

Copied

James_Caldwell@adobeforums.com wrote:
>
> //Retrieving Layer data:
>
> // ref acitve doc's top layer:
> var my_layer = app.activeDocument.Artlayers[0];
should be
var my_layer = app.activeDocument.artLayers[0];

The description in the docs sometimes seems a bit confused WRT capitalization.
Classes and constants are capitalized, methods and properties aren't.

>
> var my_layer_bounds = my_layer.bounds; // bounds returns an array of x, y, w, h
>
> var my_layer_height = my_layer_bounds[3] //ref the layer height?

Nope.
var my_layer_height = my_layer_bounds[3]-my_layer_bounds[1];

>
> var my_string = "" + my_layer_height; //put the height into a string?
>
> //move the layer 100 pixels up and 100 pixels to the right:
> my_layer.translate(100, 100);

This goes down and to the right. The upper left corner is 0,0 in PS.

>
> //File Writing:
>
> //create a new fileObj named file_name.txt:
> var my_file = new fileObj("file_name.txt");
>
More like:
var my_file = new File("/c/file_name.txt");

You need a my_file.open("w");

> //write 3 strings on 3 lines:
> my_file.write(my_string ("string_2" , "string_3"));

my_file.writeln(my_string + "\nstring_2\nstring_3"));

>
> my_file.close();
>
> //=====================================================
>
> Also, doesn't Adobe have a free IDE for scripting Photoshop? Do you know where I can get/find it?

It comes bundled with CS2 and CS3.

If you have any other questions, I suggest asking over on the PS scripting forum.
-X
--
for photoshop scripting solutions of all sorts
contact: xbytor@gmail.com

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Jul 19, 2007 Jul 19, 2007

Copy link to clipboard

Copied

okay, ignore the above...

This is what I got now:

//====================================================

//Retrieving Layer data:

// ref acitve doc's top layer:
var my_layer = activeDocument.artLayers[0];

var my_layer_bounds = my_layer.bounds; // bounds returns an array of x, y, w, h

var my_layer_height = "my_layer_bounds[3]"; //ref the layer height?

var my_string = "" + my_layer_height; //put the height into a string?

//move the layer 100 pixels up and 100 pixels to the right:
my_layer.translate(0.01, -0.01);

//File Writing:

my_file = new File(path.toString() + "my_ps7.txt");
//write 3 strings on 3 lines:
my_file.write("my_ps7.txt", "string_3");

my_file.close();

//=====================================================

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Jul 19, 2007 Jul 19, 2007

Copy link to clipboard

Copied

Hey thanks a lot, I actually got all of the file writing working because of your getter example on your PS script forum.

Do you prefer using that forum?

If so I'll just move over there.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Jul 19, 2007 Jul 19, 2007

Copy link to clipboard

Copied

James_Caldwell@adobeforums.com wrote:
> Do you prefer using that forum?

For JS stuff, yep.

>
> If so I'll just move over there.

I'm surprised Chris C. hasn't chased over there already :)

-X

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Jul 19, 2007 Jul 19, 2007

Copy link to clipboard

Copied

Oh, btw I meant your execute example, not your getter.

Alright, I moved my posts over here at least until I get activated by your people.

http://www.adobeforums.com/cgi-bin/webx/.3bbf2765.3bc48105

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Jul 20, 2007 Jul 20, 2007

Copy link to clipboard

Copied

I wrote a program (<a href="http://www.telegraphics.com.au/sw/#psdparse">psdparse</a>) which extracts all this layer information from a PSD on disk, to XML if you wish (Photoshop is not required). Sample output:<br /><br /><pre><br />$ ./psdparse ../testpsd/slashtest.psd --xmlout<br /><?xml version="1.0"?><br /><PSD FILE='../testpsd/slashtest.psd' VERSION='1' CHANNELS='3' ROWS='72' COLUMNS='72' DEPTH='8' MODE='3' MODENAME='RGBColor'><br /> <LAYER NAME='Background' TOP='0' LEFT='0' BOTTOM='72' RIGHT='72' WIDTH='72' HEIGHT='72'><br /> <BLENDMODE OPACITY='100' CLIPPING='0'><br /> <NORMAL /> <!-- not parsed --><br /> <TRANSPARENCYPROTECTED /><br /> </BLENDMODE><br /> </LAYER><br /><br /> <LAYER NAME='abc/def' TOP='26' LEFT='31' BOTTOM='57' RIGHT='68' WIDTH='37' HEIGHT='31'><br /> <BLENDMODE OPACITY='100' CLIPPING='0'><br /> <NORMAL /> <!-- not parsed --><br /> </BLENDMODE><br /> </LAYER><br /><br /> <LAYER NAME='a/b/c/de/fg' TOP='12' LEFT='7' BOTTOM='43' RIGHT='44' WIDTH='37' HEIGHT='31'><br /> <BLENDMODE OPACITY='100' CLIPPING='0'><br /> <NORMAL /> <!-- not parsed --><br /> <TRANSPARENCYPROTECTED /><br /> </BLENDMODE><br /> </LAYER><br /><br /> <COMPOSITE CHANNELS='3' HEIGHT='72' WIDTH='72'><br /> </COMPOSITE><br /></PSD><br /><br /></pre>

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Jul 20, 2007 Jul 20, 2007

Copy link to clipboard

Copied

Thanks Toby but the layer need to be manipulated using the dimension info right before they are saved so it's just more work to have another program do this.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Jul 21, 2007 Jul 21, 2007

Copy link to clipboard

Copied

LATEST
Gotcha - I missed that bit :)

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines