I think the main problem is that it is a bit difficult to get a continuous range of a specific attribute when there are text portions in mixed attributes. For example, the following macro will transform "
abcdefghi" into "\textit{abc}\textit{def}\textit{ghi}" which is not desirable for a macro converting style attributes into tags used in LaTeX, html, etc.
Code: Select all
$prefix = Cast to String '\textit{'
$suffix = Cast to String '}'
Require Application Version 3.1
$doc = Document.active
$selections = Array.new
$textObjects = $doc.allTexts
foreach $text in $textObjects
if $text.length
$index = 0
$limit = $text.length
while $index < $limit
$attr = $text.displayAttributesAtIndex $index
$range = $text.rangeOfDisplayAttributesAtIndex $index
$isItalic = $attr.italic
if $isItalic == true
$sel = TextSelection.new $text, $range
$selections.appendValue $sel
end
$index = $range.bound
end
end
end
foreach $sel in reversed $selections
$sel.text.insertAtIndex $sel.bound, $suffix
$sel.text.insertAtIndex $sel.location, $prefix
end
To get "\textit{abcdefghi}", you have to add something which merges text selection objects (I don't know if "merge" is the right word).
Code: Select all
$i = $selections.count - 1
while $i > 0
$sel = $selections[$i]
$selBefore = $selections[$i-1]
if $sel.text == $selBefore.text
if $sel.location == $selBefore.bound
$selBefore.length += $sel.length
$selections.removeValueAtIndex $i
end
end
$i -= 1
end
It works but it would be
very nice if a future version of Nisus Writer Pro will have a macro command such as $selections.merge or something alike.
Thanks! ;-)
Edit: I corrected a bug in the macro by replacing "rangeOfAttributesAtIndex" with "rangeOfDisplayAttributesAtIndex".