Show Find Results matches, [options] v3.0
Opens a new Find Results floating window displaying a list of arbitrary selections / matches.
The given array of matches should contain TextSelection objects, but may also include a mix of other valid content objects, as detailed in the Document object’s setSelection command. Here’s an example that shows all cross-references in the active document:
$doc = Document.active
Show Find Results $doc.allCrossReferences
The options argument can either be a title for the window that displays the results, or a hash defining one or more of the following options:
Option Key |
Purpose |
title |
A string to customize the title of the results window. |
sort |
By default the matches are not sorted and will be displayed in the order given to the Show Find Results command. A value of either "location" or "alpha" will sort the matches, either by logical content location in the document, or alphabetically. |
Multi-Document Results
To show matches for multiple documents the matches argument should be a Hash instead of an array. The hash keys should be Document objects. The value for each key should be an array of matches in that single document. Each match array should contain objects as described above (ie: a mix of TextSelection and other objects).
Here’s an example that shows all bookmarks from all open documents:
$docMap = Hash.new
ForEach $doc in Document.openDocuments
$docMap{$doc} = $doc.allBookmarks
End
Show Find Results $docMap, "All Bookmarks"
Grouped Results
Sometimes you might want to show multiple groups of matches, so the results are grouped by some other criteria. You can do this by again passing a Hash for the matches argument, but using text labels for the hash keys. The value for each key should be an array of matches in a single document.
Here’s an example that shows all images in grouped by their data type:
$resultsMap = Hash.new
$doc = Document.active
ForEach $image in $doc.allImages
$type = $image.dataType
If ! $type
$type = 'unknown'
End
$matches = $resultsMap{$type}
If ! $matches
$matches = Array.new
End
$matches.appendValue($image)
$resultsMap{$type} = $matches
End
Show Find Results $resultsMap, "Images by Type"
Customizing Match Previews
Each match in the results listing will by default show a preview that includes the matched text itself, as well as its surrounding context. In some cases you might want to override this preview, if your macro can show more specialized information for each match.
To customize the preview for a match just include a preview string before the match in the matches array. This string will act as a label for the next array entry. You don’t need to provide a custom label for each match in the array, only those which you want to customize.
Here’s an example that shows all cross-references in a document, but calls out those with missing bookmarks:
$doc = Document.active
$matches = Array.new
ForEach $xref in $doc.allCrossReferences
If $xref.bookmark
$matches.appendValue($xref)
Else
$warning = "⚠️ " & $xref.bookmarkName
If $xref.lastValidBookmarkPreview
$warning &= ": " & $xref.lastValidBookmarkPreview
End
$matches.appendValue($warning)
$matches.appendValue($xref)
End
End
Show Find Results $matches, "Cross-References"
You can use this technique whether you are displaying matches for a single or multiple documents. Here’s an example that lists all images in all open documents, but customizes the results to show image file names:
$docMap = Hash.new
ForEach $doc in Document.openDocuments
$matches = Array.new
ForEach $image in $doc.allImages
$name = $image.fileName
If $name
$matches.appendValue($name)
End
$matches.appendValue($image)
End
$docMap{$doc} = $matches
End
Show Find Results $docMap, "All Images"
Previous Chapter Find and Replace Examples |
<< index >> |
Next Chapter Modify and Format Text |