I modified the macro again. Thanks to
getProperty command, I could simplify the code in the first “
foreach . . . end” loop. This makes it easier to maintain the macro, e.g. fix a potential/real bug. As a side effect, not only for Paragraph Style but also for Character and List Styles, selections not having any style are listed up. Useless, I think, but harmless. And
perhaps there may be situations in which “
[a̱] <no style>” or “
[#] <no style>” are useful.
In spite of that, this version is longer than the previous ones because...
• It checks for empty document (i.e. not a singe character in it).
• The Macro Prompt is more informative (addition of
$detail).
• More easily customizable/localizable because of variables.
The last reason mentioned above applies especially to an
array “
$styleCategories” and
hashes declared in “
$styleSymbols = $properties = $selectionsByStyle = Hash.new”. The macro works without problem even if the definition of
$styleCategories has been changed to “
$styleCategories = Array.new '○', '▴', '□'”, for example ;-)
IMO, one of the most fantastic features of Nisus Writer Pro Macro language is
hash. It is very strong and versatile. You can put in it anything: normal
Text object (
$styleSymbols and
$properties although the latter will be treated as
Attributes Object properties by
getProperty),
array (
$selectionsByStyle) and/or anything else.
Code: Select all
$noStyleName = Cast to String '<no style>' # customizable
$styleString = 'Style' # customizable (used in Macro Prompt's $detail)
$promptMessage = 'Select all texts in styles of your choice...' # customizable
$emptyDocument = 'Empty document, exiting...' # customizable (error message)
$nothingChecked = 'Nothing checked, exiting...' # customizable (error message)
$sp = Cast to String "\x20" # customizable but...
$styleCategories = Array.new 'Character', 'Paragraph', 'List' # customizable
$styleSymbols = $properties = $selectionsByStyle = Hash.new
$styleSymbols{$styleCategories[0]} = Cast to String '[a̱]' # customizable (for Character Style)
$styleSymbols{$styleCategories[1]} = Cast to String '[¶]' # customizable (for Paragraph Style)
$styleSymbols{$styleCategories[2]} = Cast to String '[#]' # customizable (for List Style)
$properties{$styleCategories[0]} = 'characterStyle'
$properties{$styleCategories[1]} = 'paragraphStyle'
$properties{$styleCategories[2]} = 'listStyle'
Require Pro Version 1.3
$doc = Document.active
if $doc == undefined
exit # no open document
end
$language = Language.systemLanguage
foreach $text in $doc.allTexts
if $text.length
$textRanges = Array.new
if $text.tables.count # then, exclude special chars enclosing tables
$tableRanges = Array.new
foreach $table in $text.tables
$tableRanges.appendValue $table.enclosingTextRange
end
$tableRanges.sort
$i = 0
foreach $tableRange in $tableRanges
$range = Range.newWithLocationAndBound $i, $tableRange.location
$textRanges.appendValue $range
$i = $tableRange.bound
end
$range = Range.newWithLocationAndBound $i, $text.length
else
$range = Range.new 0, $text.length
end
$textRanges.appendValue $range
foreach $textRange in $textRanges
$i = $textRange.location
while $i < $textRange.bound
$attr = $text.displayAttributesAtIndex $i
$range = $text.rangeOfDisplayAttributesAtIndex $i
$sel = TextSelection.new $text, $range
foreach $property in $properties.keys
$styleObject = $attr.getProperty $properties{$property}
$name = $styleSymbols{$property}
if Defined $styleObject
$name &= $sp & $styleObject.name
else
$name &= $sp & $noStyleName
end
if $selectionsByStyle{$name} == undefined
$selectionsByStyle{$name} = Array.new
end
$selectionsByStyle{$name}.appendValue $sel
end
$i = $range.bound
end
end
end
end
if ! $selectionsByStyle.count
exit $emptyDocument
end
$styleNames = $selectionsByStyle.keys
$styleNames.sort 'li', $language
$detail = Array.new
foreach $key, $value in $styleSymbols
$temp = ''
$temp = $temp.textByAppending $value, $sp, $key, $sp, $styleString
$detail.appendValue $temp
end
$detail.sort 'li', $language
$detail = $detail.join ', '
$styleNames = Prompt Checkboxes $promptMessage, $detail, 'Select!', $styleNames
if ! $styleNames.count
exit $nothingChecked
end
$sels = Array.new
foreach $name in $styleNames
$sels.appendValuesFromArray $selectionsByStyle{$name}
end
$doc.setSelections $sels