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

Script to select same stroke color of path group ~~Adobe Illustrator

Participant ,
Mar 29, 2024 Mar 29, 2024

Copy link to clipboard

Copied

how to select path group with script ? if they are not group, I can select with script. pls help me, thanks

 

ruihuang_0-1711761632580.png

var doc = app.activeDocument;
var artb = doc.artboards;
var artLayer = doc.layers[0];
var s = artLayer.pathItems;

var colorObj = new CMYKColor();
colorObj.cyan = 100;
colorObj.magenta = 0;
colorObj.yellow = 0;
colorObj.black = 0;

var aLen = artb.length;
for(var i = 0; i < s.length; i++){
var pathItem = s[i];

if (pathItem.strokeColor.cyan == 100 && pathItem.strokeColor.magenta == 0 && pathItem.strokeColor.yellow == 0 && pathItem.strokeColor.black == 0){
pathItem.selected = true;
artb.add(pathItem.geometricBounds);
pathItem.remove();
}else{

}
}

 

TOPICS
Scripting

Views

533

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 , Apr 01, 2024 Apr 01, 2024

I had just opened page 3, it comes as I showed with no clipping mask

 

if you open the 3 pages, each page is a group. The group you're interested in is the top group or page 3.

 

all you have to do is target the clipping mask, which is also a group, 

 

try this

var doc = app.activeDocument;
var artb = doc.artboards;
var artLayer = doc.layers[0];

var topGroup = artLayer.groupItems[0]; // *** added
var clipGroup = topGroup.groupItems[0]; // *** added

var s = clipGroup.pathItems; // *** edited

v
...

Votes

Translate

Translate
Adobe
Participant ,
Mar 29, 2024 Mar 29, 2024

Copy link to clipboard

Copied

Additional explanation, actually I want to open PDF with AI and select the path object of the PDF,  if it is rectangle, then change to artboards. but I can't selected.

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
Advocate ,
Mar 30, 2024 Mar 30, 2024

Copy link to clipboard

Copied

for(var i = s.length-1; i >= 0; 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
Community Expert ,
Mar 30, 2024 Mar 30, 2024

Copy link to clipboard

Copied

instead of checking for pathItems

var s = artLayer.pathItems;

 

check for pageItems, 

var s = artLayer.pageItems;

 

pageItems will include everything ie text, groups, compound paths, etc, so, in your loop, first check what kind of object you're dealing with before proceeding

 

// PSEUDO CODE
for(var i = 0; i < s.length; i++){
  var pgItem = s[i];

  if (pgItem.typename == "TextFrame")
    continue; // stop here, continue with next item

  if (pgItem.typename == "PathItem") {
    // do your thing
  }

  if (pgItem.typename == "GroupItem") {
    var pItem = pgItem[0]; // first item in the group
    // check if pItem is a path Item and if it meets your criteria
    // then add artboard using pgItem (group) bounds
  }

 

 

 

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 ,
Mar 31, 2024 Mar 31, 2024

Copy link to clipboard

Copied

Hi CarlosCanto,

    thanks for your answer, but I still have problem for this, pgItem should be one page in PDF, and there is not found pathItem. see below, script code: alert(pathItem.length) return 0,  Where do I need to modify it?

if(pgItem.typename == "GroupItem"){
var pathItem = pgItem.pathItems;
alert(pathItem.length);
for(var i=0; i<pathItem.length;i++){
//do thing

}

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 ,
Apr 01, 2024 Apr 01, 2024

Copy link to clipboard

Copied

your pageItem might be inside a clipping mask, most pdf files include a lot of nested clipping masks.

 

can you share your pdf file?

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 ,
Apr 01, 2024 Apr 01, 2024

Copy link to clipboard

Copied

Hi Carlos,  I have uploaded pdf, I want to change all white rectangle of pdf to artboard, please help to try, many thanks

ruihuang_0-1712019533386.png

 

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 ,
Apr 01, 2024 Apr 01, 2024

Copy link to clipboard

Copied

based on the sample file, your script would work without issues, except that the rectangles are white, not cyan

 

CarlosCanto_0-1712029672403.png

 

so if you test for cmyk 0, 0, 0, 0 then you'll be able to find all four rectangles

 

var doc = app.activeDocument;
var artb = doc.artboards;
var artLayer = doc.layers[0];
var s = artLayer.pathItems;

var whiteRectangles = [];

// collect white rectangles
for (var i = 0; i < s.length; i++) {
  var pathItem = s[i];
  
  if (pathItem.strokeColor.cyan == 0 && pathItem.strokeColor.magenta == 0 && pathItem.strokeColor.yellow == 0 && pathItem.strokeColor.black == 0) {
    whiteRectangles.push(pathItem);
  } else {}
}   

for (var a=0; a<whiteRectangles.length; a++) {
    artb.add(whiteRectangles[a].geometricBounds);
    whiteRectangles[a].remove();
}

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 ,
Apr 01, 2024 Apr 01, 2024

Copy link to clipboard

Copied

Your solution is great, but if you open my PDF to test the script code, it still be failed. I think it's because of the clipping mask.

ruihuang_2-1712037303709.png

ruihuang_3-1712037354212.png

 

 

 

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 ,
Apr 01, 2024 Apr 01, 2024

Copy link to clipboard

Copied

I had just opened page 3, it comes as I showed with no clipping mask

 

if you open the 3 pages, each page is a group. The group you're interested in is the top group or page 3.

 

all you have to do is target the clipping mask, which is also a group, 

 

try this

var doc = app.activeDocument;
var artb = doc.artboards;
var artLayer = doc.layers[0];

var topGroup = artLayer.groupItems[0]; // *** added
var clipGroup = topGroup.groupItems[0]; // *** added

var s = clipGroup.pathItems; // *** edited

var whiteRectangles = [];

// collect white rectangles
for (var i = 0; i < s.length; i++) {
  var pathItem = s[i];
  
  if (pathItem.strokeColor.cyan == 0 && pathItem.strokeColor.magenta == 0 && pathItem.strokeColor.yellow == 0 && pathItem.strokeColor.black == 0) {
    whiteRectangles.push(pathItem);
  } else {}
}   

for (var a=0; a<whiteRectangles.length; a++) {
    artb.add(whiteRectangles[a].geometricBounds);
    whiteRectangles[a].remove();
}

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 ,
Apr 02, 2024 Apr 02, 2024

Copy link to clipboard

Copied

LATEST

You are so awesome, I admire you!!!

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