Skip to main content
Participant
December 22, 2023
Answered

How to catch the variable from edittext for further use

  • December 22, 2023
  • 3 replies
  • 448 views

Hey,

I'm new to scripting and at the moment I got a problem and I have no idea so far.

I know the function of variables, how to create and connect further actions to it
in easy scripts.

 

At the moment I'm working on a little helper for using GREP-expressions.

My code works for the common GREPs I want to add (see below).

Checkbox 1 and 7 got already an grep expression. (Other ones will follow.)

 

The challenging one is checkbox 5 – a editable field – for me. The user
should enter a unit as miles or cm. Then I will do a GREP-Lookaround.

Find: (?<=\d)(\x{0020})(?=cm) -> Replace: ~S

(Change the normal blank to a non-breaking blank).

The cm in this case should come from the variable in checkbox 5.

 

Can you help me to integrate the varibale and to execute it?

 

Best regards,

Julian

 

 

//target indesign
var doc = app.activeDocument;

// DIALOG
// ======
var dialog = new Window("dialog"); 
    dialog.text = "GREP Toolbox"; 
    dialog.preferredSize.width = 320; 
    dialog.orientation = "column"; 
    dialog.alignChildren = ["center","top"]; 
    dialog.spacing = 10; 
    dialog.margins = 16; 

// PANEL1
// ======
var panel1 = dialog.add("panel", undefined, undefined, {name: "panel1"}); 
    panel1.text = "Beschreibung"; 
    panel1.orientation = "column"; 
    panel1.alignChildren = ["left","top"];
    panel1.preferredSize.width = 320; 
    panel1.spacing = 10; 
    panel1.margins = 13; 

// GROUP1
// ======
var group1 = panel1.add("group", undefined, {name: "group1"}); 
    group1.orientation = "row"; 
    group1.alignChildren = ["left","center"]; 
    group1.spacing = 10; 
    group1.margins = 0; 

var statictext1 = group1.add("group", undefined , {name: "statictext1"}); 
    statictext1.getText = function() { var t=[]; for ( var n=0; n<statictext1.children.length; n++ ) { var text = statictext1.children[n].text || ''; if ( text === '' ) text = ' '; t.push( text ); } return t.join('\n'); }; 
    statictext1.orientation = "column"; 
    statictext1.alignChildren = ["left","center"]; 
    statictext1.spacing = 0; 

    statictext1.add("statictext", undefined, "Dieses Skript kann die folgenden GREP-Befehle"); 
    statictext1.add("statictext", undefined, "ausführen. Immer ein Back-Up des Dokuments"); 
    statictext1.add("statictext", undefined, "erstellen. Datenverlust möglich!"); 

// PANEL2
// ======
var panel2 = dialog.add("panel", undefined, undefined, {name: "panel2"}); 
    panel2.text = "GREP-Funktionen "; 
    panel2.preferredSize.width = 320; 
    panel2.orientation = "column"; 
    panel2.alignChildren = ["left","top"]; 
    panel2.spacing = 8; 
    panel2.margins = 13; 

var checkbox1 = panel2.add("checkbox", undefined, undefined, {name: "checkbox1"}); 
    checkbox1.text = "Mehrfache Leerzeichen entfernen"; 

var checkbox2 = panel2.add("checkbox", undefined, undefined, {name: "checkbox2"}); 
    checkbox2.text = "Mehfache Wortzwischenräume entfernen"; 

var checkbox3 = panel2.add("checkbox", undefined, undefined, {name: "checkbox3"}); 
    checkbox3.text = "Mehrfache Umbrüche entfernen"; 

var divider1 = panel2.add("panel", undefined, undefined, {name: "divider1"}); 
    divider1.alignment = "fill"; 

var checkbox4 = panel2.add("checkbox", undefined, undefined, {name: "checkbox4"}); 
    checkbox4.text = "Einheiten und wichtige Begriffe schützen"; 

// GROUP2
// ======
var group2 = panel2.add("group", undefined, {name: "group2"}); 
    group2.orientation = "row"; 
    group2.alignChildren = ["left","center"]; 
    group2.spacing = 10; 
    group2.margins = 0; 

var checkbox5 = group2.add("checkbox", undefined, undefined, {name: "checkbox5"}); 
    checkbox5.text = "Einheit manuell schützen:"; 

var edittext1 = group2.add('edittext {properties: {name: "edittext1"}}'); 
    edittext1.text = "Wert einfügen"; 
    edittext1.preferredSize.width = 112; 

// PANEL2
// ======
var checkbox6 = panel2.add("checkbox", undefined, undefined, {name: "checkbox6"}); 
    checkbox6.text = "Gedankenstriche schützen"; 

var checkbox7 = panel2.add("checkbox", undefined, undefined, {name: "checkbox7"}); 
    checkbox7.text = "UND-Zeichen schützen";

var checkbox8 = panel2.add("checkbox", undefined, undefined, {name: "checkbox8"}); 
    checkbox8.text = "Datumsangaben schützen"; 

var checkbox9 = panel2.add("checkbox", undefined, undefined, {name: "checkbox9"}); 
    checkbox9.text = "Akronyme schützen"; 

var divider2 = panel2.add("panel", undefined, undefined, {name: "divider2"}); 
    divider2.alignment = "fill"; 

var checkbox10 = panel2.add("checkbox", undefined, undefined, {name: "checkbox10"}); 
    checkbox10.text = "Zahlen mit Achtelgevierten versehen"; 

var checkbox11 = panel2.add("checkbox", undefined, undefined, {name: "checkbox11"}); 
    checkbox11.text = "Französische Anführungszeichen anwenden"; 

// GROUP3
// ======
var group3 = dialog.add("group", undefined, {name: "group3"}); 
    group3.preferredSize.height = 34; 
    group3.orientation = "row"; 
    group3.alignChildren = ["left","center"]; 
    group3.spacing = 10; 
    group3.margins = 0; 

var button1 = group3.add("button", undefined, undefined, {name: "button1"}); 
    button1.text = "OK"; 
    button1.preferredSize.width = 90; 

var button2 = group3.add("button", undefined, undefined, {name: "button2"}); 
    button2.text = "Abbrechen"; 
    button2.preferredSize.width = 90;
    button2.onClick = function(){
        dialog.close();
    }

var button3 = group3.add("button", undefined, undefined, {name: "button3"}); 
    button3.text = "Info"; 
    button3.preferredSize.width = 90;
    button3.onClick = function(){
//Infobox
var newDialog = new Window("dialog", "Infobox");
    newDialog.preferredSize.width = 200; 
    newDialog.orientation = "column"; 
    newDialog.alignChildren = ["center","top"]; 
    newDialog.spacing = 10; 
    newDialog.margins = 16; 
        
// TPANEL1
// =======
var tpanel1 = newDialog.add("tabbedpanel", undefined, undefined, {name: "tpanel1"}); 
    tpanel1.alignChildren = "fill"; 
    tpanel1.preferredSize.width = 200; 
    tpanel1.margins = 0; 

// TAB1
// ====
var tab1 = tpanel1.add("tab", undefined, undefined, {name: "tab1"}); 
    tab1.text = "Kontakt"; 
    tab1.orientation = "column"; 
    tab1.alignChildren = ["center","center"]; 
    tab1.spacing = 10; 
    tab1.margins = 10; 

// GROUP1
// ======
var group1 = tab1.add("group", undefined, {name: "group1"}); 
    group1.orientation = "row"; 
    group1.alignChildren = ["left","center"]; 
    group1.spacing = 10; 
    group1.margins = 0; 

var statictext1 = group1.add("group", undefined , {name: "statictext1"}); 
    statictext1.getText = function() { var t=[]; for ( var n=0; n<statictext1.children.length; n++ ) { var text = statictext1.children[n].text || ''; if ( text === '' ) text = ' '; t.push( text ); } return t.join('\n'); }; 
    statictext1.preferredSize.width = 200; 
    statictext1.orientation = "column"; 
    statictext1.alignChildren = ["left","center"]; 
    statictext1.spacing = 0; 

    statictext1.add("statictext", undefined, "Version 1.0 "); 
    statictext1.add("statictext", undefined, ""); 
    statictext1.add("statictext", undefined, "Diese Bibliothek wird"); 
    statictext1.add("statictext", undefined, "fortlaufend erweitert. "); 
    statictext1.add("statictext", undefined, ""); 
    statictext1.add("statictext", undefined, "Name"); 

// TAB2
// ====
var tab2 = tpanel1.add("tab", undefined, undefined, {name: "tab2"}); 
    tab2.text = "Changelog"; 
    tab2.orientation = "column"; 
    tab2.alignChildren = ["left","top"]; 
    tab2.spacing = 10; 
    tab2.margins = 10; 

// TPANEL1
// =======
tpanel1.selection = tab1; 

// GROUP2
// ======
var group2 = tab2.add("group", undefined, {name: "group2"}); 
    group2.orientation = "row"; 
    group2.alignChildren = ["left","center"]; 
    group2.spacing = 10; 
    group2.margins = 0; 

var statictext2 = group2.add("group", undefined , {name: "statictext2"}); 
    statictext2.getText = function() { var t=[]; for ( var n=0; n<statictext2.children.length; n++ ) { var text = statictext2.children[n].text || ''; if ( text === '' ) text = ' '; t.push( text ); } return t.join('\n'); }; 
    statictext2.preferredSize.width = 200; 
    statictext2.orientation = "column"; 
    statictext2.alignChildren = ["left","center"]; 
    statictext2.spacing = 0; 

    statictext2.add("statictext", undefined, "Version 1.0 "); 
    statictext2.add("statictext", undefined, "Dies ist die erste Version "); 
    statictext2.add("statictext", undefined, "für die Produktion."); 

// DIALOG
// ======
var button1 = newDialog.add("button", undefined, undefined, {name: "button1"}); 
    button1.text = "OK"; 
    newDialog.show();
};

var result = dialog.show();
if (result === 1) {
    // First search:
    if (checkbox1.value) {
        app.findGrepPreferences = NothingEnum.nothing;
        app.changeGrepPreferences = NothingEnum.nothing;

        app.findGrepPreferences.findWhat = "\\x{0020}{2,}";
        app.changeGrepPreferences.changeTo = "\\x{0020}";

        doc.changeGrep();
    }

    // Seventh search:
    if (checkbox7.value) {
        app.findGrepPreferences = NothingEnum.nothing;
        app.changeGrepPreferences = NothingEnum.nothing;

        app.findGrepPreferences.findWhat = "\\x{0020}+&";
        app.changeGrepPreferences.changeTo = "~S&";

        doc.changeGrep();
    }
    {
    alert("Aktion(en) erfolgreich ausgeführt.");
}

    // Clear the find/change preferences after the search
    app.findGrepPreferences = NothingEnum.nothing;
    app.changeGrepPreferences = NothingEnum.nothing;

} 

 

 

This topic has been closed for replies.
Correct answer Peter Kahrel

You can't do that like that, Eugene, Julian wants to invoke InDesign's GREP change, not JavaScript's. Besides, changing the text involves replacing the text's content, so you'd lose all formatting and special markers -- you end up with plain text.

 

I think you're after something like this, Julian:

 

app.findGrepPreferences.findWhat = "(?<=\\d)(\\x{0020})(?=" + edittext1.text + ")"

 

By the way, " The user should enter a unit as miles or cm" -- miles? 

3 replies

Peter Kahrel
Community Expert
Community Expert
December 23, 2023

> What are the functions of the + signs around the variable edittext1.text?

 

You need to construct an expression by combining some text and some variable content, and you combine those pieces with the + signs.

Peter Kahrel
Community Expert
Peter KahrelCommunity ExpertCorrect answer
Community Expert
December 23, 2023

You can't do that like that, Eugene, Julian wants to invoke InDesign's GREP change, not JavaScript's. Besides, changing the text involves replacing the text's content, so you'd lose all formatting and special markers -- you end up with plain text.

 

I think you're after something like this, Julian:

 

app.findGrepPreferences.findWhat = "(?<=\\d)(\\x{0020})(?=" + edittext1.text + ")"

 

By the way, " The user should enter a unit as miles or cm" -- miles? 

Community Expert
December 23, 2023

I had a feeling it wasn't good - thanks for clearing it up. 
It's quite difficult this stuff - kudos to you

Community Expert
December 23, 2023

Not the best scrpter on the forums by  a longshot - just throwing a suggestion your way to see if it helps

var checkboxUnit = checkbox5.value;

var regex = new RegExp(r"(?<=\d)(\x{0020})(?=%s)", "g");

var text = app.activeDocument.text;
text.replace(regex, "~S");

app.findGrepPreferences = NothingEnum.nothing;