Why is this macro so slow?
Posted: 2009-07-11 18:52:28
Hello,
I have two version of a macro which lists up all the style attributes of a given text. One of them works rather quickly [version a], while the other [version b], which seems probably simpler, is VERY slow. Could you please indicate me why the latter is so slow, and how I can get it work more quickly?
Here is the version a:
and the version b:
Thank you in advance.
I have two version of a macro which lists up all the style attributes of a given text. One of them works rather quickly [version a], while the other [version b], which seems probably simpler, is VERY slow. Could you please indicate me why the latter is so slow, and how I can get it work more quickly?
Here is the version a:
Code: Select all
# Begin macro 'nwp_1-2_to_utf8_test1'
Require Application Version "3.2"
$doc = Document.active
$text = $doc.text
$text = $text.copy
$footnotes = $doc.footnotes
$endnotes = $doc.endnotes
$section_notes = $doc.sectionnotes
$footnotes_ct = $footnotes.count
$endnotes_ct = $endnotes.count
$sectionnotes_ct = $section_notes.count
if $footnotes_ct
while $footnotes_ct
$this_footnote = $footnotes[$footnotes_ct - 1]
$fn_text = $this_footnote.contentSubtext
$fn_text.findAndReplace '^\s+', '', 'E'
$fn_text = "<fn>" & $fn_text
$fn_text &= "</fn>"
$fn_ref = $this_footnote.documentTextRange
$text.replaceInRange $fn_ref, $fn_text
$footnotes_ct -= 1
End
End
if $endnotes_ct
while $endnotes_ct
$this_endnote = $endnotes[$endnotes_ct - 1]
$en_text = $this_endnote.contentSubtext
$en_text.findAndReplace '^\s+', '', 'E'
$en_text = "<en>" & $en_text
$en_text &= "</en>"
$en_ref = $this_endnote.documentTextRange
$text.replaceInRange $en_ref, $en_text
$endnotes_ct -= 1
End
End
if $sectionnotes_ct
while $sectionnotes_ct
$this_sectionnote = $section_notes[$sectionnotes_ct - 1]
$sn_text = $this_sectionnote.contentSubtext
$sn_text.findAndReplace '^\s+', '', 'E'
$sn_text = "<sn>" & $sn_text
$sn_text &= "</sn>"
$sn_ref = $this_sectionnote.documentTextRange
$text.replaceInRange $sn_ref, $sn_text
$sectionnotes_ct -= 1
End
End
$charIndex = 0
$limit = $text.length
$ranges = Array.new
$attribute_text = ""
# inspect the attributes applied to every character in the text
While $charIndex < $limit
$range = $text.rangeOfDisplayAttributesAtIndex($charIndex)
$ranges.push ($range)
$attributes = $text.displayAttributesAtIndex($charIndex)
$attribute_text &= $attributes
# move to the next area that has different attributes
$charIndex = $range.bound
End
$tmp_file = '/Users/me/Desktop/test_things/NWP_rtf_to_utf8/test_aby.txt'
$separator = Text.newWithCodepoint (2)
File.writeDataToPath “$text $separator $ranges $separator $attribute_text”, $tmp_file
exit
Code: Select all
# Begin macro 'nwp_1-2_to_utf8_test2'
Require Application Version "3.2"
$doc = Document.active
$text = $doc.text
$text = $text.copy
$footnotes = $doc.footnotes
$endnotes = $doc.endnotes
$section_notes = $doc.sectionnotes
$footnotes_ct = $footnotes.count
$endnotes_ct = $endnotes.count
$sectionnotes_ct = $section_notes.count
if $footnotes_ct
while $footnotes_ct
$this_footnote = $footnotes[$footnotes_ct - 1]
$fn_text = $this_footnote.contentSubtext
$fn_text.findAndReplace '^\s+', '', 'E'
$fn_text = "<fn>" & $fn_text
$fn_text &= "</fn>"
$fn_ref = $this_footnote.documentTextRange
$text.replaceInRange $fn_ref, $fn_text
$footnotes_ct -= 1
End
End
if $endnotes_ct
while $endnotes_ct
$this_endnote = $endnotes[$endnotes_ct - 1]
$en_text = $this_endnote.contentSubtext
$en_text.findAndReplace '^\s+', '', 'E'
$en_text = "<en>" & $en_text
$en_text &= "</en>"
$en_ref = $this_endnote.documentTextRange
$text.replaceInRange $en_ref, $en_text
$endnotes_ct -= 1
End
End
if $sectionnotes_ct
while $sectionnotes_ct
$this_sectionnote = $section_notes[$sectionnotes_ct - 1]
$sn_text = $this_sectionnote.contentSubtext
$sn_text.findAndReplace '^\s+', '', 'E'
$sn_text = "<sn>" & $sn_text
$sn_text &= "</sn>"
$sn_ref = $this_sectionnote.documentTextRange
$text.replaceInRange $sn_ref, $sn_text
$sectionnotes_ct -= 1
End
End
$charIndex = 0
$limit = $text.length
$ranges = Array.new
$attribute_text = ""
$black_text = 'textColor=<Red=0.000, Green=0.000, Blue=0.000, Opacity=1.000>, textBackgroundColor=<Red=1.000, Green=1.000, Blue=1.000, Opacity=1.000>'
$the_delimiter_a = Text.newWithCharacter(2)
$the_delimiter_b = Text.newWithCharacter(3)
# inspect the attributes applied to every character in the text
While $charIndex < $limit
$attributes = ''
$range = $text.rangeOfDisplayAttributesAtIndex($charIndex)
$attributes &= $text.displayAttributesAtIndex($charIndex)
$attributes.findAndReplace $black_text, ''
$attribute_text &= $attributes
$this_text = $text.subtextInRange ($range)
$this_text = $the_delimiter_a & $this_text
$this_text &= $the_delimiter_b
$attribute_text &= $this_text
# move to the next area that has different attributes
$charIndex = $range.bound
End
$tmp_file = '/Users/me/Desktop/test_things/NWP_rtf_to_utf8/test_abz.txt'
#$separator = Text.newWithCodepoint (2)
File.writeDataToPath $attribute_text, $tmp_file
exit