Skip to main content
Inspiring
August 22, 2024
Question

Running Find/Change queries via dialog window

  • August 22, 2024
  • 2 replies
  • 738 views

Hi everyone,

I'm working on a script which runs Find/Change queries via a dialog window where each query has its own painel/button. The thing is that, only the 1st button works and when I click on it, it runs 'query 3' instead of 'query 1'.  When I close the window, the script still runs 'query3', something that it's not supposed to do. I assume there are issues with 'myWindow.close' and 'myWindow.show'? Should I use another structure for the Find/Change function codes (e.g.: starting with If(...) etc.)?

See below a short version of the script - the final version will have several queries and lots of formatting attributes to Find/Change in the code. For now, I just need to figure out how to fix the mentioned issues. Could anyone please help me? Thanks in advance, Rogerio.

// My dialog window
var myWindow = new Window("dialog", "Title");
myWindow.text = "Find/Change Queries";
myWindow.preferredSize = [300,300];
myWindow.alignChildren = ["center","center"];
myWindow.orientation = "column"; 
myWindow.spacing = 5; 
myWindow.margins = 15; 

// Create panels and buttons

var panel1 = myWindow.add("panel");
panel1.text = "Query description 1"
var query1 = panel1.add("button", undefined, "Run query 1");
panel1.spacing = 10;
panel1.margins = 20;

var panel2 = myWindow.add("panel");
panel2.text = "Query description 2"
var query2 = panel2.add("button", undefined, "Run query 2");
panel2.spacing = 10;
panel2.margins = 20;

var panel3 = myWindow.add("panel");
panel3.text = "Query description 3"
var query3 = panel3.add("button", undefined, "Run query 3");
panel3.spacing = 10;
panel3.margins = 20;

// Query 1

query1.onClick = function() {
  myWindow.close();
}

myWindow.show();

main();

function main(){

    var doc = app.activeDocument;
   
    app.findGrepPreferences = app.changeGrepPreferences = null;
    app.findGrepPreferences.findWhat = "Grep expression 1";
    app.changeGrepPreferences.horizontalScale = 88;
    app.changeGrepPreferences.verticalScale = 88;
    app.findChangeGrepOptions.includeMasterPages = true;
    doc.changeGrep();
  
}

// Query 2

query2.onClick = function() {
    myWindow.close();
  }
  
  myWindow.show();
  
  main();
  
  function main(){
  
      var doc = app.activeDocument;
     
      app.findGrepPreferences = app.changeGrepPreferences = null;
      app.findGrepPreferences.findWhat = "Grep expression 2";
      app.changeGrepPreferences.horizontalScale = 89;
      app.changeGrepPreferences.verticalScale = 89;
      app.findChangeGrepOptions.includeMasterPages = true;
      doc.changeGrep();
    
  }

  // Query 3

query3.onClick = function() {
    myWindow.close();
  }
  
  myWindow.show();
  
  main();
  
  function main(){
  
      var doc = app.activeDocument;
     
      app.findGrepPreferences = app.changeGrepPreferences = null;
      app.findGrepPreferences.findWhat = "Grep expression 3";
      app.changeGrepPreferences.horizontalScale = 90;
      app.changeGrepPreferences.verticalScale = 90;
      app.findChangeGrepOptions.includeMasterPages = true;
      doc.changeGrep();
    
  }




This topic has been closed for replies.

2 replies

rob day
Community Expert
Community Expert
August 22, 2024

Hi @Rogerio5C09 , Try setting a result variable at the top to capture the button clicked, something like this:

 

var res;
var q1 = "Query description 1";
var q2 = "Query description 2"
var q3 = "Query description 3"

// My dialog window
var myWindow = new Window("dialog", "Title");
myWindow.text = "Find/Change Queries";
myWindow.preferredSize = [300,300];
myWindow.alignChildren = ["center","center"];
myWindow.orientation = "column"; 
myWindow.spacing = 5; 
myWindow.margins = 15; 

// Create panels and buttons

var panel1 = myWindow.add("panel");
panel1.text = q1
var query1 = panel1.add("button", undefined, "Run query 1");
panel1.spacing = 10;
panel1.margins = 20;

var panel2 = myWindow.add("panel");
panel2.text = q2
var query2 = panel2.add("button", undefined, "Run query 2");
panel2.spacing = 10;
panel2.margins = 20;

var panel3 = myWindow.add("panel");
panel3.text = q3
var query3 = panel3.add("button", undefined, "Run query 3");
panel3.spacing = 10;
panel3.margins = 20;

// Query Buttons

query1.onClick = function() {
  myWindow.close();
  res = q1;
  main()
}

query2.onClick = function() {
  myWindow.close();
  res = q2;
  main()
}

query3.onClick = function() {
  myWindow.close();
  res = q3;
  main()
}

myWindow.show();


function main(){
  $.writeln("Button Pressed Result: " + res)
  var gf;
  if (res == "Query description 1") {
    gf = "grep expression to run"
  } 
  if (res == "Query description 2") {
    gf = "grep expression to run"
  } 
  if (res == "Query description 3") {
    gf = "grep expression to run"
  } 

  $.writeln(gf)
  /* var doc = app.activeDocument;
 
  app.findGrepPreferences = app.changeGrepPreferences = null;
  app.findGrepPreferences.findWhat = gf;
  app.changeGrepPreferences.horizontalScale = 88;
  app.changeGrepPreferences.verticalScale = 88;
  app.findChangeGrepOptions.includeMasterPages = true;
  doc.changeGrep();*/
}
Inspiring
August 22, 2024

Hi @rob day, this also works! Thank you so much! 🙂

Robert at ID-Tasker
Legend
August 22, 2024

There is a free Find Change by List script included with InDesign. You can either use it - or convert it for your needs.

 

Also, I'm not JS guy - but you've declared main() 3x times in your code...

 

Then:

 

 

query1.onClick = function() {
  myWindow.close();
}

myWindow.show();

main();

 

 

This declares that myWindow should be closed when button is clicked, then myWindow should be shown - then main() should be executed.

 

And you have this repeated 3x times.

 

So - I think - you should rather rename all main() functions to query1(), query2(), query3() and call them from inside onClick - rather than outside.

 

 

 

// Query 2

query2.onClick = function() {
    myWindow.close();
    query2();
  }
  
  myWindow.show();
  
function query2()
{
      var doc = app.activeDocument;
     
      app.findGrepPreferences = app.changeGrepPreferences = null;
      app.findGrepPreferences.findWhat = "Grep expression 2";
      app.changeGrepPreferences.horizontalScale = 89;
      app.changeGrepPreferences.verticalScale = 89;
      app.findChangeGrepOptions.includeMasterPages = true;
      doc.changeGrep();    
}

 

 

 

I'm not sure about the myWindow.show() - I'm not JS guy - it probably should be shown only once, after the dialog has been created?

 

Inspiring
August 22, 2024

Hi @Robert at ID-Tasker , thanks for checking! The main() functions were indeed one of the issues. In order to fix it, I had to add a variable for each button that closes the dialog and runs its respective function. That did the trick! 🙂

 

// My dialog window
var myWindow = new Window("dialog", "Title");
myWindow.text = "Find/Change Queries";
myWindow.preferredSize = [300,300];
myWindow.alignChildren = ["center","center"];
myWindow.orientation = "column"; 
myWindow.spacing = 5; 
myWindow.margins = 15; 

// Set variables for each function
var query1 = '';
var query2 = '';
var query3 = '';

// Create panels and buttons

var panel1 = myWindow.add("panel");
panel1.text = "Query description 1"
var button1 = panel1.add("button", undefined, "Run query 1");
panel1.spacing = 10;
panel1.margins = 20;

var panel2 = myWindow.add("panel");
panel2.text = "Query description 2"
var button2 = panel2.add("button", undefined, "Run query 2");
panel2.spacing = 10;
panel2.margins = 20;

var panel3 = myWindow.add("panel");
panel3.text = "Query description 3"
var button3 = panel3.add("button", undefined, "Run query 3");
panel3.spacing = 10;
panel3.margins = 20;

// If a button is clicked, change the variable and close the dialog
button1.onClick = function() {query1 = '1'; myWindow.close()};
button2.onClick = function() {query2 = '2'; myWindow.close()};
button3.onClick = function() {query3 = '3'; myWindow.close()};

myWindow.show();

// Query 1

if (query1 != '') grep1(query1);

function grep1(){

    var doc = app.activeDocument;
   
    app.findGrepPreferences = app.changeGrepPreferences = null;
    app.findGrepPreferences.findWhat = "Grep expression 1";
    app.changeGrepPreferences.horizontalScale = 88;
    app.changeGrepPreferences.verticalScale = 88;
    app.findChangeGrepOptions.includeMasterPages = true;
    doc.changeGrep();
  
}

// Query 2

if (query2 != '') grep2(query2);

function grep2(){
  
      var doc = app.activeDocument;
     
      app.findGrepPreferences = app.changeGrepPreferences = null;
      app.findGrepPreferences.findWhat = "Grep expression 2";
      app.changeGrepPreferences.horizontalScale = 89;
      app.changeGrepPreferences.verticalScale = 89;
      app.findChangeGrepOptions.includeMasterPages = true;
      doc.changeGrep();
    
  }

  // Query 3

  if (query3 != '') grep3(query3);

  function grep3(){
  
      var doc = app.activeDocument;
     
      app.findGrepPreferences = app.changeGrepPreferences = null;
      app.findGrepPreferences.findWhat = "Grep expression 3";
      app.changeGrepPreferences.horizontalScale = 90;
      app.changeGrepPreferences.verticalScale = 90;
      app.findChangeGrepOptions.includeMasterPages = true;
      doc.changeGrep();
    
  }

 





Inspiring
August 30, 2024

@rob day, I would like to include an optional check box that can used along with any of the buttons - if it returns true, the script should run an additional query before the main one (e.g: before function grep1). For now, I was able to come up with the codes below:

// Optional check box (needs a variable)

myWindow.add("checkbox", undefined, "Perfom additional query");

// If true, run additional query (incomplete)
if (...);

function additionalGrep(){

var doc = app.activeDocument;

app.findGrepPreferences = app.changeGrepPreferences = null;
app.findGrepPreferences.findWhat = "Grep expression";
app.changeGrepPreferences.fillColor=app.swatches.item("Yelow");
app.findChangeGrepOptions.includeMasterPages = true;
doc.changeGrep();
}