Copy link to clipboard
Copied
Playing with scripting and finding it very frustrating but this one would save me a good bit of time
I just want to ungroup an object - leave it selected
I figured that out
var ungroupMenuItem = app.menuActions.item("$ID/Ungroup");
if (ungroupMenuItem.isValid) {
// Invoke the menu item
ungroupMenuItem.invoke();
} else {
alert("The Ungroup menu item was not found.");
}
Seems to work - maybe not the best way
But can't access
Object>Path>add
Then next step is
Object>Convert to Shape>Rectangle
All the while leaving the object selected while running through
1 out of 3 ain't bad...
Ultimately - I have 2 of these objects on every page that need to be ungrouped
Ideally I'd like to just make them a link - as I'm replacing them with an image anyway
If they were converted to links that would be ideal
So I would select the objects on the page in 1 selection - then run the script - it makes them a link
Then I can just relink them
But given that I can't convert the group to a rectangle by scripting I feel the former and latter part of my plan is beyond me.
Any help is much appreciated.
You want to open the file outside of the loop, so I think this might work:
var s = app.activeDocument.selection;
var f = File.openDialog("Select the file", "")
if (f != null) {
for (var i = 0; i < s.length; i++){
if (s[i].constructor.name == "Group") {
s[i].pageItems[0].addPath(s[i].allPageItems);
s[i].pageItems[0].convertShape(ConvertShapeOptions.CONVERT_TO_RECTANGLE);
s[i].pageItems[0].place(f)
s[i].pageItems[0].fit(FitOptions.FIL
Copy link to clipboard
Copied
You can't convert Group into a Rectangle.
Can you post some screenshot? I'm having hard time to understand what exactly are you trying to achieve.
Copy link to clipboard
Copied
Can't post a screenshot - sorry
But imagine say a simple logo - and it's pasted into InDesign
All I want to do is convert that to rectangle - then I place the logo into the frame - same size etc.
Steps I do are
Ungroup
Pathfinder>Add
Pathfinder>Convert to Shape>Rectangle
Then place the image into the resulting frame.
Copy link to clipboard
Copied
Why so complicated?
Copy link to clipboard
Copied
Hi Eugene, I don’t think you need to resort to the menu invoke() method— there is the the selection’s ungroup() method. The question might be which item in the group do you want to convert to a rectangle?
//the first item in a selection–a selected group would have 1 item
var s = app.activeDocument.selection[0];
//check if your selection is a group
if (s.constructor.name == "Group") {
//converts the first page item in the group to a rectangle
s.pageItems[0].convertShape(ConvertShapeOptions.CONVERT_TO_RECTANGLE)
}
//ungroups the group
s.ungroup()
Before and after
Copy link to clipboard
Copied
Everything in the group to be turned into a rectangle
Ungroup Object
Pathfinder>Add
Pathfinder>Convert Shape to Rectangle
And then place the image
If I could got another route of converting the grouped object into an active link and replace the link that would be even better.
Copy link to clipboard
Copied
Do you mean you want to remove the group and replace it with a rectangle with the same bounds, or convert both items in the group to rectangles and then place an image into one of the rectangles?
Copy link to clipboard
Copied
For example
Grouped object
Ungroup
Pathfinder Add
Result
Convert to Rectangle
Result
Then I place the image in as a link
========
Ideally
If this could be converted to a link then I just replace the link
Copy link to clipboard
Copied
Try this:
//the first item in a selection–a selected group would have 1 item
var s = app.activeDocument.selection[0];
//check if your selection is a group
if (s.constructor.name == "Group") {
//combines the groups items into one path
s.pageItems[0].addPath(s.allPageItems)
//converts the shape into a rectangle
s.pageItems[0].convertShape(ConvertShapeOptions.CONVERT_TO_RECTANGLE)
}
//ungroups the group
s.ungroup()
Copy link to clipboard
Copied
Are my replies invisible?
Copy link to clipboard
Copied
Absolutely not invisible, and thank you for the replies - it's simply crossed over when I replied to Rob earlier, and didn't your reply - the notificaiton bell was not active so didn't see any new notifications.
I had to come offline for a while and only back now.
Thank you - I'll try the suggestions and see where I can get to.
I guess I think too literal about it in InDesign how-to terms and not programming it to do what I want.
I thought it was very simple but I can't get my brain around the problems I face.
Copy link to clipboard
Copied
Hi Eugene, are you not getting email notifications? I stopped getting email notifications yesterday—recently moved my web hosting and mail server, so I thought that was the reason, but maybe not?
I thought it was very simple
Methods can sometimes be hard to find in the API, but a rectangle, oval, or polygon have the addPath() and convertShape() methods, which are the equivalent of the UI’s Pathfinder buttons
https://www.indesignjs.de/extendscriptAPI/indesign-latest/#Rectangle.html#d1e217687__d1e219657
Copy link to clipboard
Copied
Never got email notifications.
Just the bell symbol on the top of the forum.
Copy link to clipboard
Copied
But what's the point in converting objects - in this situation - when @Eugene Tyson wants to completely replace current Object - and only need location & size information?
Copy link to clipboard
Copied
But what's the point in converting objects
Because he was asking about the scripting equivalents of the Pathfinder panel.
Copy link to clipboard
Copied
Really open to suggestions best way forward - by analytical thinking seems to be way off when it comes to scripting.
Bascially I want to replace the bunch of grouped objects with a link I have in a folder.
If the script just did that then I'd be well chuffed with that.
At the moment, I thought I'm doing the Select>Ungroup>Path Add>Path Convert
Figured I could script that part to make it 1 click option
But couldn't get past Ungroup
I'd be very impressed to see the whole group of objects that are grouped together replaced with the link.
That was my goal at the start but considering I barely figured out how to ungroup things with a script... I toned down my efforts.
Copy link to clipboard
Copied
Based on the @rob day example:
var s = app.activeDocument.selection[0];
var r = app.activeDocument.rectangles.add();
r.geometricBounds() = s.geometricBounds();
s.remove();
r.place(newLink);
I'm not sure about "()" after GeometricBounds - and you need to define "newLink".
Copy link to clipboard
Copied
Also, if you are going to add a new rectangle, what page is it on and are there other properties for the new rectangle you care about, e.g. what layer is it in etc?
You could get the graphic link from the group—assuming there is just one and it is linked and not pasted:
var s = app.activeDocument.selection[0];
if (s.constructor.name == "Group") {
var fp = s.allGraphics[0].itemLink.filePath
s.pageItems[0].addPath(s.allPageItems)
s.pageItems[0].convertShape(ConvertShapeOptions.CONVERT_TO_RECTANGLE);
s.pageItems[0].place(fp);
s.pageItems[0].fit(FitOptions.FILL_PROPORTIONALLY)
s.ungroup();
}
Copy link to clipboard
Copied
Also, if you are going to add a new rectangle, what page is it on and are there other properties for the new rectangle you care about, e.g. what layer is it in etc?
[...]
By @rob day
Yeah, all valid points - but I was replying on the phone and I'm not JS guy.
This should work:
var r = s.parentPage.rectangles.add();
r.itemLayer = s.itemLayer;
Copy link to clipboard
Copied
This one worked for me first time, converted the object and as explained to @Robert at ID-Tasker earlier - the rotation was important and something I neglected to mention earlier - as my previous approach of manually ungrouping and converting to rectangle mainted it's relationship of rotation.
I've had a go at it - and replacing the link and it's working well
only issue is that it asks to relink multiple times for multiple items.
Where I got to was adding a dialog box (clusmly probably)
And also allowing multiple selections of the object.
It's not a huge deal I could have 2 or 4 or 6 or 8 of these, it's just a matter of clicking the icon in the dialog box over and over.
But I can't get it to only ask once.
var selectedObjects = app.activeDocument.selection;
for (var i = 0; i < selectedObjects.length; i++) {
var s = selectedObjects[i];
if (s.constructor.name == "Group") {
var dialog = File.openDialog("Select an image file", "*.jpg;*.jpeg;*.png;*.gif");
if (dialog != null) {
var fp = dialog.fsName; // Get the file path
var pageItemsToAdd = [];
for (var j = 0; j < s.pageItems.length; j++) {
pageItemsToAdd.push(s.pageItems[j]);
}
if (s.pageItems[0].constructor.name == "PageItem") {
s.pageItems[0].addPath(pageItemsToAdd);
} else {
alert("The first item in the selection is not a page item.");
}
s.pageItems[0].convertShape(ConvertShapeOptions.CONVERT_TO_RECTANGLE);
s.pageItems[0].place(fp);
s.pageItems[0].fit(FitOptions.FILL_PROPORTIONALLY);
s.ungroup();
} else {
alert("No file selected. Please select an image file.");
}
}
}
Copy link to clipboard
Copied
From the code - it looks like you are allowing selection of multiple Groups - and it will ask for link for each Group - perfectly normal for me?
But then it looks like you are still trying to "process" each item in the Group?
Why?
You want to link new image for every item in the group?
Copy link to clipboard
Copied
It's the only way it worked after about 20 attempts
Copy link to clipboard
Copied
It's the only way it worked after about 20 attempts
By @Eugene Tyson
But I thought that you want to replace whole Group - with a single new Rectangle?
Copy link to clipboard
Copied
the rotation was important and something I neglected to mention earlier
Rather than trying to place the logo, you could cut it then paste it into the combined rectangle—that would preserve all of the graphic’s properties. So, assuming it’s the graphic that is rotated:
//the first item in a selection–a selected group would have 1 item
var s = app.activeDocument.selection[0];
if (s.constructor.name == "Group") {
//select and cut the group’s graphic—can be a pasted or linked image, AI, PDF...
app.select(s.allGraphics[0])
app.cut()
//convert the group’s page items into a single rectangle
s.pageItems[0].addPath(s.allPageItems)
s.pageItems[0].convertShape(ConvertShapeOptions.CONVERT_TO_RECTANGLE);
//select the rectangle and paste into
app.select(s.pageItems[0])
app.pasteInto();
s.ungroup()
}
Copy link to clipboard
Copied
I'm a bit lost...
@rob day, why are you assuming, that there is a linked file as a part of the group?
From what @Eugene Tyson said - there is a group of objects - and the whole group - without analysing contents - should be removed and in its place should be new Rectangle created and completely new file linked?
And why do you keep combining paths - when it has been already established - that the Rectangle that will have GeometricBounds of the Group is enough?
Of course, it might be beneficial to first un-rotate Group, get GeometricBounds, create Rectangle, apply GeometricBounds and then re-rotate it - so the size will perfectly match the Group.
Or am I missing something?