If you use GREP queries a lot, it's easy to lose track of what's in which query. The script displays an overview of all GREP queries, showing each query's name, find expression, and change expression. The script aims to address various shortcomings of InDesign's possibilities to manage queries, such as InDesign's inability to chain existing queries and to edit GREP expressions conveniently. The script is a panel, so you can keep it open and work in InDesign.
Note: For a much simpler script, one that simply runs selected queries, see here.
The script displays the contents of all GREP queries in the current user's folder as shown in the screenshot. It looks in the user's folder only. Information is displayed in three columns. Query: the name of the query on disk, i.e. the name under which you originally saved the query; Find what: the contents of the Find what field in the Find/Change folder; Change to: the contents of the Change to field in the Find/Change folder. If a field has no contents, the script shows that by printing ---- for clarity. The functions of the buttons are explained below.
The script's window can be resized; the width of the columns can be changed in the normal way, too: click in the header row on a column separator and drag it left or right. (You'll notice that if you've resized the columns and then resize the window, the columns snap back to their original widths; this is a bug in the script language, not the script.)
The script's functionality is contained in the three panels on the right-hand side of the script's window.
Any number of queries can be grouped in a chain. When you execute a chain, all queries in the chain are processed in the order in which they occur in the list on the left.
To create a chain, select some queries in the main window and press . Provide a name and press OK:
Chains are small files which are stored in the user's Grep query folder, using the extension .qrpq. To delete a chain, press -- this deletes just the chain, not the queries.
When you select a stored chain from the dropdown, the queries defined in it are selected in the list.
Press Execute queries to execute the queries selected in the list. (These could have been chosen manually or by picking a chain from the dropdown.) Queries are executed in the order in which you see them in the list. If you want to change the order of execution, you'll have to rename the queries (using the Rename query button in the bottom panel).
Because the script executes queries using InDesign's Find/Change, all formatting defined in queries is respected and applied, even though you can't see that formatting in the script's interface. But open the Find/Change dialog and you can see the query's format settings in the Find Format and Change Format panels.
The script's scope, too, is determined by the Find/Change dialog: Document, All documents, Story, Selection, etc.
Rather than executing the selected queries, you can instead collect whatever the query or queries match and print the found results in a new file. Your document is not changed at all. To collect the found items, check Collect found items. The found items are listed in a new document, sorted, and any duplicates and formatting are removed. To leave formatting intact, check Include format.
You can opt to add the numbers of the pages where each query was found (thanks to Laurent Tournier for this idea).
The third panel offers some options to manipulate a query. The first two buttons in the panel (Edit query in GREP editor and Query to F/C dialog) are disabled when more than one query is selected, as in the screenshot.
Press Edit query in GREP editor to load the Find what part of the query in the GREP editor, which is a separate script. (If the script can't be found the button is disabled; the Grep editor and its icons must be in the same folder as the Grep manager.)
In addition, the script has a simple built-in editor that can be used simply to display a long query that doesn't fit in the list, but also to make changes to the GREP expression. To use this built-in editor, select a query, then press :
The Find expression is shown in the upper panel, the replace expression in the bottom one. Use the ¶ button to add a new line (you can't use the Enter key because of restrictions of the script language). To copy and paste, right-click in the window and pick Copy or Paste from the context menu. Exit the window either by pressing Save changes or press Cancel to ignore any changes.
You can copy the Find what part of queries to GREP styles. To do so, select all the queries you want to copy, then press Query to F/C dialog. The script displays a dialog which lists all the document's paragraph and character styles. Select the paragraph styles to which you want to copy the GREP queries as GREP styles, then select the character style to be used in the GREP style and press OK to commit:
In this example, the GREP expressions in two selected queries (range-phonetic and range-symbol) are copied to the three selected paragraph styles, and the character style specials is assigned to the GREP expressions.
The last panel has four buttons that can be used to manage queries.
You can rename queries () and duplicate () them; the script uses a simple dialog for this:
Queries can also be deleted (); you'll be asked to confirm the deletion.
When you rename, delete, or duplicate a query, the change is reflected in the list in the scripts's interface. But while the script is active it doesn't notice what other programs do in the query folder. So if you rename, copy, or delete queries from outside the script -- probably in the Find/Change dialog -- the script doesn't notice these changes. You need to update the list; press Update list to do so. (Unfortunately, updating the list also resets the column widths.)
View a long query or edit it it. See above.
Press the icon to disable or enable the tooltips defined for most buttons and icons.
9 May 2011: It's now possible to select a character style when you assign queries to GREP styles.
5 May 2011: The metastrings (?#) and (?#T) weren't always removed when expressions were copied to the Find/Change dialog or to GREP styles. Fixed.
20 April 2011: When expressions were saved from the built-in editor, variables ($1, $2, etc.) in the Change To part were not written correctly into the query file. Fixed.
2 April 2011: Incorporated the chaining script in the query manager; added an option to include the page numbers on which the found items occur; changed the query viewer (accessed by right-clicking a query) into a "light editor"; see the text for details.
22 March 2011: Improved performance here and there.
21 March 2011: Added some functions: execute selected queries; duplicate and rename queries. Code rewritten to a large extent.
26 October 2010: More integration with the GREP editor. Queries can now be loaded into the editor straight away.
15 July 2010: Improved interface; improved copying to GREP styles.
Download script (To install the script, just unzip the downloaded zip file in your script folder.)
Questions, comments? Get in touch