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();
}
Two issue with your code
savelist.onClick = function () {
//Define path and file name
var filePathMinusExtension = app.activeDocument.fullName.fsName.substr(0, app.activeDocument.fullName.fsNam
...
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();
...
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
Copy link to clipboard
Copied
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) ?
Copy link to clipboard
Copied
Two issue with your code
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
-Manan
Copy link to clipboard
Copied
Thank you alot manan, i will try it and let you know
Thanks Again.
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;
}
}
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;
}
}
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
Copy link to clipboard
Copied
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.