Copy link to clipboard
Copied
Hello,
I made a short routine which reads some texts from an external text file and search for the corresponding data in an open document.
Every time the routine finds an occurence, it shows a dialog box and fills the content of an "edittext" with some data from the text file.
this is my code:
#target "InDesign-8.0"
var myDoc = app.activeDocument;
//Create colours
try{
myDoc.colors.item("RED").name;
}
catch (myError){
myDoc.colors.add({name:"RED", model:ColorModel.process, colorValue:[15, 100, 100, 0]});
}
var txtFile = File.openDialog("Open .txt file", "Files Txt:*.Txt");
if (txtFile == null) exit();
var txtArr = ReadTxtData(txtFile);// array of data
var foundArticles = 0;
for(i=0; i<txtArr.length; i++){
try{
var myPart, myCode, myDesc, myDesc2;
if(txtArr[0] && txtArr[0] != ""){
var myPart = txtArr[0];
}
if(txtArr[1] && txtArr[1] != ""){
var myCode = txtArr[1];
}
if(txtArr[2] && txtArr[2] != ""){
var myDesc = txtArr[2];
}
if(txtArr[3] && txtArr[3] != ""){
var myDesc2 = txtArr[3];
}
app.changeGrepPreferences = NothingEnum.nothing;
app.findGrepPreferences = NothingEnum.nothing;
app.findGrepPreferences.findWhat = '\\b'+myCode+'\\b';
app.changeGrepPreferences.fillColor = "RED";
var finds = myDoc.findGrep();
if (finds.length > 0) {
app.changeGrep();
foundArticles ++;
var myPage = finds[0].parentTextFrames[0].parentPage;
var myText= finds[0].parentTextFrames[0];
myText.select();
//========================< DIALOG WINDOW >===========================
var w = new Window ("dialog");
w.size = [450,180];
w.margins = 5;
var messPanel = w.add('panel', undefined);
messPanel.size = [430,100];
var buttonPanel = w.add('panel', undefined);
buttonPanel.size = [430,50];
buttonPanel.margins = 5;
buttonPanel.orientation = "column";
buttonPanel.alignChildren = "center";
var okButton = buttonPanel.add ("button", undefined, "OK");
okButton.size = [150,35];
if(myDesc != null && myDesc2 != null){
var mess1 = messPanel.add('edittext', undefined, myPart + " / " + myCode + ": " + myDesc + " - " + myDesc2, {multiline:true});
mess1.size = [430,80];
}else if(myDesc != null){
var mess1 = messPanel.add('edittext', undefined, myPart + " / " + myCode + ": " + myDesc, {multiline:true});
mess1.size = [430,80];
}
w.onShow = function() {
this.frameLocation = [1100,600];
}
w.show();
//=======================< \ DIALOG WINDOW >=========================
}
//Reset the find/change grep preferences after each search.
app.changeGrepPreferences = NothingEnum.nothing;
app.findGrepPreferences = NothingEnum.nothing;
}catch(e){
}
}
if(foundArticles > 0){
alert("Found "+ foundArticles + " Articles");
}
function ReadTxtData(txtFile) {
var line, temp, txtRecord, tempArr;
var txtData = [];
txtFile.open("r");
while (!txtFile.eof) {
line = txtFile.readln();
tempArr = line.split("\t");
txtData.push(tempArr);
}
txtFile.close();
return txtData;
}
The fact is that using a dialog window, I cannot move the document to fit with its contents.
I'd like to use a palette window instead, and pause the routine every time I find an occurrence.
Any idea would be much appreciated.
Thanks in advance,
Marco
Have something to add?