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

Skripting, undo: I gt an Error i dont understand

Participant ,
Apr 08, 2024 Apr 08, 2024

Copy link to clipboard

Copied

In the following simplified code I get the error that "the last command could not be undone".

Anyone knows WHY I get this error?

 

 

 

QGlobalSemaphores = [];

// ECMAScript 3 has no classes
function WaitForIt(callback, object) {
	this.object = object;
	this.answer = null;
	this.index  = -1;
	this.callback = callback;
}

function doSubscript(index) {
	mySemaphore = QGlobalSemaphores[index];
	mySemaphore.callback(mySemaphore.object);
	alert("executed");
}

function executeSubscript(callback, object) {
	var w = new WaitForIt(callback, object);
	w.index = QGlobalSemaphores.push(w) - 1;
	app.doScript(doSubscript,ScriptLanguage.JAVASCRIPT,[w.index],UndoModes.FAST_ENTIRE_SCRIPT,'FE-Test');
}

function deleteLayers(obj) {
	var doc = app.activeDocument;
	var i = doc.layers.length;
	while (i-->1) {
		doc.layers[i].remove();
	}
}

function test() {
	executeSubscript(deleteLayers, null);
	app.undo(); // <-- I get the error here.
}

app.doScript(test,ScriptLanguage.JAVASCRIPT,[],UndoModes.ENTIRE_SCRIPT,'Callback-Test');

 

 

 

TOPICS
Bug , How to , Scripting

Views

1.0K

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 16, 2024 Apr 16, 2024
quote

I didnt get a solution, I got some workarounds.

 

By @Lordrhavin

 

well. workarounds are solutions for situations where there are no other solutions.

Votes

Translate

Translate
Community Expert ,
Apr 16, 2024 Apr 16, 2024

Copy link to clipboard

Copied

quote

There’s also revert()

 

app.scriptPreferences.userInteractionLevel = UserInteractionLevels.NEVER_INTERACT;
app.activeDocument.revert();
app.scriptPreferences.userInteractionLevel = UserInteractionLevels.INTERACT_WITH_ALL

By @rob day

 

Yeah ... but ... you would have to check at the beginning of the script if file has been saved and no changes has been made after opening it- otherwise, you might go back too much?

 

And sometimes, after opening file, InDesign marks it as "edited", even when there was nothing done - so you'll get false-positive.

 

There is also possibility, that user has made some changes - and then wants script to perform operation on the current state of the file.

 

So working on a copy - is, I think, the safest way?

 

Everything what script will do - wont affect in any way the original document...

 

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

Copy link to clipboard

Copied

LATEST
quote

I didnt get a solution, I got some workarounds.

 

By @Lordrhavin

 

well. workarounds are solutions for situations where there are no other solutions.

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

Copy link to clipboard

Copied

Another approach would be to set global variables to capture the dialog results at the top of the script. This example is a dialog but you should be able to adapt it for a palette that is listening for results.

 

The yres variable gets the result of the yellow dropdown, and mres gets the result of the magenta dropdown, then I run helper functions based on the results. The setExport() function calls the setup and export and it can be undone:

 



//results as global variables
var yres, mres, dlist;

makeDialog();
function makeDialog(){
    var d = app.dialogs.add({name:"Export Layers", canCancel:true});
    with(d){
        with(dialogColumns.add()){
                with(dialogColumns.add()){
                    staticTexts.add({staticLabel:"Yellow:"});
                    staticTexts.add({staticLabel:"Magenta:"});
                }
                with(dialogColumns.add()){
                    dlist = ["dont export", "as specified (visible + printable)", "as unprintable", "as hidden", "as hidden+unprintable"]
                    yres = dropdowns.add({stringList:dlist, selectedIndex:0, minWidth:80});
                    mres = dropdowns.add({stringList:dlist, selectedIndex:0, minWidth:80});
                } 
        }
        if(d.show() == true){
			//get the dialog result or with a panel listen for results
            yres = yres.selectedIndex;
            mres = mres.selectedIndex;

			//do onResult which calls the setters and export
            app.doScript(setExport, ScriptLanguage.JAVASCRIPT, [], UndoModes.ENTIRE_SCRIPT, 'Export Doc');
			//undo the onResult function
			app.activeDocument.undo();
        }
    }
}


function setExport(){
    //set yellow
	if (yres == 0) {
		deleteLayer("Yellow")
	} else if (yres == 1) {
		alert("Do Nothing with Yellow")
	}else if (yres == 2) {
		noPrint()
	}else if (yres == 3) {
		hideNoPrintAll()
	}

	//app.activeDocument.pages.item(0).rectangles.add();
	
//setter here for magenta choice then export

	//export routine here
	alert("Export")
}


////////////////Settings Functions////////////////


function deleteLayer(ln){
	alert("Deleting " +ln)
	var y = app.activeDocument.layers.itemByName("Yellow")
	if (y.isValid) {
		y.remove()
	} 
}

function noPrint(){
	alert("Set Layers to No Print")
}

function hideAll(){
	alert("Set Layers to Hidden")
}

function hideNoPrintAll(){
	alert("Set Layers to Hidden")
}

 

 

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

Copy link to clipboard

Copied

quote

@Robert at ID-Tasker while loop could also be written like this where I’m explicity using the collection’s firstItem() and lastItem() methods to remove all the items above or below.


By @rob day

 

But firstItem() and lastItem() methods are very slow.

Referring by "0" or "-1" is much faster.

 

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