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

Event Handling/Creating

Participant ,
Dec 19, 2016 Dec 19, 2016

Copy link to clipboard

Copied

Hey all

I am trying to find some documentation around Event Handlers/Listeners etc, but have not found anything I can interpret fully for my purpose.

My terminology may be completely wrong below, but please humour me!

I am wanting to add a way at the end of a current script, to trigger off an editText area in an already scriptUI panel to update itself with the contents of a text file that was changed during the running of the first script. Lets call it a log file for augments sake.

I don't know where event Handlers, triggers, listeners etc need to be placed if this is even possible.

Thanks in advance for any help on this matter.

Roy

TOPICS
Scripting

Views

521

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

People's Champ , Dec 20, 2016 Dec 20, 2016

Hi Roy,

Mainly issue of yours is that ScriptUI has no timer internal function (that I know of at least). So you can only rely on InDesign events and I guess the perfect candidate for this are idleEvents.

#targetengine "readLog"

#include "EventHandler.jsinc"

function main(){

  var ts, onIdleEventListener,

  onIdleEventHandler = function (myIdleEvent){

  var log  = File ( Folder.desktop +"/log.txt" );

  if (!log.exists ||  ) {

  EventManager.dispatchEvent ( "LOG_UPDATE", {log:"No log yet"} );

  return;

  }

 

...

Votes

Translate

Translate
People's Champ ,
Dec 20, 2016 Dec 20, 2016

Copy link to clipboard

Copied

Hi Roy,

Mainly issue of yours is that ScriptUI has no timer internal function (that I know of at least). So you can only rely on InDesign events and I guess the perfect candidate for this are idleEvents.

#targetengine "readLog"

#include "EventHandler.jsinc"

function main(){

  var ts, onIdleEventListener,

  onIdleEventHandler = function (myIdleEvent){

  var log  = File ( Folder.desktop +"/log.txt" );

  if (!log.exists ||  ) {

  EventManager.dispatchEvent ( "LOG_UPDATE", {log:"No log yet"} );

  return;

  }

  if ( ts && log.modified==ts ) {

  return;

  }

  ts = log.modified;

  log.encoding ="UTF-8";

  log.open('r');

  var c =log.read();

  log.close();

  EventManager.dispatchEvent ( "LOG_UPDATE", {log:c} );

  };

  var myIdleTask = app.idleTasks.item("logger");

  if ( !myIdleTask.isValid ) {

  myIdleTask = app.idleTasks.add({name:"logger", sleep:100});

  myIdleTask.addEventListener(IdleEvent.ON_IDLE, onIdleEventHandler, false);

  }

  ui.show();

  EventManager.dispatchEvent ( "LOG_UPDATE", {log:"No log yet"} );

}

var ui = (function(){

  var w = new Window('palette'),

  bloc = w.add('edittext', undefined, '', {multiline:true});

  w.preferredSize = [300, 200];

  w.alignChildren = ["fill","fill"];

  EventManager.addEventListener ( "LOG_UPDATE", function(data) {

  bloc.text = data.log;

  });

  return w;

})();

main();

Download full script here.

Capture d’écran 2016-12-20 à 19.40.47.png

Capture d’écran 2016-12-20 à 19.40.26.png

HTH

Loic

Ozalto | Productivity Oriented - Loïc Aigon

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
Participant ,
Dec 20, 2016 Dec 20, 2016

Copy link to clipboard

Copied

Hey Loic

Thanks for the comprehensive response.

On initial execution, this does exactly what I was looking for.

I will dissect this to get a full understanding of the events, as they are still 'magic' for me at the moment, but I know I can use the power if I understand it.

Thanks again

Roy

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
People's Champ ,
Dec 20, 2016 Dec 20, 2016

Copy link to clipboard

Copied

LATEST

Basically the idleEvent will be fired every time InDesign will remain idle for 100ms thus executing the event handler. They will be fired more often than some afterOpen event because the latter can be fired only once in a while.

Those events and the app ones are well documented in the scripting guides.

Loic

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