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

Running Find/Change queries via dialog window

Contributor ,
Aug 22, 2024 Aug 22, 2024

Copy link to clipboard

Copied

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();
    
  }




TOPICS
How to , Scripting , UXP Scripting

Views

213

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 ,
Aug 22, 2024 Aug 22, 2024

Copy link to clipboard

Copied

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?

 

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
Contributor ,
Aug 22, 2024 Aug 22, 2024

Copy link to clipboard

Copied

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();
    
  }

 





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
Contributor ,
Aug 30, 2024 Aug 30, 2024

Copy link to clipboard

Copied

@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();
}

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 ,
Aug 31, 2024 Aug 31, 2024

Copy link to clipboard

Copied

LATEST

I wonder if radio buttons would be better for something like this?

 

For simple dialogs I prefer the built in InDesign dialog class—you don‘t have to setup onClick listeners, and it takes less code.

 

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

 

Something like this:

 

makeDialog();
//result variables
var altq, q1, q2, q3;  
function makeDialog(){
    var theDialog = app.dialogs.add({name:"Find/Change Queries", canCancel:true});
    with(theDialog)
        with(dialogColumns.add()){
            staticTexts.add({staticLabel:"Choose a Query", staticAlignment:StaticAlignmentOptions.LEFT_ALIGN, minWidth: 250});
        with(borderPanels.add()){
            with(radiobuttonGroups.add()){
                q1 = radiobuttonControls.add({checkedState:false});
                q2 = radiobuttonControls.add({checkedState:false});
                q3 = radiobuttonControls.add({checkedState:false});
            }
            with(dialogColumns.add()){
                staticTexts.add({staticLabel:"Query Description 1"});
                staticTexts.add({staticLabel:"Query Description 2"});
                staticTexts.add({staticLabel:"Query Description 3"});
            }   
        }
        with(dialogRows.add()){
            altq = checkboxControls.add({checkedState:false});
            staticTexts.add({staticLabel:"Also Run (Query Description)"});
        }
    }
    //get the dialog results
    var res = theDialog.show();
    if(res == true){
        altq = altq.checkedState;
        q1 = q1.checkedState
        q2 = q2.checkedState
        q3 = q3.checkedState
        main()
	}else{
        theDialog.destroy();
    }
}


function main(){
    if (q1) {
        alert("Run Query 1 code")
    } else if (q2) {
        alert("Run Query 2 code")
    } else if (q3) {
        alert("Run Query 3 code")
    } 
    if (altq) {
        alert("Run Alt Query code")
    } 
}

 

 

Screen Shot 1.png

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 ,
Aug 22, 2024 Aug 22, 2024

Copy link to clipboard

Copied

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();*/
}

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
Contributor ,
Aug 22, 2024 Aug 22, 2024

Copy link to clipboard

Copied

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

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