Welcome Dialog

Welcome to the Community!

We have a brand new look! Take a tour with us and explore the latest updates on Adobe Support Community.


Saving or Opening a List not Working inside JavasScript

Enthusiast ,
Jul 02, 2021 Jul 02, 2021

Copy link to clipboard

Copied

Hi Experts,

I'm Trying to built a small system to insert grep queries and save them to seperated file, my questions is :

1- how to select a GREP and show directly in the input area instead of click on print button

2-how to save the list? and how to open it again (in the same indesign file opened folder) or (dialoug)

Thanks a lot in Advance

 

//Save GREP List
var w = new Window ("dialog");
w .text = "GREP List";
var myList = w.add ("listbox", undefined, ["\(.+?\)", "\d+", ".+:", ".+!?", "!?"], {multiselect: true});
var input = w.add ("edittext");
input.active = true;
input.characters = 20;

var inserttxt = w.add ("button", undefined, "Insert", {name: "Insert"});
var del = w.add ("button", undefined, "Delete", {name: "Delete"});
var print = w.add ("button", undefined, "Print");
var savelist = w.add ("button", undefined, "Save", {name: "Save"});

//myList onChange
myList.onChange = function () {
if (myList.selection === null) {
myList.selection = myList.prevSel;
} else {
myList.prevSel = myList.selection.index;
}
}

//Print Item in Edit Text
print.onClick = function () {
for (var i = 0; i < myList.selection.length; i++)
input.text = myList.selection[i].text;
}

//insert item Button
inserttxt.onClick = function () {
insert_item (myList, 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;

}

//Showing Dialog
w.show ();
//Insert Item Function
function insert_item (list_obj, new_item) {
if (list_obj.find (new_item) == null) {
var stop = list_obj.items.length;
var i = 0;
while (i < stop && new_item > list_obj.items[i].text)
i++;
list_obj.add ("item", new_item, i);
}
}

//Saving the List in the Same path of Indesign file
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 + "_GREPList.txt");
writeFile.encoding = 'UTF-8';
writeFile.open('w');
//~ file.write('data here');
writeFile.write (myList.join("\r")+"\r");
writeFile.close();
}

 

GREP List.jpg 

Best
M.Hasanain
TOPICS
Scripting

Views

116

Likes

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

Adobe Community Professional , Jul 02, 2021 Jul 02, 2021
Two issue with your code You have created a modal dialog and added the onclick handler after the show method call. This results in the event handler never being registered and hence your method is never called. Move the onclick method above the show method call Modify your onclick method for the save button as follows savelist.onClick = function () { //Define path and file name var filePathMinusExtension = app.activeDocument.fullName.fsName.substr(0, app.activeDocument.fullName.fsNa...

Likes

Translate

Translate
Adobe Community Professional , Jul 02, 2021 Jul 02, 2021
Try the following del.onClick = function () { myList.removeAll() var filePathMinusExtension = app.activeDocument.fullName.fsName.substr(0, app.activeDocument.fullName.fsName.lastIndexOf(".")); var ReadFile = File(filePathMinusExtension + "_GREPList.txt"); if (ReadFile.exists) { try { ReadFile.open("r"); while (ln = ReadFile.readln()) { myList.add("item", ln); } //Close the file ReadFile.close(); ...

Likes

Translate

Translate
Adobe Community Professional ,
Jul 02, 2021 Jul 02, 2021

Copy link to clipboard

Copied

For directly using the list selection in the textfield you could use the following

myList.onChange = function () {
input.text = input.text + myList.selection
}

I did not understand your second question can you elaborate.

-Manan

Likes

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 ,
Jul 02, 2021 Jul 02, 2021

Copy link to clipboard

Copied

GREP List2.jpg

Thanks a lot Manan, my Second question is how to save a list, i already do the script but its not working!, and i added (Open Button) so how to open the List.txt file and Show it to the user from the Open Button? as showned above (screen capture) ?

Best
M.Hasanain

Likes

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
Adobe Community Professional ,
Jul 02, 2021 Jul 02, 2021

Copy link to clipboard

Copied

Two issue with your code

  1. You have created a modal dialog and added the onclick handler after the show method call. This results in the event handler never being registered and hence your method is never called. Move the onclick method above the show method call
  2. Modify your onclick method for the save button as follows

 

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 + "_GREPList.txt");
    writeFile.encoding = 'UTF-8';
    writeFile.open('w');
    //~ file.write('data here');
	var content = ""
	for(var i = 0; i < myList.items.length; i++)
		content += myList.items[i].toString() + "\r"
    writeFile.write(content);
    writeFile.close();
}

 

This will save the file(does for me) with each entry of the Listbox in a single line.

As far as loading the txt file is concerned, you can do the following

  • Browse to the file using the file objects select method
  • Clear the Listbox. if you want to remove all the items in it and just load entries from the file.
  • Open the file, read it line by line and add it to the Listbox collection

-Manan

Likes

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 ,
Jul 02, 2021 Jul 02, 2021

Copy link to clipboard

Copied

Thank you alot manan, i will try it and let you know

Thanks Again.

Best
M.Hasanain

Likes

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 ,
Jul 02, 2021 Jul 02, 2021

Copy link to clipboard

Copied

Hi Manan, I tried hard to make the Open Button Script, but i think i have issues!, here is my code, the script delete the old list completley but i still cant load the items of the file!, pleae help and thanks in advance :

//Load List Items and Show them
load.onClick = function () {
    //Remove old list items
    for (var i = myList.items.length-1; i > -1; i--)
    myList.remove(myList.items[i]);
    var g = {};
    main();
    g = null;
    var filePathMinusExtension = app.activeDocument.fullName.fsName.substr(0, app.activeDocument.fullName.fsName.lastIndexOf("."));
    var ReadFile = File(filePathMinusExtension + "_GREPList.txt");
	if (ReadFile.exists){
		try{
              ReadFile.open("r");
              g.ReadFile = ReadFile.read ().split("\n");  
              var readcontent = ""
              for(var i = 0; i < myList.items.length; i++)    
              readcontent += myList.items[i].toString() + "\r"
              myList.add(myList.items[i]);
             //Close the file
             ReadFile.close();
             alert("Done, the GREP List Loaded");
             return true;
		}
		catch(errOpen){
			alert("Error. The GREP List file could not be opened.");
			return false;
		}
	}
	else{
		alert("The GREP List file could not be found");
		return false;
	}	
}

 

Best
M.Hasanain

Likes

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 ,
Jul 02, 2021 Jul 02, 2021

Copy link to clipboard

Copied

Hi Manan- i also tried to simpilfy the things but also doesnt work, only remove the old list in UI:

 

 

//Load List Items and Show them
load.onClick = function () {
    //Remove old list items
    for (var i = myList.items.length-1; i > -1; i--)
    myList.remove(myList.items[i]);
    var filePathMinusExtension = app.activeDocument.fullName.fsName.substr(0, app.activeDocument.fullName.fsName.lastIndexOf("."));
    var ReadFile = File(filePathMinusExtension + "_GREPList.txt");
	if (ReadFile.exists){
		try{
              ReadFile.open("r");
              for(var n=0;n<ReadFile.length;n++)
                {
               myList[myList.length++] = ReadFile.read().split("\r");
               myList.add(ReadFile.items[n]);
                };
            //Close the file
             ReadFile.close();
             alert("Done, the GREP List Loaded");
             return true;
		}
		catch(errOpen){
			alert("Error. The GREP List file could not be opened.");
			return false;
		}
	}
	else{
		alert("The GREP List file could not be found");
		return false;
	}	
}

 

 

 

Best
M.Hasanain

Likes

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
Adobe Community Professional ,
Jul 02, 2021 Jul 02, 2021

Copy link to clipboard

Copied

Try the following

del.onClick = function () {
    myList.removeAll()
    var filePathMinusExtension = app.activeDocument.fullName.fsName.substr(0, app.activeDocument.fullName.fsName.lastIndexOf("."));
    var ReadFile = File(filePathMinusExtension + "_GREPList.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 List Loaded");
            return true;
        }
        catch (errOpen) {
            alert("Error. The GREP List file could not be opened.");
            return false;
        }
    }
    else{
        alert("The GREP List file could not be found");
        return false;
    }
}

-Manan

Likes

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 ,
Jul 02, 2021 Jul 02, 2021

Copy link to clipboard

Copied

LATEST

That was Genius and Simple, Thanks a lot for your help, this is was first time playing with external txt files, thanks a lot, 

STAR Credits for you.

Best
M.Hasanain

Likes

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