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
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{
}
}
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
...
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.
Copy link to clipboard
Copied
for(var i = s.length-1; i >= 0; i--){
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
}
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
}
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?
Copy link to clipboard
Copied
Copy link to clipboard
Copied
based on the sample file, your script would work without issues, except that the rectangles are white, not cyan
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();
}
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.
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();
}
Copy link to clipboard
Copied
You are so awesome, I admire you!!!