Adobe 65009333 Scripting Guide - Page 48

Using grep, to turn case sensitivity on

Page 48 highlights

Text and Type Finding and changing text 48 You also can search for a string of text and apply formatting, as shown in the following script fragment (from the FindChangeStringFormatting tutorial script): //Clear the find/change preferences. app.findTextPreferences = NothingEnum.nothing; app.changeTextPreferences = NothingEnum.nothing; //Set the find options. app.findChangeTextOptions.caseSensitive = false; app.findChangeTextOptions.includeFootnotes = false; app.findChangeTextOptions.includeHiddenLayers = false; app.findChangeTextOptions.includeLockedLayersForFind = false; app.findChangeTextOptions.includeLockedStoriesForFind = false; app.findChangeTextOptions.includeMasterPages = false; app.findChangeTextOptions.wholeWord = false; app.findTextPreferences.findWhat = "WIDGET^9^9^9^9"; //The following line will only work if your default font //has a font style named "Bold" if not, change the text to //a font style used by your default font. app.changeTextPreferences.fontStyle = "Bold"; //Search the document. In this example, we'll use the //InCopy search metacharacter "^9" to find any digit. var myFoundItems = app.documents.item(0).changeText(); alert("Changed " + myFoundItems.length + " instances of the search string."); //Clear the find/change preferences after the search. app.findTextPreferences = NothingEnum.nothing; app.changeTextPreferences = NothingEnum.nothing; Using grep InCopy supports regular expression find/change through the findGrep and changeGrep methods. Regular-expression find and change also can find text with a specified format or replace text formatting with formatting specified in the properties of the changeGrepPreferences object. The following script fragment shows how to use these methods and the related preferences objects (for the complete script, see FindGrep): //Clear the find/change preferences. app.findGrepPreferences = NothingEnum.nothing; app.changeGrepPreferences = NothingEnum.nothing; //Set the find options. app.findChangeGrepOptions.includeFootnotes = false; app.findChangeGrepOptions.includeHiddenLayers = false; app.findChangeGrepOptions.includeLockedLayersForFind = false; app.findChangeGrepOptions.includeLockedStoriesForFind = false; app.findChangeGrepOptions.includeMasterPages = false; //Regular expression for finding an email address. app.findGrepPreferences.findWhat = "(?i)[A-Z]*?@[A-Z Apply the change to 24-point text only. app.findGrepPreferences.pointSize = 24; app.changeGrepPreferences.underline = true; app.documents.item(0).changeGrep(); //Clear the find/change preferences after the search. app.findGrepPreferences = NothingEnum.nothing; app.changeGrepPreferences = NothingEnum.nothing; NOTE: The findChangeGrepOptions object lacks two properties of the findChangeTextOptions object: wholeWord and caseSensitive. This is because you can set these options using the regular expression string itself. Use (?i) to turn case sensitivity on and (?-i) to turn case sensitivity off. Use \> to match the beginning of a word and \< to match the end of a word, or use \b to match a word boundary.

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88

Text and Type
Finding and changing text
48
You also can search for a string of text and apply formatting, as shown in the following script fragment
(from the FindChangeStringFormatting tutorial script):
//Clear the find/change preferences.
app.findTextPreferences = NothingEnum.nothing;
app.changeTextPreferences = NothingEnum.nothing;
//Set the find options.
app.findChangeTextOptions.caseSensitive = false;
app.findChangeTextOptions.includeFootnotes = false;
app.findChangeTextOptions.includeHiddenLayers = false;
app.findChangeTextOptions.includeLockedLayersForFind = false;
app.findChangeTextOptions.includeLockedStoriesForFind = false;
app.findChangeTextOptions.includeMasterPages = false;
app.findChangeTextOptions.wholeWord = false;
app.findTextPreferences.findWhat = "WIDGET^9^9^9^9";
//The following line will only work if your default font
//has a font style named "Bold" if not, change the text to
//a font style used by your default font.
app.changeTextPreferences.fontStyle = "Bold";
//Search the document. In this example, we'll use the
//InCopy search metacharacter "^9" to find any digit.
var myFoundItems = app.documents.item(0).changeText();
alert("Changed " + myFoundItems.length + " instances of the search string.");
//Clear the find/change preferences after the search.
app.findTextPreferences = NothingEnum.nothing;
app.changeTextPreferences = NothingEnum.nothing;
Using grep
InCopy supports regular expression find/change through the
findGrep
and
changeGrep
methods.
Regular-expression find and change also can find text with a specified format or replace text formatting
with formatting specified in the properties of the
changeGrepPreferences
object. The following script
fragment shows how to use these methods and the related preferences objects (for the complete script,
see FindGrep):
//Clear the find/change preferences.
app.findGrepPreferences = NothingEnum.nothing;
app.changeGrepPreferences = NothingEnum.nothing;
//Set the find options.
app.findChangeGrepOptions.includeFootnotes = false;
app.findChangeGrepOptions.includeHiddenLayers = false;
app.findChangeGrepOptions.includeLockedLayersForFind = false;
app.findChangeGrepOptions.includeLockedStoriesForFind = false;
app.findChangeGrepOptions.includeMasterPages = false;
//Regular expression for finding an email address.
app.findGrepPreferences.findWhat = "(?i)[A-Z]*?@[A-Z]*?[.]...";
//Apply the change to 24-point text only.
app.findGrepPreferences.pointSize = 24;
app.changeGrepPreferences.underline = true;
app.documents.item(0).changeGrep();
//Clear the find/change preferences after the search.
app.findGrepPreferences = NothingEnum.nothing;
app.changeGrepPreferences = NothingEnum.nothing;
N
OTE
:
The
findChangeGrepOptions
object lacks two properties of the
findChangeTextOptions
object:
wholeWord
and
caseSensitive
. This is because you can set these options using the regular expression
string itself. Use
(?i)
to turn case sensitivity on and
(?-i)
to turn case sensitivity off. Use
\>
to match the
beginning of a word and
\<
to match the end of a word, or use
\b
to match a word boundary.