Find colored text
Posted: 2011-03-23 07:42:53
I've looked through the manual but can't find it. Is there a way to find a words or characters having colored text?
Any help appreciated.
Any help appreciated.
Code: Select all
Require Pro Version 1.3
$doc = Document.active
if $doc == undefined
exit
end
$black = Color.newWithRGBA 0, 0, 0, 1
$white = Color.newWithRGBA 1, 1, 1, 1
$colors = Hash.new
$samples = Array.new
$TC = Cast to String '[text] '
$BG = Cast to String '[back] '
$HL = Cast to String '[highlight] '
$sep = Cast to String '/'
$showSamples = Cast to String 'Show Samples'
$language = Language.systemLanguage
foreach $text in $doc.allTexts
$textRanges = Array.new
if $text.tables.count # then, exclude special texts 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
if $attr.textColor != $black
$key = $TC & $attr.textColor.hexTriplet
$key &= $sep & $attr.textColor.opacity
if $colors{$key} == undefined
$colors{$key} = Array.new
$sample = $key
Push Target Text $sample
Set Text Color $attr.textColor
Pop Target Text
$samples.appendValue $sample
end
$colors{$key}.appendValue $sel
end
if $attr.textBackgroundColor != $white
$key = $BG & $attr.textBackgroundColor.hexTriplet
$key &= $sep & $attr.textBackgroundColor.opacity
if $colors{$key} == undefined
$colors{$key} = Array.new
$sample = $key
Push Target Text $sample
Set Background Color $attr.textBackgroundColor
Pop Target Text
$samples.appendValue $sample
end
$colors{$key}.appendValue $sel
end
if Defined $attr.textHighlightColor
$key = $HL & $attr.textHighlightColor.hexTriplet
$key &= $sep & $attr.textHighlightColor.opacity
if $colors{$key} == undefined
$colors{$key} = Array.new
$sample = $key
Push Target Text $sample
Set Highlight Color $attr.textHighlightColor
Pop Target Text
$samples.appendValue $sample
end
$colors{$key}.appendValue $sel
end
$i = $range.bound
end
end
end
if ! $colors.keys.count
exit 'No color found, exiting...'
end
$keys = $colors.keys
$keys.sort 'li', $language
$keys.appendValue $showSamples
$keys = Prompt Checkboxes '', '', '', $keys
if ! $keys.count
exit # nothing checked
end
if $keys.lastValue == $showSamples
$showSamples = true
$keys.pop
end
if $keys.count
$sels = Array.new
foreach $key in $keys
$sels.appendValuesFromArray $colors{$key}
end
$doc.setSelections $sels
end
if $showSamples == true
$LF = Cast to String "\n"
$samples.sort 'li', $language
$samples = $samples.join $LF
Document.newWithText $samples
end