Cliff S. wrote:I would like to be able to search for all underlined text in a document and change it to italicized text.
Although NWP’s attribute sensitive Find/Replace is great and works fine in most of cases, it is not always easy to adjust the attributes of the find expression. So I wrote a macro which, I hope, makes this kind of job easier.
The comment lines in the macro explain how it works. In this particular case, put the insertion point somewhere in an underlined word and run the macro. You will be asked to specify attributes to be found. So check
Underline: Single (or
Double?) and hit
Where button.

- SelectByCriteria.png (18.61 KiB) Viewed 11402 times
Then, you will be asked to specify text areas where you want to find underlined text. Check as many checkboxes as you like and hit
Select All… button.

- SelectByCriteria2.png (11.07 KiB) Viewed 11402 times
All underlined texts in the text areas of your choice will be found and selected so that you can remove Underline and apply Italic easily.
I feel the name of the macro and some messages shown by it are not very good but, alas, my poor English does not allow me to improve them.
EDIT and
P.S. How to install a new macro is explained here:
http://www.nisus.com/forum/viewtopic.php?p=8737#p8737Code: Select all
# 1. The macro will show you a list of character attributes
# at the insertion point (or of the first character in the selection).
# 2. Check all attributes you need, e.g. 'Font Size: 12' and 'Italic: On',
# and hit 'Where'
# 3. Then, you will be asked to specify the find scope.
# Check all text areas to be included, e.g. 'body' and 'footnote',
# and hit 'Select All…'.
# 4. All text portions satisfying the criteria, i.e. 'Font Size: 12'
# and 'Italic: On' in 'body' and 'footnote', will be selected.
# - You can remove attributes you do not want to be listed up
# by editing $properties near the beginning of the macro.
# - Subscript style will be indicated inappropriately as
# 'Superscript: Subscript' for entries in the attribute list are
# generated programmatically, not to make the code yet more complicated.
# - For a List Style, you will see something redundant such as
# 'List Style: List Style "Number List"' for a command corresponding with
# 'paragraphStyleName' is not available for List Style.
# - The macro will show RGB values of colors except for very basic ones:
# white, black, red, green and blue.
$doc = Document.active
if $doc == undefined
exit
end
$properties = Cast to String 'fontSize, fontFamilyName, bold, italic, underline, strikethrough, superscript, characterCase, language, textColor, textBackgroundColor, textHighlightColor, characterStyleName, paragraphStyleName, listStyle, firstLineHeadIndent, headIndent, tailIndent, paragraphAlignment, paragraphWritingDirection, paragraphSpacingBefore, paragraphSpacingAfter, fixedLineHeight, lineHeightMultiple'
$properties = $properties.split ', '
$humanize = Hash.new
$humanize{'bold'} = $humanize{'italic'} = Array.new 'Off', 'On'
$humanize{'underline'} = $humanize{'strikethrough'} = Array.new 'None', 'Single', 'Double'
$humanize{'superscript'} = Array.new 'Subscript', 'None', 'Superscript'
$humanize{'characterCase'} = Array.new 'None', 'lowercased', 'Capitalized', 'Small Caps', 'UPPERCASED'
$humanize{'paragraphAlignment'} = Array.new 'Left', 'Right', 'Center', 'Justified'
$humanize{'paragraphWritingDirection'} = Array.new 'Natural', 'Left-to-Right', 'Right-to-Left'
$white = Color.white
$black = Color.black
$red = Color.red
$green = Color.green
$blue = Color.blue
$humanize{'colors'} = Hash.new $white, 'White', $black, 'Black', $red, 'Red', $green, 'Green', $blue, 'Blue'
$sel = $doc.textSelection
if $sel.location == $sel.text.length
$attr = $sel.displayTypingAttributes
else
$attr = $sel.text.displayAttributesAtIndex $sel.location
end
$valueOfProperty = Hash.new
$propertyList = Array.new
$sep = ': '
foreach $property in $properties
$value = $attr.getProperty($property)
$valueOfProperty{$property} = $value
$propertyName = $property
$propertyName.findAndReplace '^.', '\T{uppercase:\0}', 'E-i'
$propertyName.replaceAll '(?<=\p{Lower})(?=\p{Upper})', '\x20', 'E-i'
if $property == 'superscript'
$value += 1
elsif $property == 'textColor'
if Defined $humanize{'colors'}{$value}
$value = $humanize{'colors'}{$value}
end
elsif $property == 'textBackgroundColor'
if Defined $humanize{'colors'}{$value}
$value = $humanize{'colors'}{$value}
end
end
if Defined $humanize{$property}
$value = $humanize{$property}[$value]
elsif $value == undefined
$value = 'None'
end
$propertyName = $propertyName.textByAppending $sep, $value
$propertyList.appendValue $propertyName
end
$message = 'Adjust Search Criteria'
$detail = 'Check attributes to be included'
$button = 'Where'
$properties = Prompt Checkboxes $message, $detail, $button, $propertyList
if ! $properties.count
exit 'Nothing checked, exiting…'
end
$objectTypes = Hash.new
foreach $text in $doc.allTexts
$objectTypes{$text.documentContentType} = 1
end
$objectTypes = $objectTypes.keys
$objectTypes.sort
$message = 'In checked text areas:'
$button = 'Select All…'
$objectTypes = Prompt Checkboxes $message, '', $button, $objectTypes
if ! $objectTypes.count
exit 'Nothing checked, exiting…'
end
foreach $i, $property in $properties
$property = $property.split $sep
$property = $property.firstValue
$property.findAndReplace '^.', '\T{lowercase:\0}', 'E-i'
$property.replaceAll '\x20', '', 'E-i'
$properties.setValueAtIndex $property, $i
end
$sels = Array.new
foreach $text in $doc.allTexts
if $text.length
if $objectTypes.indexOfValue($text.documentContentType) != -1
$i = 0
while $i < $text.length
$attr = $text.displayAttributesAtIndex $i
$range = $text.rangeOfDisplayAttributesAtIndex $i
$passed = true
foreach $property in $properties
if $attr.getProperty($property) != $valueOfProperty{$property}
$passed = false
break
end
end
if $passed == true
$sel = TextSelection.new $text, $range
$sels.appendValue $sel
end
$i = $range.bound
end
end
end
end
if $sels.count
$doc.setSelections $sels
else
exit 'Nothing found, exiting…'
end