//DESCRIPTION: Collect GREP results //Peter Kahrel -- www.kahrel.plus.com #target indesign; if (parseInt (app.version) > 4 && app.documents.length > 0) chain_queries (); function chain_queries () { $.localize = true; // This is pointless in scriptUI dialogs var found; var array = []; var locale_strings = define_strings (); // returns four-element object: the selected queries, the search scope, test_only value, and includee_attrib var obj = select_queries (locale_strings); // return a manageable string var search = find_domain (obj.scope); message_txt = create_message (30, "Running queries"); // If attributes should be included, add a doc and return a text frame ("output") if (obj.test == true && obj.include_attrib == true) var output = create_output (app.documents[0].name); for (var i = 0; i < obj.selected_queries.length; i++) { message_txt.text = obj.selected_queries[i]; app.loadFindChangeQuery (obj.selected_queries[i], SearchModes.grepSearch); if (search instanceof Array) for (var j = 0; j < search.length; j++) { if (obj.test == true) { f = search[j].parentStory.findGrep () array = add_found (array, f, obj.include_attrib, output) } else search[j].parentStory.changeGrep () } else { if (obj.test == true) array = add_found (array, eval (search + ".findGrep ()"), obj.include_attrib, output); else eval (search + ".changeGrep ()") } } if (obj.test == true) { if (obj.include_attrib == false) { message_txt.text = localize (locale_strings.dlg_writing); write_found (array); } else app.activeWindow = output.parent.parent.parent.layoutWindows[0]; } message_txt.parent.close (); } function add_found (array, found, attr, text_frame) { if (attr == false) { for (var i = 0; i < found.length; i++) array.push (found[i].contents); return array } else for (var i = 0; i < found.length; i++) { found[i].duplicate (LocationOptions.after, text_frame.insertionPoints[-1]); text_frame.insertionPoints[-1].contents = "\r" } } function write_found (array) { var s = array.sort().join('\r')+'\r'; s = s.replace (/([^\r]+\r)(\1)+/g,'$1'); s = s.replace (/\r$/,''); flow (s, 2) } function flow (s, col) { var doc = app.documents.add(); doc.viewPreferences.rulerOrigin = RulerOrigin.pageOrigin; var marg = doc.pages[0].marginPreferences; var gb = [marg.top, marg.left, app.documentPreferences.pageHeight - marg.bottom, app.documentPreferences.pageWidth - marg.right]; doc.textFrames.add ({geometricBounds: gb, contents: s, textFramePreferences: {textColumnCount: col}}); while (doc.pages[-1].textFrames[0].overflows) { doc.pages.add().textFrames.add ({geometricBounds: gb, textFramePreferences: {textColumnCount: col}}); doc.pages[-1].textFrames[0].previousTextFrame = doc.pages[-2].textFrames[0]; } } function create_output (n) { var newdoc = app.documents.add (); newdoc.viewPreferences.rulerOrigin = RulerOrigin.pageOrigin; var marg = newdoc.pages[0].marginPreferences; var gb = [marg.top, marg.left, app.documentPreferences.pageHeight - marg.bottom, app.documentPreferences.pageWidth - marg.right]; var f = newdoc.textFrames.add ({geometricBounds: gb, textFramePreferences: {textColumnCount: 2}}); app.activeWindow = app.documents.item (n).layoutWindows[0]; return f } function find_domain (s) { switch (s) { case "Document" : case "Dokument" : return "app.documents[0]"; case "All documents": case "Tous les documents": case "Alle dokumente": return "app"; case "Story": case "Article": case "Textabschnitt": return "app.selection[0].parentStory"; case "Stories": case "Articles": case "Textabschnitte": return app.selection; // can't return a string for eval: we have two or more text frames //~ case "To end of story": //~ case "Jusqu'à la fin de l'article": //~ case "Zum Ende des Textabschnitts": //~ return "app.selection[0].parentStory.insertionPoints.itemByRange (app.selection[0], app.selection[0].parentStory.insertionPoints[-1])"; case "Selection": case "Sélection": case "Auswahl": return "app.selection[0]"; } } function select_queries (loc) { var history = app.scriptPreferences.scriptsFolder + "/chain_queries_history.txt"; var queries = find_queries (); var previous = previous_run (history); var search_list = get_list (loc); var w = new Window ("dialog", localize (loc.dlg_title), undefined, {resizeable: true}); w.alignChildren = ["fill", "fill"]; var list = w.add ("listbox", undefined, queries, {multiselect: true}); list.preferredSize = [300, 500]; var scope = w.add ("group"); scope.add ("statictext", undefined, localize (loc.dlg_search)); var search_domain = scope.add ("dropdownlist", undefined, search_list.list); search_domain.selection = search_list.preselect; search_domain.minimumSize.width = 250; var test = w.add ("checkbox", undefined, localize (loc.dlg_test_only)); var attrib = w.add ("checkbox", undefined, localize (loc.dlg_include_format)); var buttons = w.add ("group"); buttons.alignChildren = ["right", "bottom"]; buttons.orientation = 'row'; buttons.add ("button", undefined, "OK", {name: "ok"}); buttons.add ("button", undefined, localize (loc.dlg_cancel), {name: "cancel"}); if (previous.length > 0) { for (var i = 0; i < queries.length; i++) if (previous.search ("|" + queries[i] + "|") > -1) list.items[i].selected = true; } function get_selected_queries (listbox) { var array = []; for (var i = 0; i < listbox.items.length; i++) if (listbox.items[i].selected) array.push (listbox.items[i].text) return array } attrib.enabled = false; test.onClick = function () { if (this.value == true) attrib.enabled = true; else { attrib.value = false; attrib.enabled = false } } if (w.show () == 1) { var array = get_selected_queries (list) save_run (history, array, attrib.value); return {selected_queries: array, scope: search_domain.selection.text, test: test.value, include_attrib: attrib.value} } else exit() } function find_queries () { var user_folder = Folder (app.scriptPreferences.scriptsFolder.parent.parent + "/Find-Change Queries/Grep"); var user_queries = user_folder.getFiles ("*.xml"); if (parseInt (app.version) == 5) var app_folder = Folder (app.filePath + "/Presets/Find-Change Queries/Grep/"); else var app_folder = Folder (app.filePath + "/Presets/Find-Change Queries/Grep/" + $.locale + "/"); var app_queries = app_folder.getFiles ("*.xml"); var array = user_queries.concat (app_queries); for (var i = 0; i < array.length; i++) array[i] = decodeURI (array[i].name).replace (/\.xml$/, ""); return array } function get_list (o) { var list = [localize (o.dlg_all_documents), localize (o.dlg_document)]; var preselect = 1; if (app.selection.length > 0) { if (app.selection.length > 1 && app.selection[0] instanceof TextFrame) { list.push (localize (o.dlg_stories)); return {preselect: 2, list: list} } if (app.selection.length ==1 && app.selection[0] instanceof TextFrame) { list.push (localize (o.dlg_story)); return {preselect: 2, list: list} } if (app.selection[0] instanceof InsertionPoint) { // "to_end_of_story doesn't work list = list.concat ([localize (o.dlg_story) /*, localize (o.dlg_to_end_of_story)*/ ]); return {preselect: 2, list: list} } if ("TextWordParagraph".search (app.selection[0].constructor.name) > -1) { list = list.concat ([localize (o.dlg_story), localize (o.dlg_selection)]); return {preselect: 3, list: list} } } return {preselect: preselect, list: list} } function previous_run (s) { var f = File (s); if (f.exists) { f.open ("r"); var temp = f.read (); f.close (); return temp; } else return "" } function save_run (s, array) { var f = File (s); f.open ("w"); f.write ("|" + array.join ("|") + "|"); f.close (); } function create_message (le, title) { message_dlg = new Window('palette', title); message_dlg.alignChildren = ['left', 'top']; var message_txt = message_dlg.add('statictext', undefined, ''); message_txt.characters = le; message_dlg.show(); return message_txt; } function define_strings () { return { dlg_title: {en: "Chain GREP queries", fr: "Enchaîner requêtes GREP", de: "GREP Abfragen ketten"}, dlg_search: {en: "Search:", fr: "Chercher dans:", de: "Suchen:"}, dlg_cancel: {en: "Cancel", fr: "Annuler", de: "Abbrechen"}, dlg_document: {en: "Document", fr: "Document", de: "Dokument"}, dlg_all_documents: {en: "All documents", fr: "Tous les documents", de: "Alle Dokumente"}, dlg_story: {en: "Story", fr: "Article", de: "Textabschnitt"}, dlg_stories: {en: "Stories", fr: "Articles", de: "Textabschnitte"}, dlg_to_end_of_story: {en: "To end of story", fr: "Jusqu'à la fin de l'article", de:"Zum Ende des Textabschnitts"}, dlg_selection: {en: "Selection", fr: "Sélection", de: "Auswahl"}, dlg_writing: {en: "Writing...", fr: "Écrire...", de: "Schreiben..."}, dlg_include_format: {en: " Include formatting", fr: " Conserver la mise en forme", de: " Einschliesslich Formatierung"}, dlg_test_only: {en: " Test only", fr: " Seulement évaluer", de: " Nur testen"} } }