Skip to main content
lfcorullon13651490
Legend
February 13, 2020
Answered

beforeSave or afterSave eventListeners

  • February 13, 2020
  • 5 replies
  • 2318 views

Why does this code don't work?
Could anyone help me on the right way?

I want to create a new backup of the current document each time the user saves it.

 

app.activeDocument.addEventListener("beforeSave" , myBKP);

function myBKP() {
    var doc = app.activeDocument;
    var myDate = myDate();
    var myDocName = doc.name.replace(".indd" , "");
    var myPath = Folder.decode(doc.filePath);
    if (!Folder(myPath + "/bkps").exists) {
        Folder(myPath + "/bkps").create();
        myPath = Folder(myPath + "/bkps");
        } else {
            myPath = Folder(myPath + "/bkps");
            }
    var myBkpFileName = new File(myPath + "/" + myDocName + ".idml");
    alert(myBkpFileName);
//~     doc.saveACopy(File(myBkpFileName));
    }





//GET DATES
//========================================================================================
function myDate() {
    var date = new Date();
    var day = (date.getDate() > 10 ) ? date.getDate() : ("0" + date.getDate() );
    var month = ((date.getMonth()+1) >= 10 ) ? (date.getMonth()+1) : ("0" + (date.getMonth()+1) );
    var year = (date.getFullYear() > 10 ) ? date.getFullYear() : ("0" + date.getFullYear() );
    var hours = (date.getHours() > 10 ) ? date.getHours() : ("0" + date.getHours() );
    var minutes = (date.getMinutes() > 10 ) ? date.getMinutes() : ("0" + date.getMinutes() );
    var seconds = (date.getSeconds() > 10 ) ? date.getSeconds() : ("0" + date.getSeconds() );
    var weekday = date.getDay();
    var myDate = String(year)+String(month)+String(day)+"_"+String(hours)+String(minutes)+String(seconds);
    return myDate;
    }
//========================================================================================
//GET DATES
This topic has been closed for replies.
Correct answer Kasyan Servetsky
#targetengine "session"  
app.addEventListener("beforeSave", myBKP);

function myBKP() {
    var doc = app.documents[0];
    var myDate = myDate();
    var myDocName = doc.name.replace(".indd" , "");
    var myPath = Folder.decode(doc.filePath);
    if (!Folder(myPath + "/bkps").exists) {
        Folder(myPath + "/bkps").create();
        myPath = Folder(myPath + "/bkps");
        } else {
            myPath = Folder(myPath + "/bkps");
            }
    var myBkpFileName = new File(myPath + "/" + myDocName + ".idml");
    $.writeln(myBkpFileName);
    doc.exportFile(ExportFormat.INDESIGN_MARKUP, File(myBkpFileName));
	
	function myDate() {
		var date = new Date();
		var day = (date.getDate() > 10 ) ? date.getDate() : ("0" + date.getDate() );
		var month = ((date.getMonth()+1) >= 10 ) ? (date.getMonth()+1) : ("0" + (date.getMonth()+1) );
		var year = (date.getFullYear() > 10 ) ? date.getFullYear() : ("0" + date.getFullYear() );
		var hours = (date.getHours() > 10 ) ? date.getHours() : ("0" + date.getHours() );
		var minutes = (date.getMinutes() > 10 ) ? date.getMinutes() : ("0" + date.getMinutes() );
		var seconds = (date.getSeconds() > 10 ) ? date.getSeconds() : ("0" + date.getSeconds() );
		var weekday = date.getDay();
		var myDate = String(year)+String(month)+String(day)+"_"+String(hours)+String(minutes)+String(seconds);
		return myDate;
		}	
    }

5 replies

Community Expert
February 15, 2020

Also look into this by Marc Autret:

 

1/ InDesign Events
Can We Kill an Event?

http://www.indiscripts.com/post/2018/06/indesign-scripting-forum-roundup-12#hd1sb1

 

And also all the other entries there.

 

Regards,
Uwe Laubender

( ACP )

 

Community Expert
February 15, 2020

Then study properties and methods of object eventListener:

https://www.indesignjs.de/extendscriptAPI/indesign-latest/#EventListeners.html

https://www.indesignjs.de/extendscriptAPI/indesign-latest/#EventListener.html

 

There is a name property. And there is method itemByName() for example.

 

Regards,
Uwe Laubender

( ACP )

Community Expert
February 14, 2020

lf_corullon said:

Also, I tried a

app.activeDocument.removeEventListener("beforeSave" , myBKP);

to remove the eventListener (which, as you can see, I'm not familiar with).

Doesn't work.

 

If you used Kasyan's code this does not work, because he added the listener to app and not to the active document.

 

Regards,
Uwe Laubender

( ACP )

lfcorullon13651490
Legend
February 14, 2020

Yes, I know it. But I just changed the line where I used saveACopy to the exportFile.

And I tried to remove using my custom targetengine, and without it.

Then I tried an alert(app.activeDocument.eventListeners[0].eventType) to realize that this is really the one I added. I just can remove it using app.activeDocument.eventListeners[0].remove().

Kasyan Servetsky
Kasyan ServetskyCorrect answer
Legend
February 13, 2020
#targetengine "session"  
app.addEventListener("beforeSave", myBKP);

function myBKP() {
    var doc = app.documents[0];
    var myDate = myDate();
    var myDocName = doc.name.replace(".indd" , "");
    var myPath = Folder.decode(doc.filePath);
    if (!Folder(myPath + "/bkps").exists) {
        Folder(myPath + "/bkps").create();
        myPath = Folder(myPath + "/bkps");
        } else {
            myPath = Folder(myPath + "/bkps");
            }
    var myBkpFileName = new File(myPath + "/" + myDocName + ".idml");
    $.writeln(myBkpFileName);
    doc.exportFile(ExportFormat.INDESIGN_MARKUP, File(myBkpFileName));
	
	function myDate() {
		var date = new Date();
		var day = (date.getDate() > 10 ) ? date.getDate() : ("0" + date.getDate() );
		var month = ((date.getMonth()+1) >= 10 ) ? (date.getMonth()+1) : ("0" + (date.getMonth()+1) );
		var year = (date.getFullYear() > 10 ) ? date.getFullYear() : ("0" + date.getFullYear() );
		var hours = (date.getHours() > 10 ) ? date.getHours() : ("0" + date.getHours() );
		var minutes = (date.getMinutes() > 10 ) ? date.getMinutes() : ("0" + date.getMinutes() );
		var seconds = (date.getSeconds() > 10 ) ? date.getSeconds() : ("0" + date.getSeconds() );
		var weekday = date.getDay();
		var myDate = String(year)+String(month)+String(day)+"_"+String(hours)+String(minutes)+String(seconds);
		return myDate;
		}	
    }
lfcorullon13651490
Legend
February 13, 2020

Thanks!

The error is because I'm trying a save process during a save process?

Because of this the export works (because it's not a save)?

Kasyan Servetsky
Legend
February 13, 2020

I don't think so: unlike doing it manually in InDesign, you can't 'save it as IDML' by script using the saveACopy() method. You can only 'export it' with exportFile().
By the way, here are a couple of similar scripts:

Save with backup

Save versions

ajabon grinsmith
Community Expert
Community Expert
February 13, 2020

To make the EventListener resident in the application, the description of the scriptengine is required.

 

#targetengine "foo"

 

Try adding this to the beginning of your code:
where foo is your arbitrary name.

lfcorullon13651490
Legend
February 13, 2020

I added the targetengine...

Than the alert appears. But, if I comment the alert line and uncomment the saveACopy line, this error appears.

Any idea?