Copy link to clipboard
Copied
Does this setting of object styles can be modified with a Javascript?
I have bunch of Object Styles that I need to reduced by 3mm the value found in the Y Offset field when enabled.
Got my braces wrong there, sorry.
Also got my brackets wrong -- objStyles in line 5 needs an index [i].
Here's the fix, works over here.
var objStyles = app.activeDocument.allObjectStyles;
var adjust = -3;
for (var i = 1; i < objStyles.length; i++) {
try {
objStyles[i].transformAttributeOptions.transformAttrY += adjust;
} catch (_) {
}
}
Copy link to clipboard
Copied
Can target the objects with the style and move them - but can't find a way to redefine the style.
Copy link to clipboard
Copied
That's my conclusion so far.
Copy link to clipboard
Copied
This is one of the more counterintuitive things in the scripting APIs, entirely inconsistent with how the other (earlier) object-style properties had been implemented. Anyway, first you need to do
ostyle.setPositionAttributeState (
PositionAttributes.BOTH_X_Y_ATTRIBUTE, true
)
Then to set the offset:
ostyle.transformAttributeOptions.transformAttrY = '11.5mm';
P.
Copy link to clipboard
Copied
I was able to introduce an override to frames with a specific ObStyle using this:
// Define the object style name you want to target
var targetStyleName = "name_here";
// Define the amount to change the Y value (in mm)
var yOffset = -3;
// Get a reference to the current InDesign document
var doc = app.activeDocument;
// Set the document's ruler units to points
app.scriptPreferences.measurementUnit = MeasurementUnits.MILLIMETERS;
// Loop through all the page items in the document
for (var i = 0; i < doc.pageItems.length; i++) {
var pageItem = doc.pageItems[i];
// Check if the page item has an applied object style
if (pageItem.appliedObjectStyle !== null && pageItem.appliedObjectStyle.name == targetStyleName) {
// Change the Y value by the specified amount
pageItem.move(undefined, [0, yOffset]);
}
}
// Alert when the process is completed
alert("Frames with object style '" + targetStyleName + "' have been moved by " + Math.abs(yOffset) + " millimeters.");
Copy link to clipboard
Copied
> I was able to introduce an override to frames with a specific ObStyle
Nice try. But didn't you get the style change to work? That would be better than applying th overries, wouldn't it?
Copy link to clipboard
Copied
Sure it will be better... but my knowledge of Javascript is limited. 🙂
Copy link to clipboard
Copied
Arnis Rugājs come up with this snippet in a Facebook group.
The base of it is there, just need an escape when the Y Offset is not enable or the value go lower than 0. Also need a way to set units measurment.
var doc = app.activeDocument;
var myObjectStyle;
for (var i = 1; i < doc.objectStyles.length; i++){
myObjectStyle = doc.objectStyles.item(i);
if (myObjectStyle.enableTransformAttributes == true && myObjectStyle.transformAttributeOptions.transformAttrY){
myObjectStyle.transformAttributeOptions.transformAttrY -= 3;
}
}
Copy link to clipboard
Copied
You could check whether the offset stays greater than 0 after adjusting it:
var doc = app.activeDocument;
var myObjectStyle;
var adjust = 3;
for (var i = 1; i < doc.objectStyles.length; i++){
myObjectStyle = doc.objectStyles.item(i);
if (myObjectStyle.enableTransformAttributes == true &&
myObjectStyle.transformAttributeOptions.transformAttrY - adjust > 0) {
myObjectStyle.transformAttributeOptions.transformAttrY -= adjust;
}
}
But since InDesign returns an error if you try to set it less than 0, you could simply try to do it and catch any error. Also, you don't really need variables to the document, just grab the object styles straight away:
var styles = app.activeDocument.objectStyles.everyItem().getElements();
for (var i = 1; i < styles.length; i++){
try {
styles.item(i).transformAttributeOptions.transformAttrY -= 3;
} catch (_) {
}
}
P.
Copy link to clipboard
Copied
Peter, your reduced script is doing nothing for me.
I have figure out to use a catch (_) to ignore the error. I like the idea of a variable for the adjust. I’ll probably create a popup for it.
Now how can it modify also ObStyles inside folders?
try {
var myObjectStyle;
var adjust = -3;
for (var i = 1; i < app.activeDocument.objectStyles.length; i++){
myObjectStyle = app.activeDocument.objectStyles.item(i);
if (myObjectStyle.enableTransformAttributes == true &&
myObjectStyle.transformAttributeOptions.transformAttrY - adjust > 0) {
myObjectStyle.transformAttributeOptions.transformAttrY += adjust;
}
}
} catch (_) {
}
Copy link to clipboard
Copied
> Now how can it modify also ObStyles inside folders?
Use allObjectStyles:
app.activeDocument.allObjectStyles;
In your latest effort you're overdoing it: either check whether enableTransformAttributes is true and whether the calculated offset is greater than 0, or just try to change the offset. Making the checks and use try just creates unnecessary overhead.
So my idea would be this:
var objStyles = app.activeDocument.allObjectStyles;
var adjust = -3;
for (var i = 1; i < objStyles.length; i++) {
try {
objStyles.transformAttributeOptions.transformAttrY += adjust;
}
} catch (_) {
}
but you mentioned that the shortened script doesn't do anything. Any idea why that is the case?
Copy link to clipboard
Copied
When I use the:
allObjectStyles
Nothing is working anymore...
When I remove the enableTransformAttributes, nothng works anymore too.
When I use you latest last small script i get this error.
JavaScript Error!
Error Number: 15
Error String: Try without catch or finally
Engine: main
Line: 7
Source: } catch () {
Offending Text: }
The one I have working look like this now.
try {
var askYoffset = prompt("Y value - Using doc unit");
var adjust = parseInt(askYoffset);
var myObjectStyle;
for (var i = 1; i < app.activeDocument.objectStyles.length; i++){
myObjectStyle = app.activeDocument.objectStyles.item(i);
if (myObjectStyle.enableTransformAttributes == true && myObjectStyle.transformAttributeOptions.transformAttrY) {
myObjectStyle.transformAttributeOptions.transformAttrY += adjust;
}
}
} catch (e) {
}
But only for first level. But it will stop working completely when I change these two lines :
for (var i = 1; i < app.activeDocument.allObjectStyles.length; i++){
myObjectStyle = app.activeDocument.allObjectStyles.item(i);
Copy link to clipboard
Copied
Got my braces wrong there, sorry.
Also got my brackets wrong -- objStyles in line 5 needs an index [i].
Here's the fix, works over here.
var objStyles = app.activeDocument.allObjectStyles;
var adjust = -3;
for (var i = 1; i < objStyles.length; i++) {
try {
objStyles[i].transformAttributeOptions.transformAttrY += adjust;
} catch (_) {
}
}
Copy link to clipboard
Copied
BINGO! 😉
Thanks Peter! You are unrivaled! I’ll had my stuff around it.
Copy link to clipboard
Copied
My final version for both X & Y Offset with a single undo.
app.doScript("main()", ScriptLanguage.javascript, undefined, UndoModes.ENTIRE_SCRIPT, "X & Y Offset");
function main() {
var input = prompt("X & Y Offset separated by a space\nWill use the current document measurement unit", "");
var inputValues = input.split(" ");
if (inputValues.length === 2) {
var adjustX = parseFloat(inputValues[0]);
var adjustY = parseFloat(inputValues[1]);
} else {
alert("Please enter two values separated by a space.");
}
var objStyles = app.activeDocument.allObjectStyles;
for (var i = 1; i < objStyles.length; i++) {
try {
objStyles[i].transformAttributeOptions.transformAttrY += adjustY;
objStyles[i].transformAttributeOptions.transformAttrX += adjustX;
} catch (_) {
}
}
}