Undo a script in one control z A routine problem
Hello
There is a routine issue i face when I try to make a specific script which seems to make undoing the script in one control z impossible.
Of note I ahve read the post about app.doScript(function()
So the script I am trying to make is to break a single text box with several columns inside into single text columns frames threaded the same way as the og text frame etc
when i tried to make one before I got it to work but couldnt undo
So i tried to make one again
it duplicates the 1st frame the an eqaual amount of times to columns in the 1st text frame and then changes all of the duplicated frames to 1 column
howwever it seems impossible to undo, and instead i have to watch indesign undoing every step
code below is a bit messy but potentially could anyone signpost me to solving this issue
I ahve made a script which allows for rules to be placed inbetween differnt columns of different lengths and its a very multi step process, which has to come up with particaular values for particular situtions yet that can undo in one go.
As always many thanks all.
Best
// Create a reference to the active document
var doc = app.activeDocument;
// Check if a text frame is selected
if (doc.selection.length === 1 && doc.selection[0] instanceof TextFrame) {
var selectedTextFrame = doc.selection[0];
// Get the number of columns in the selected text frame
var numColumns = selectedTextFrame.textColumns.length;
// Wrap the entire operation in a single undoable action
app.doScript("duplicateAndPositionTextBoxes()", ScriptLanguage.JAVASCRIPT);
// Function to duplicate and position the text boxes
function duplicateAndPositionTextBoxes() {
try {
var frameBounds = selectedTextFrame.geometricBounds;
for (var i = 0; i < numColumns; i++) {
// Duplicate the selected text frame
var duplicatedTextFrame = selectedTextFrame.duplicate(selectedTextFrame.parent);
duplicatedTextFrame.contents = "";
// Set the position of the duplicated text frame
var xOffset = i * (frameBounds[3] - frameBounds[1]);
duplicatedTextFrame.move(undefined, [xOffset, 0]); // Change the coordinates as needed
// Set the duplicated frame to a single column layout
duplicatedTextFrame.textFramePreferences.textColumnCount = 1;
}
} finally {
// Perform undo if there's an error
if (app.scriptPreferences.enableRedraw) {
app.scriptPreferences.enableRedraw = false;
app.undo();
app.scriptPreferences.enableRedraw = true;
}
}
}
} else {
alert("Please select a single text frame.");
}