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

Delete and Load Funcions not working in ListBox Script UI

Enthusiast ,
Mar 04, 2023 Mar 04, 2023

Copy link to clipboard

Copied

Hi Experts

its just very tiny tool for saving GREP Queries, previously @Manan Joshi  helped me in creating it , now i add a title using multi columns lisbox in script UI so every grep query to be saved with its own title!, i  try to change tha code , i succeeded only in modifying inserting and saving function but i stucked in the Load and Delete functions, i still learning javascript so i get stucked from time to time! , i apperciate any help to make those functions works and thanks in advance :

var w = new Window ("dialog");
var myList = w.add ("listbox", undefined, " ",
{numberOfColumns: 2, showHeaders: true, columnTitles: ["GREP Name", "GREP Formula"]});

//MyListSize
myList.maximumSize.height = 1000;
myList.maximumSize.width = 1000;
myList.minimumSize.width = 1000;

//GREP Name
var StaticaName = w.add("statictext", undefined,"GREP Name");
StaticaName.alignment = "Left";
var myname = w.add ("edittext");
myname.active = true;
myname.characters = 100;

//GREP Formula
var StaticaGREP = w.add("statictext", undefined,"GREP Formula");
var input = w.add ("edittext");
StaticaGREP.alignment = "Left";
input.active = false;
input.characters = 100;

//-----------------------------------------------------------------------------------//
//Buttons Group 1
//-----------------------------------------------------------------------------------//
var gqmButtonGroup1 = w.add ('group {orientation: "row"}');
gqmButtonGroup1.alignChildren = "fill";
//GQMButtonGroup1.alignment = "stack";
//------------------------------------------------------------------------------------//
var inserttxt = gqmButtonGroup1.add ("button", undefined, " Insert GREP Query", {name: "Insert"});
var del = gqmButtonGroup1.add ("button", undefined, " Delete GREP Query", {name: "Delete"});
var gqmButtonGroup2 = w.add ('group {orientation: "row"}');
gqmButtonGroup2.alignChildren = "fill";
//GQMButtonGroup2.alignment = "stack";
var load = gqmButtonGroup2.add ("button", undefined, " Load GREP Queries", {name: "Load"});
var savelist = gqmButtonGroup2.add ("button", undefined, " Save GREP Queries", {name: "Save"});

//insert item Button
inserttxt.onClick = function () {
    insert_item (myList, myname.text);
    //insert_item (myList, input.text);
    input.text = "";
    input.text = "";
    input.active = true;
}

//delete selected item
del.onClick = function () {  
    // remember which line is selected
    var sel = myList.selection[0].index;
    for (var i = myList.selection.length-1; i > -1; i--)
    myList.remove(myList.selection[i]);

    // select a line after deleting one or more items
    if (sel > myList.items.length-1)
    myList.selection = myList.items.length-1;
    else
    myList.selection = sel;
}

function insert_item (list_obj, new_item) {
    if (list_obj.find (new_item) == null) {
    var stop = list_obj.items.length;
    //w.add(myList);
    var i = 0;
    while (i < stop && new_item > list_obj.items[i].text)
    i++;
    with (list_obj.add ("item", new_item, i)){
        subItems[0].text = input.text;
        }
    }
}

//Saving the List in the Same path of Indesign file (must before Show Command)
savelist.onClick = function () {
    //Define path and file name
    var filePathMinusExtension = app.activeDocument.fullName.fsName.substr(0, app.activeDocument.fullName.fsName.lastIndexOf("."));
    var writeFile = File(filePathMinusExtension + "_GREP_Queries.txt");
    writeFile.encoding = 'UTF-8';
    writeFile.open('w');
	var content = ""
	for(var i = 0; i < myList.items.length; i++)
        content += myList.items[i].toString() +","+ myList.items[i].subItems[0].toString() + "\r"
    writeFile.write(content);
    writeFile.close();
    alert("Done, File Saved in Same InDesign Document Location", "Alert!");
}

//Load List Items and Show them
load.onClick = function () {
    //Remove old list items
    myList.removeAll()
    var filePathMinusExtension = app.activeDocument.fullName.fsName.substr(0, app.activeDocument.fullName.fsName.lastIndexOf("."));
    var ReadFile = File(filePathMinusExtension + "_GREP_Queries.txt");
    if (ReadFile.exists) {
        try {
            ReadFile.open("r");

            while (ln = ReadFile.readln()) {
                myList.add("item", ln);
            }
            //Close the file
            ReadFile.close();
            alert("Done, the GREP Queries Loaded");
            return true;
        }
        catch (errOpen) {
            alert("Error. The GREP Queries file could not be opened!");
            return false;
        }
    }
    else{
        alert("The GREP Queries file could not be found!");
        return false;
    }
}

w.show ();

  

Best
Mohammad Hasanin
TOPICS
Scripting

Views

589

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 2 Correct answers

Enthusiast , Mar 05, 2023 Mar 05, 2023

Hi All

Now i discovered a solution to remove the items from the list box from trail from error, and it works! for removing item and subitem :

 

//delete selected item 
del.onClick = function () {
     //Remove Items
     myList.remove(myList.selection.text);
     myList.remove(myList.selection.subItems[0].text);
}

 

Votes

Translate

Translate
Enthusiast , Mar 05, 2023 Mar 05, 2023

Thanks alot friends! , i spent some time to squeez my mind and i figured it out, here is the loading solution, it works!

load.onClick = function () {
    //Remove old list items
    myList.removeAll()
    var filePathMinusExtension = app.activeDocument.fullName.fsName.substr(0, app.activeDocument.fullName.fsName.lastIndexOf("."));
    var ReadFile = File(filePathMinusExtension + "_GREP_Queries.txt");
    if (ReadFile.exists) {
        try {
            ReadFile.open("r");
            //
         
...

Votes

Translate

Translate
People's Champ ,
Mar 04, 2023 Mar 04, 2023

Copy link to clipboard

Copied

Maybe you should provide a more global explanation of what you are trying to achieve. Having code is great but without the big picture, we can only shoot in the dark.

What about screenshots/images of what you have now vs what you want to achieve? Once that is clarified, suggesting code edits will be more meaningful.

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
Enthusiast ,
Mar 04, 2023 Mar 04, 2023

Copy link to clipboard

Copied

Thanks @Loic.Aigon for your reply, here is a screen shot to clarify concept, see here (the yellow buttons) are attached with the funciton (onClick) those Buttons linked to funcitons (Delete Grep Query) not working also (Load Grep Queries) not working also

ListBox.png

so this function not working (Delete GREP Query) :

//delete selected item
del.onClick = function () {  
    // remember which line is selected
    var sel = myList.selection[0].index;
    for (var i = myList.selection.length-1; i > -1; i--)
    myList.remove(myList.selection[i]);

    // select a line after deleting one or more items
    if (sel > myList.items.length-1)
    myList.selection = myList.length-1;
    else
    myList.selection = sel;
}

also this function not working (Load GREP Query) :

//Load List Items and Show them
load.onClick = function () {
    //Remove old list items
    myList.removeAll()
    var filePathMinusExtension = app.activeDocument.fullName.fsName.substr(0, app.activeDocument.fullName.fsName.lastIndexOf("."));
    var ReadFile = File(filePathMinusExtension + "_GREP_Queries.txt");
    if (ReadFile.exists) {
        try {
            ReadFile.open("r");

            while (ln = ReadFile.readln()) {
                myList.add("item", ln);
            }
            //Close the file
            ReadFile.close();
            alert("Done, the GREP Queries Loaded");
            return true;
        }
        catch (errOpen) {
            alert("Error. The GREP Queries file could not be opened!");
            return false;
        }
    }
    else{
        alert("The GREP Queries file could not be found!");
        return false;
    }
}

these functions need to be modified to work with multi columns listbox, i hope this clear and thanks again for help

Best
Mohammad Hasanin

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
Enthusiast ,
Mar 05, 2023 Mar 05, 2023

Copy link to clipboard

Copied

Hi All

Now i discovered a solution to remove the items from the list box from trail from error, and it works! for removing item and subitem :

 

//delete selected item 
del.onClick = function () {
     //Remove Items
     myList.remove(myList.selection.text);
     myList.remove(myList.selection.subItems[0].text);
}

 

Best
Mohammad Hasanin

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
Enthusiast ,
Mar 05, 2023 Mar 05, 2023

Copy link to clipboard

Copied

LATEST

Thanks alot friends! , i spent some time to squeez my mind and i figured it out, here is the loading solution, it works!

load.onClick = function () {
    //Remove old list items
    myList.removeAll()
    var filePathMinusExtension = app.activeDocument.fullName.fsName.substr(0, app.activeDocument.fullName.fsName.lastIndexOf("."));
    var ReadFile = File(filePathMinusExtension + "_GREP_Queries.txt");
    if (ReadFile.exists) {
        try {
            ReadFile.open("r");
            //
            var i = 0;
            while (ln = ReadFile.readln()) {
                var arr = ln.split(',');
                with (myList.add ("item", arr[i]+"\n")){
                    subItems[0].text = arr[1+i]+"\n";
                }                 
            }
            //Close the file
            ReadFile.close();
            alert("Done, the GREP Queries Loaded");
            return true;
        }
        catch (errOpen) {
            alert("Error. The GREP Queries file could not be opened!");
            return false;
        }
    }
    else{
        alert("The GREP Queries file could not be found!");
        return false;
    }
}

 

Best
Mohammad Hasanin

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