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

Scripting - EventListener on changing of ActiveDocument

Engaged ,
May 23, 2024 May 23, 2024

Hi guys,
I'm enjoying more and more scripting and managed to create a few scripts already that help improve my productivity. Right now, I need your insight to figure out if something is possible within a script. If yes, then how would you tackle the problem? So far, I haven't succeeded to make it happen.

Note that I'm just trying to make parts of the scripts better, let's say more "automatic".

 

- Is it possible to get something done automatically when we change the Active Document when a script is already "active" (ie changing tabs, opening a new file)?

 

What are we talking about here :

- I've got a script running via a palette  (a window always open then, that only closes when I want to)

- I've got an EditText in this palette that gathers the name of the Active Document, among other things.

- When I run the script, it gets automatically the name of the ActiveDocument
- Since it's a palette, I can swap to another document/ tab (or open a new file), without losing the script.
- But on the palette, the content of the EditText stays the same until I hit a button named "Refresh" that runs a function which gathers the name of the new Active Document. It works fine, but it's obviously manual

 

I would like to update the content of this damn EditText upon the opening/ change of documents.

I've tried things like ActiveDocument.addEventListener("onChange", … with no avail.
And I've found this page on the forum that doesn't give me much hope… but it's 7yo so maybe…

Where is there a list of InDesign Event types? 


Any thoughts ?

Thanks for your time and help

TOPICS
Scripting
1.6K
Translate
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
LEGEND ,
May 23, 2024 May 23, 2024

What is your end goal? 

 

Translate
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
Engaged ,
May 23, 2024 May 23, 2024

Hi,

Thank you for your interest

Let's indeed explain a bit more about what this does in the end

 

The end goal is related to a bad behavior of mine, meaning forgetting to hit the refresh button. Since it appears to not be intuitive enough, I'd like to make this button less important.

The script is about quiclky exporting multiple PDFs/ jpg (see image atached to get a hint about it) silently using specific settings (.joboptions / location/custom folder etc.)

The workflow is like this:

- I open 10 documents that i need to handle  

- I run the script

- I select my option and hit the export button

At the moment, I need to hit the refresh button to update the name of the active Document in order to create the Pds with the proper names. If I forget that part, I get files with the wrong content

Translate
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
LEGEND ,
May 24, 2024 May 24, 2024

@Fred.L

 

But you don't need to hit Refresh button - in order to get Active Document - you can get it when you click Export. 

 

InDesign updates reference automatically. 

 

Translate
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
Engaged ,
May 23, 2024 May 23, 2024

Here's something that might work, but I have some serious doubts about the practicality. If you're willing to be the guinea pig...

 

#target InDesign
#targetengine session

app.addEventListener("afterOpen", aoFunction);

function aoFunction(evt) {
	alert(app.activeDocument.name);
	if (evt.target.constructor.name==="LayoutWindow") {
		app.activeWindow.addEventListener("afterSelectionChanged",ascFunction);
	} 
}

function ascFunction(evt){
	if (evt.target.constructor.name==="LayoutWindow") {
	alert(app.activeDocument.name);
	}
}

 

The first function is pretty straightforward: When a new LayoutWindow is .open, it displays the document name. But it also attaches an afterSelectionChanged event listener to that LayoutWIndow, which also displays the active document name.

 

So when you open document 1, you see the name. (Instead of using alert(), you can figure out how to display this name in your script window). When you open document 2, you get its name. If you've left document 1 open and click on that tab, you see the document 1 name again. That's the afterSelectionChanged handler in action.

My reservation is this: The afterSelectionChanged handler is going to fire every time the user clicks the mouse. Or types into a text frame. It doesn't take long for the script to figure out whether the target is a LayoutWindow, but I wouldn't be shocked to hear that InDesign seems to get bogged down with this script running.
\

I haved some scripts that dynamically update when the user switches from one open document to another, but they are attached to ScriptUI dropdown menus. The trick there is that the update is done by a function attached to the .onChange method of a popup menu.

Translate
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
Engaged ,
May 24, 2024 May 24, 2024

Hey,

 

Thanks for the tips

I see some good behaviour already. I do get a feedback with the name of the Active Document. It's great!

The only odd thing  though is that, so far, I noticed that it only works for the documents opened AFTER the run of the script, not on all the opened document, regardless on when they were there.

For exemple:

- I open 2 docs (Doc1, Doc2)

- I run the script

- I open 2 more Docs (Doc3, Doc4)

- If I switch from Doc3 to Doc4, the script behaves properly. If I switch to Doc1 or Doc2, nothing happens

Any reason in your opinion?

Translate
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
Engaged ,
May 24, 2024 May 24, 2024

I should have mentioned: Put the script in your Startup Scripts folder so that it's active throughout your session. 

The aftrrSelectionChanged function is attached to each LayoutWindow by the afterOpen function. In your example, the afterOpen function wasn't running when Doc1 and Doc2 were opened, so the afterSelectionChanged function was not attached to those Layout Windows. 

If you want to activate this script after you've opened some documents, you could add some run time code to loop through the open documents and attach the afterSelectionChanged function to each LayoutWindow. 

Translate
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 ,
May 25, 2024 May 25, 2024
LATEST

- If I switch from Doc3 to Doc4, the script behaves properly. If I switch to Doc1 or Doc2, nothing happens

 

afterActivate would get the document or layout window when it becomes active—should work whether you are opening or cycling through already open docs:

 
#targetengine "session"
var el = app.eventListeners.add("afterActivate", getActive);

/**
* Gets the document name when it becomes active
* @ param the event 
*/
function getActive(e){
    if (e.target.constructor.name=="Document") {
        alert(e.target.name + " is now active")
    } 
}

 

 

Screen Shot 41.png

Translate
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 ,
May 24, 2024 May 24, 2024

I would like to update the content of this damn EditText upon the opening/ change of documents.

I've tried things like ActiveDocument.addEventListener("onChange", … with no avail.
And I've found this page on the forum that doesn't give me much hope… but it's 7yo so maybe…

Where is there a list of InDesign Event types? 

 

Hi @Fred.L , The listeners Marc is referring to in your link are specific to the scriptUI object. InDesign has listeners that can be attached to objects. For example here is a list of document events:

 

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

 

So you could listen for an export to complete and get the document’s name like this:

 

#targetengine "session"

var el = app.eventListeners.add("afterExport", exportDone);

/**
* Function to run after an Export
* @ param the event 
*/
function exportDone(e){
    alert(e.parent.name + " has been Exported")
}

 

Screen Shot 40.png

Translate
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
LEGEND ,
May 24, 2024 May 24, 2024

@rob day

 

I'm pretty sure @Fred.L needs the name of the current / active document BEFORE exporting - so he can set options and then export document.

 

Translate
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