Skip to main content
Inspiring
June 17, 2016
Answered

How to make this check box Apply to all Documents?

  • June 17, 2016
  • 3 replies
  • 1791 views

I'm sure this is probably an easy question.  I've Added another Check BOx (Apply to all Documents) and when its selected i want the changes of the check boxes below it to chage for all open documents.

var RunCH1, RunCH2, RunCH3, RunCH6, RunCH8;

            RunCH8= app.documents[0], 

            allPagesMaPref = RunCH8.pages.everyItem()  ; 

            RunCH8= w.tabs[1].add ('checkbox {text: "APPLY TO ALL DOCUMENTS"}');

            RunCH1= w.tabs[1].add ('checkbox {text: "Find and Replace"}');

Thank you in advance.

This topic has been closed for replies.
Correct answer Loic.Aigon

I know little to nothing about grep.  What does this do?  When i activate it the Apply button does the changes to the documents and other than some s's being removed i'm not sure what all it does.  I only need the "Apply/cb1CallBack" CB to Apply to all documents if.

cb1CallBack + cb2CallBack    OR      

cb1CallBack + cb3CallBack    OR

cb1CallBack + cb2CallBack + cb3CallBack etc...

and if just the cb1CallBack is unchecked then cb2CallBack, cb3CallBack, etc... will only apply to the active document(which this is how it works now).

BTW thank you for all your doing here.  I'm learning A TON.


Fixed some typos but basically your "all/none" checkbox is a modifier for the source object onto which call the changeGrep function.

Ok you have a lot of material now, so it's up to you now.

//Our Pseudo Event Class   

var EventManager = function()   

{   

   

  //Our internal key object   

  var o  = {   

   

  };   

   

   

  //return the Event Manager API   

  return {   

   

  //Add a listener to the class for a specific event   

  addEventListener:function(name, handler) {   

   

  //Return if the supplied handler already belongs to the listened event   

  if ( this.hasEventListener (name, handler) ) return;   

   

  //Adding the handler for the event   

  o[name] = o[name] || [];   

  o[name][o[name].length]=handler;   

   

  },   

   

  //Executes every handler attached to the event   

  //passing data as argument   

  dispatchEvent:function(name, data) {   

   

  var n, i=0;    

   

  //Return if no handler is set   

  if ( !o[name] || !(o[name] instanceof Array) || !o[name].length ) return;   

   

  n = o[name].length;   

   

  //executes every handler   

  for ( i = 0; i<n; i++ ) {   

  o[name](data);   

  }   

  },   

   

  //Remove a specific handler for the event   

  removeEventListener:function(name, handler) {   

   

  //We will redefine the internal chain of handlers   

  //passing the content of the "new" newArray array   

  var newArray = [], n;   

   

  //Return if no handler is set   

  if ( !this.hasEventListener ( name, handler ) ) return;   

   

  n = o[name].length;   

   

  //Storing handlers into newArray if different from specific handler   

  while ( n-- ) {   

  if ( (o[name] !== handler) ) {   

  newArray [ newArray.length ] = o[name];   

  }   

  }   

   

  //Redefining handlers array   

  o[name] = newArray ;   

   

  },   

   

  //Checking if some event host a specific handler   

  hasEventListener:function(name, handler) {   

  var n;   

   

  if ( !o[name] || !(o[name] instanceof Array) || !o[name].length ) return false;   

   

  n = o[name].length;   

   

  //If handle is found, return true. Otherwise return false   

  while ( n-- ) if ( o[name] === handler ) return true;   

   

  return false;   

  },   

   

  //Some utility to get access to the handler array for some specific event or all the eventManager class   

  expose:function(name){   

  if ( name ) {   

  if ( !o[name] || !(o[name] instanceof Array) || o[name.length] ) return "no events set";   

  else return o[name].toSource();   

  }   

  else {   

  return o.toSource();   

  }   

  }   

  }   

};

var main = function() {   

  var ev = new EventManager(),   

  u, actions = {};

  var CBX = function(p, label, v, callback) {   

  var cbx = p.add('checkbox', undefined, label);  

  cbx.onClick = function() { 

  ev.dispatchEvent ( (cbx.value? "ADD_ACTION":"REMOVE_ACTION"), {label:label, callback:callback} ); 

  } 

  return cbx;   

  }; 

  var w = new Window ('dialog','About CBX & EVENTS');   

  var p1 = w.add('panel', u,'MAIN CBX');   

  new CBX ( p1, 'F/C 1', false, cb1CallBack );

  //adding main checkbox that once click will dispatch the "DEFINE_TARGET" event

  var mainCbx = w.add('checkbox',undefined, 'Apply to all documents');

  mainCbx.onClick = function() {

  ev.dispatchEvent ( "DEFINE_TARGET", mainCbx.value );

  }

  var btn = w.add('button',u, 'Run' );   

  btn.onClick = function () {   

  w.close(1);   

  }   

  ev.addEventListener ( "ADD_ACTION", function(data) { 

  ev.actions = ev.actions || {}; 

  ev.actions[data.label] = data.callback; 

  }); 

 

  ev.addEventListener ( "REMOVE_ACTION", function(data) { 

  if ( !ev.actions) return; 

  delete ev.actions[data.label]; 

  }); 

  ev.target = app.properties.activeDocument;

  //add a listener so any event of type "DEFINE_TARGET" dispatched set ev.target value;

  ev.addEventListener ( "DEFINE_TARGET", function(data) {  

  ev.target = data? app : app.properties.activeDocument;

  });

  if ( w.show() == 1 && ev.actions) {   

 

  //looping through all properties of the ev.actions object 

  //which are functions 

 

  for ( prop in ev.actions ) {   

  ev.actions[prop](ev.target);   

  }   

  }   

}

function grep(target, findProps, changeProps) { 

  app.findGrepPreferences = app.changeGrepPreferences = null; 

  app.findGrepPreferences.properties = findProps; 

  app.changeGrepPreferences.properties = changeProps; 

  target.changeGrep(); 

 

 

function cb1CallBack(target) { 

  var findProps = { 

  findWhat:"\\s{2, }", 

  }, 

  changeProps = { 

  changeTo:" ", 

  }; 

 

 

  grep (target,  findProps, changeProps ); 

if ( !app.properties.activeDocument ) {

  alert('you need an open document at least');

}

else {

  main();

}

 

   

 

3 replies

cbishop01Author
Inspiring
June 21, 2016

Ok one more update.  GOt the FInd/replace to work.  I have to take it out of the function {}.  SO now i just need it to Apply to all documetns once that check box is selected and i'm pretty much done.  Oh and i do like the way this script handels Checkboxes.  it is cleaner and easier to manage.  SO thank you..

Loic.Aigon
Legend
June 21, 2016

Glad it helped.

cbishop01Author
Inspiring
June 22, 2016

I cannot find how to make this apply button work for every document when its checked.  I've tried searching here to look for the coding but i'm not seeing it. Also.

This did break one thing that i need.  It makes all my changes at once.  I only want it to make the changes of the selected Checkbox.  How can i do that with this code?  

cbishop01Author
Inspiring
June 21, 2016

If you need to see the full code let me know and i'll paste it. 

ava74628296
Inspiring
June 18, 2016

RunCH8.onClick  = function(){

                if(RunCH8.value == true){

                    RunCH1.value = true;

                }

                else{

                        RunCH1.value = false;

                }

Loic.Aigon
Legend
June 20, 2016

In this kind of stuff, I would definitively avoid having to tell any of the instances to switch true/false all the time.

Here is a proposal based on a event programming approach:

var main = function() {

  var ev = new EventManager(),

  u;

  var cbx = function(p, label, v) {

  var cbx = p.add('checkbox', undefined, label);

  var mainHandler = function(data){

  cbx.value = data.value;

  };

  cbx.onClick = function () {

  alert("Hey I am "+cbx.text );

  }

  //Adding event listeners

  ev.addEventListener ("MAIN_FIRED", mainHandler  );

  return cbx;

  };

  var w = new Window ('dialog','About CBX & EVENTS');

  var p1 = w.add('panel', u,'MAIN CBX');

  var cbxMain = p1.add('checkbox',u,'MAIN');

  cbxMain.onClick = function() {

  ev.dispatchEvent ("MAIN_FIRED", {value:cbxMain.value} );

  };

  var i = 0, n=20;

  while ( i<n ) {

  new cbx ( p1, 'Child '+(i+1), false );

  i++;

  }

  w.show();

}

main();

Using a library of mine:

//Our Pseudo Event Class

var EventManager = function()

{

  //Our internal key object

  var o  = {

  };

  //return the Event Manager API

  return {

  //Add a listener to the class for a specific event

  addEventListener:function(name, handler) {

  //Return if the supplied handler already belongs to the listened event

  if ( this.hasEventListener (name, handler) ) return;

  //Adding the handler for the event

  o[name] = o[name] || [];

  o[name][o[name].length]=handler;

  },

  //Executes every handler attached to the event

  //passing data as argument

  dispatchEvent:function(name, data) {

  var n, i=0;

  //Return if no handler is set

  if ( !o[name] || !(o[name] instanceof Array) || !o[name].length ) return;

  n = o[name].length;

  //executes every handler

  for ( i = 0; i<n; i++ ) {

  o[name](data);

  }

  },

  //Remove a specific handler for the event

  removeEventListener:function(name, handler) {

  //We will redefine the internal chain of handlers

  //passing the content of the "new" newArray array

  var newArray = [], n;

  //Return if no handler is set

  if ( !this.hasEventListener ( name, handler ) ) return;

  n = o[name].length;

  //Storing handlers into newArray if different from specific handler

  while ( n-- ) {

  if ( (o[name] !== handler) ) {

  newArray [ newArray.length ] = o[name];

  }

  }

  //Redefining handlers array

  o[name] = newArray ;

  },

  //Checking if some event host a specific handler

  hasEventListener:function(name, handler) {

  var n;

  if ( !o[name] || !(o[name] instanceof Array) || !o[name].length ) return false;

  n = o[name].length;

  //If handle is found, return true. Otherwise return false

  while ( n-- ) if ( o[name] === handler ) return true;

  return false;

  },

  //Some utility to get access to the handler array for some specific event or all the eventManager class

  expose:function(name){

  if ( name ) {

  if ( !o[name] || !(o[name] instanceof Array) || o[name.length] ) return "no events set";

  else return o[name].toSource();

  }

  else {

  return o.toSource();

  }

  }

  }

};

which produces the expected result:

And yet allow some differenciation at the child level

Loic

Ozalto | Productivity Oriented - Loïc Aigon

cbishop01Author
Inspiring
June 20, 2016

Thank you. Just for my knowledge. What would the draw back to using the True False statement?