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

Script for conversion to clipping path and running on a batch

Community Beginner ,
Feb 20, 2024 Feb 20, 2024

Hi all, hoping to get some help/guidance on this question since I received some great help on my last question.

 

Background info:

We have many image files with various numbers of paths and all potentially named differently and  we need all of them to have the path name converted to “Path 1” and then convert path one to a clipping path. (Some of the files may already have a path in them named “Path 1” and it may already be a clipping path).

 

What I have tried so far:

So I tried to research the topic on here as well as across the internet, but did not find a solution to this question. I am also far out of my depth when it comes to scripts/scripting, as in a complete newb. I am also fairly certain (?) this cannot be accomplished through an action because it requires logic.

 

I tried using ChatGPT to help me create a script and got it to as far as a point of being able to look at a file and convert the path to the name path 1. I also got it to look at a file that has multiple paths and select the one that has the most anchor points (using the logic that most of our files with multiple paths are group images and the group image path is the one I want to be turned into a clipping path) and change its name to Path 1.

 

After that I keep getting error messages that I feed back into chatgpt because it cannot complete the next step of then converting that path to a clipping path.

 

My ultimate goal:

 would be for this to achieve the path name conversion and the path type conversion correctly and then be able to run this script on a folder of files. I cannot figure out how to run it on anything other than a single image. When I try to use the batch or image processor you can only seem to select actions to run during the script.

 

I will copy and paste the script here of what has been created thus far below. Thank you in advance for any help to get me in the right direction:

 

// Function to find the path with the most anchor points

function findPathWithMostAnchorPoints(doc) {

    var paths = doc.pathItems;

    var maxPoints = 0;

    var selectedPath = null;

 

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

        var currentPath = paths[i];

        if (currentPath.subPathItems.length > maxPoints) {

            maxPoints = currentPath.subPathItems.length;

            selectedPath = currentPath;

        }

    }

 

    return selectedPath;

}

 

// Function to rename the path to "Path 1" if not already named

function renamePath(path) {

    if (path.name !== "Path 1") {

        path.name = "Path 1";

    }

}

 

// Function to convert the path to a clipping path

function convertToClippingPath(path) {

    var desc = new ActionDescriptor();

    var ref = new ActionReference();

    ref.putEnumerated(charIDToTypeID("Path"), charIDToTypeID("Path"), stringIDToTypeID("vectorMask"));

    desc.putReference(charIDToTypeID("null"), ref);

    var ref1 = new ActionReference();

    ref1.putEnumerated(charIDToTypeID("Path"), charIDToTypeID("Path"), stringIDToTypeID("vectorMask"));

    desc.putReference(charIDToTypeID("T   "), ref1);

    executeAction(charIDToTypeID("Mk  "), desc, DialogModes.NO);

}

 

// Function to log errors to a text file

function logError(errorMsg) {

    var errorFile = new File(app.activeDocument.path + "/Error Log.txt");

    errorFile.open("a");

    errorFile.writeln(errorMsg);

    errorFile.close();

}

 

// Main function

function main() {

    var doc = app.activeDocument;

    var paths = doc.pathItems;

 

    if (paths.length === 0) {

        logError("No paths found in the document.");

        return;

    }

 

    // Check if there's only one path in the document

    if (paths.length === 1) {

        var singlePath = paths[0];

        renamePath(singlePath);

        convertToClippingPath(singlePath);

        alert("Single path in the document converted to clipping path and renamed to 'Path 1'.");

        return;

    }

 

    var selectedPath = findPathWithMostAnchorPoints(doc);

 

    if (!selectedPath) {

        logError("Unable to find a path with anchor points.");

        return;

    }

 

    // Rename the selected path if needed

    renamePath(selectedPath);

 

    // Convert the selected path to a clipping path

    convertToClippingPath(selectedPath);

 

    alert("Selected path converted to clipping path and renamed to 'Path 1'.");

}

 

// Run the main function

main();

TOPICS
Actions and scripting
1.5K
Translate
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 3 Correct answers

Community Expert , Feb 20, 2024 Feb 20, 2024

@alimedi77729338 

 

In the following example, I expected the path named "main" to be renamed as Path 1 as it contained the most points, however the simplest path (3 points) was renamed, not the most complex?

 

2024-02-21_10-57-30.png

 

EDIT: Once I added a sub-path to the main path it was correctly identified as being the "most complex" path.

Translate
Community Expert , Feb 20, 2024 Feb 20, 2024

@alimedi77729338 – there is a problem with the AM code in the convertToClippingPath function. You can replace it with the following DOM code:

 

// Function to convert the path to a clipping path
function convertToClippingPath(path) {
    path.makeClippingPath();
}

 

To set a flatness value [0.2 to 100]

 

path.makeClippingPath(2);

 

 

Translate
Community Expert , Feb 21, 2024 Feb 21, 2024
quote
  1. Is there a way to suppress this popup so that it just runs through all files and does not have this pop up interrupt and need verification?

By @alimedi77729338

 

 

Yes, this is why I previously wrote:

 

"P.S. When batching, you will wish to comment out // or remove the 2 alert() lines from the script."

 

//alert("Single path in the document converted to clipping path and renamed to 'Path 1'.");

 

//alert("Selected path converted to clipping path and renamed to 'Path 1'.");

 

Or delete these

...
Translate
Adobe
Community Expert ,
Feb 20, 2024 Feb 20, 2024

@alimedi77729338 

 

In the following example, I expected the path named "main" to be renamed as Path 1 as it contained the most points, however the simplest path (3 points) was renamed, not the most complex?

 

2024-02-21_10-57-30.png

 

EDIT: Once I added a sub-path to the main path it was correctly identified as being the "most complex" path.

Translate
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 ,
Feb 20, 2024 Feb 20, 2024

@alimedi77729338 – there is a problem with the AM code in the convertToClippingPath function. You can replace it with the following DOM code:

 

// Function to convert the path to a clipping path
function convertToClippingPath(path) {
    path.makeClippingPath();
}

 

To set a flatness value [0.2 to 100]

 

path.makeClippingPath(2);

 

 

Translate
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 ,
Feb 20, 2024 Feb 20, 2024
quote

I cannot figure out how to run it on anything other than a single image. When I try to use the batch or image processor you can only seem to select actions to run during the script.

 


By @alimedi77729338

 

 

You need to record the execution of the script into an action. Then batch the action.

 

Otherwise, extra code would need to be added to process an input folder or selected files.

 

P.S. When batching, you will wish to comment out // or remove the 2 alert() lines from the script.

Translate
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 ,
Feb 21, 2024 Feb 21, 2024

If there are further questions please provide meaningful screenshots including the pertinent Panels or provide sample files (both as is and the intended result). 

Translate
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 ,
Feb 21, 2024 Feb 21, 2024

Hi all,  @Stephen Marsh the addition/replacement of that portion of the script did get it to work so thank you! @c.pfaffenbichler  following your directions there will be attachments added!

 

I created an action (I think correctly) where the script is run so that I could test it using File > Automate > Batch as well as File > Scripts > Image Processor. to some success and some failure so I have a couple of followup questions (highlighted in red):

 

  1. When I run it through Batch it seems to work but on every image it pauses and pops up a dialogue box that says "Single Path converted..." and I have to manually hit OK (see screenshot "Clipping Script_Successful Popup.png" attached—you can also see how the action I created is setup). Is there a way to suppress this popup so that it just runs through all files and does not have this pop up interrupt and need verification?
  2. I added another image to my test folder that already had a "path 1" in it and via the script "path 2" would be the proper one to rename to path 1 and convert to a clipping path. But you can see with the batch command I get this dialog box (see screenshot "Clipping Script_popup from Batch command.png"). If I rename it then the batch process will continue to run on the rest of the images (while still bringing up the "success" popup on each one). However, If I run this test folder through the Image processor with the clipping path action specified I get this popup (see and the screenshot "Clipping Script_popup from Image Processor command.png") and the whole automation comes to a standstill after that.  SO, my questions regarding this are is there a way to alter the script so that if it comes upon an image with multiple paths and one of them is already named path 1, but determines it is NOT the correct one to be the clipping path can it automatically rename the offending path 1 to something else, like path X (to ensure it's not anything that could possibly already be in the file) and then continue on renaming the CORRECT path to path 1 and doing the clipping path conversion? As a followup is it advisable to always use batch instead of Image Processor to run on a folder with an action that has a script embedded in it, given how it still kind of continued to work with batch but come to a complet standstill with image processor?

 

Also attaching the whole script .txt file for ease of looking at. Thank you all in advance!

Translate
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 ,
Feb 21, 2024 Feb 21, 2024
quote
  1. Is there a way to suppress this popup so that it just runs through all files and does not have this pop up interrupt and need verification?

By @alimedi77729338

 

 

Yes, this is why I previously wrote:

 

"P.S. When batching, you will wish to comment out // or remove the 2 alert() lines from the script."

 

//alert("Single path in the document converted to clipping path and renamed to 'Path 1'.");

 

//alert("Selected path converted to clipping path and renamed to 'Path 1'.");

 

Or delete these lines, as they can't be suppressed by the Batch command.

 

Translate
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 ,
Feb 23, 2024 Feb 23, 2024
LATEST

Thank you! THat worked. I also just tested a version of the script that changes the name of the path to something completely unique so that it avoids the pitfall I asked about in the second part of my questions. Thanks again for everyone's input!

Translate
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