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

Help with Photoshop script: how to iterate through all open documents?

Community Beginner ,
Nov 01, 2017 Nov 01, 2017

Copy link to clipboard

Copied

Hello,

I'm very new to Javascript, and scripting in Photoshop especially. I'm trying to create a script that does the following:

  1. Iterates through all open PSDs in Photoshop
  2. Checks for paths.
  3. Checks path at first state -- [0].
  4. First checks if that path is a work path. If so, renames work path to "Path 1" and creates clipping path set to .2 flatness.
  5. If not work path, then checks if the path is not a clipping path. If it isn't, sets path as clipping path set to .2 and renames it "Path 1" if needed.
  6. If path is a clipping path already, sets it to .2 flatness and renames to "Path 1" if needed.

Basically, the idea here is that any path at the top of the path list in the paths panel, whether a work path or a regular path, needs to become a clipping path set with .2 flatness and renamed to be Path 1.

I've gotten this far (script below), but I would like to now have this script iterate through all open documents. Is there a way I can accomplish this with what I've written so far? Again, I'm very new, and I've written this script in a way that I can follow -- I'm sure there are instances where I can consolidate and remove duplicate code. If there is a way to have this iterate through all open documents, I would really appreciate any advice and help. Thank you!

var docRef = app.activeDocument;

if (app.documents.length > 0) {

    var n = docRef.pathItems.length;

    //If path at state [0] is a "work path," rename as "Path 1" and make into a clipping path set to .2

    if ((n > 0) && (docRef.pathItems[0].kind == PathKind.WORKPATH)) {

        docRef.pathItems.getByName("Work Path").name = "Path 1";

        docRef.pathItems[0].makeClippingPath(.2);

    //If path at state [0] is not a clipping path, make into clipping path set to .2 and rename as "Path 1," if needed.

    //"Work paths" and regular paths needed to be treated differently, so I have it check for work path prior to this step.

    //Also, I kept getting a popup to rename the path if it was already named "Path 1," the second if statement seems to avoid that.

    } else if ((n > 0) && (docRef.pathItems[0].kind != PathKind.CLIPPINGPATH)) {

        docRef.pathItems[0].makeClippingPath(.2);

        if (docRef.pathItems[0].name != "Path 1") {

            docRef.pathItems[0].name = "Path 1";

        };

    //If path at state [0] is a clipping path, set to clipping path .2 and rename as "Path 1," if needed.

    } else if ((n > 0) && (docRef.pathItems[0].kind == PathKind.CLIPPINGPATH)) {

        docRef.pathItems[0].makeClippingPath(.2);

        if (docRef.pathItems[0].name != "Path 1") {

            docRef.pathItems[0].name = "Path 1";

        };

    };

};

TOPICS
Actions and scripting

Views

2.9K

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

correct answers 1 Correct answer

Community Expert , Nov 16, 2017 Nov 16, 2017

You're not changing what is the active document. Change line 2 to this:

app.activeDocument = app.documents

Votes

Translate

Translate
Adobe
Community Expert ,
Nov 01, 2017 Nov 01, 2017

Copy link to clipboard

Copied

You'll want to create a loop for going through the open docs:

for(var i=0;i<app.documents.length;i++){

     var actDoc = app.documents;

     //More code here

     }

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
Community Beginner ,
Nov 16, 2017 Nov 16, 2017

Copy link to clipboard

Copied

Hi Chuck,

Thank you for your response, and sorry for my late one. I incorporated the for loop into my script above, as shown below. I tested it with 3 open PSDs. Whenever I execute, the script only applies to one open document, completes the task, then ends, and it doesn't move through the rest of the open documents. Would you have another suggestion for how this could work? Let me know if the loop has been added to my code incorrectly. Thanks again for the help.

for (var i = 0; i < app.documents.length; i++) {

    var actDoc = app.documents;

    var docRef = app.activeDocument;

    var n = docRef.pathItems.length;

    //If path at state [0] is a "work path," rename as "Path 1" and make into a clipping path set to .2

    if ((n > 0) && (docRef.pathItems[0].kind == PathKind.WORKPATH)) {

        docRef.pathItems.getByName("Work Path").name = "Path 1";

        docRef.pathItems[0].makeClippingPath(.2);

    //If path at state [0] is not a clipping path, make into clipping path set to .2 and rename as "Path 1," if needed.

    //"Work paths" and regular paths needed to be treated differently, so I have it check for work path prior to this step.

    //Also, I kept getting a popup to rename the path if it was already named "Path 1," this seems to avoid that.

    } else if ((n > 0) && (docRef.pathItems[0].kind != PathKind.CLIPPINGPATH)) {

        if (docRef.pathItems[0].name != "Path 1") {

            docRef.pathItems[0].name = "Path 1";

        docRef.pathItems[0].makeClippingPath(.2);

        };

    //If path at state [0] is a clipping path, set to clipping path .2 and rename as "Path 1," if needed.

    } else if ((n > 0) && (docRef.pathItems[0].kind == PathKind.CLIPPINGPATH)) {

        docRef.pathItems[0].makeClippingPath(.2);

        if (docRef.pathItems[0].name != "Path 1") {

            docRef.pathItems[0].name = "Path 1";

        docRef.pathItems[0].makeClippingPath(.2);

        };

    };

};

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
Community Expert ,
Nov 16, 2017 Nov 16, 2017

Copy link to clipboard

Copied

You're not changing what is the active document. Change line 2 to this:

app.activeDocument = app.documents

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
Community Beginner ,
Nov 17, 2017 Nov 17, 2017

Copy link to clipboard

Copied

Yesssss, thank you! This is working now! I really appreciate your help, Chuck

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
Community Expert ,
Dec 26, 2021 Dec 26, 2021

Copy link to clipboard

Copied

LATEST

As the change in forum software mangled previous scripts by removing key items such as variable references from loops, the missing code is [i], as in:

 

app.activeDocument = app.documents[i];

 

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