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

beforeSave or afterSave eventListeners

Enthusiast ,
Feb 12, 2020 Feb 12, 2020

Copy link to clipboard

Copied

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
TOPICS
Scripting

Views

1.5K

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

Guru , Feb 13, 2020 Feb 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 + "/" 
...

Votes

Translate

Translate
Community Expert ,
Feb 12, 2020 Feb 12, 2020

Copy link to clipboard

Copied

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.

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
Enthusiast ,
Feb 13, 2020 Feb 13, 2020

Copy link to clipboard

Copied

ScreenCapture_13022020_142500.jpg

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?

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
Guru ,
Feb 13, 2020 Feb 13, 2020

Copy link to clipboard

Copied

#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;
		}	
    }

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
Enthusiast ,
Feb 13, 2020 Feb 13, 2020

Copy link to clipboard

Copied

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)?

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
Guru ,
Feb 13, 2020 Feb 13, 2020

Copy link to clipboard

Copied

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

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
Enthusiast ,
Feb 13, 2020 Feb 13, 2020

Copy link to clipboard

Copied

Thanks again. I'm just studying.

I tried the saveACopy because it's available to be done manually.

I think is just a extension thing. Now, it's understood!

 

Thank you so much, Kas!

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
Enthusiast ,
Feb 13, 2020 Feb 13, 2020

Copy link to clipboard

Copied

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. So, I check the eventListener active (using eventListeners[0].eventType) and then removing it using eventListeners[0].remove() (jut because the first try doesn't remove the event and it keep saving copies...

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
Guru ,
Feb 14, 2020 Feb 14, 2020

Copy link to clipboard

Copied

Off the top of my head, I think a more elegant solution would be creating a menu item with a checkbox. When it's on, the eventListener is added, off — removed.

Also, the menu item can be disabled if no documents open.

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
Enthusiast ,
Feb 14, 2020 Feb 14, 2020

Copy link to clipboard

Copied

Yes. For sure. But, as I said, just studying...

I need to understand better this eventListeners... =D

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
Enthusiast ,
Feb 15, 2020 Feb 15, 2020

Copy link to clipboard

Copied

LATEST

Maybe it's better now...

Thank you so much Kas and Uwe (and Marc)!

 

var myEL_action = app.scriptMenuActions.add("Save backups");
myEL_action.eventListeners.add("onInvoke" , active_deactive_myEL);

if (!app.menus.item("$ID/Main").submenus.item("byLFC").isValid) {
    var myMenuLFC = app.menus.item("$ID/Main").submenus.add("byLFC" , LocationOptions.AT_END);
    } else {
        var myMenuLFC = app.menus.item("$ID/Main").submenus.item("byLFC");
        }

if (!myMenuLFC.menuItems.item("Save backups").isValid) {
    var myBkpsBtn = myMenuLFC.menuItems.add(myEL_action);
    }
//========================================================================================
//========================================================================================
function active_deactive_myEL() {
    if (myMenuLFC.menuItems.itemByName("Save backups").checked == true) {
        app.scriptMenuActions.item("Save backups").checked = false;
        for (var i=app.eventListeners.length-1; i>=0; i--) {
            if (app.eventListeners[i].name == "1307") {
                app.eventListeners[i].remove();
                }
            }
        }
    else {
        app.scriptMenuActions.item("Save backups").checked = true;
        var myEL = app.addEventListener("beforeSave" , myBKP);
        myEL.name = "1307";
        }
    }
//========================================================================================
//========================================================================================
function myBKP(check) {
    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 + "_" + myDate + ".idml");

    doc.exportFile(ExportFormat.INDESIGN_MARKUP, 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
    }

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 ,
Feb 14, 2020 Feb 14, 2020

Copy link to clipboard

Copied

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 )

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
Enthusiast ,
Feb 14, 2020 Feb 14, 2020

Copy link to clipboard

Copied

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().

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 ,
Feb 15, 2020 Feb 15, 2020

Copy link to clipboard

Copied

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 )

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 ,
Feb 15, 2020 Feb 15, 2020

Copy link to clipboard

Copied

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 )

 

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