Could we get a simple example or two for the use of objects?

Get help using and writing Nisus Writer Pro macros.
Post Reply
js
Posts: 259
Joined: 2007-04-12 14:59:36

Could we get a simple example or two for the use of objects?

Post by js »

The new additions to the macro language seem to be very promising, particularly the use of Objects. But for some of us hobby programmers it would certainly be easier to understand how that works with some examples. May I suggest to furnish us a simple macro like how to get multiple selections of a text into a new document, each on a line?
User avatar
martin
Official Nisus Person
Posts: 5228
Joined: 2002-07-11 17:14:10
Location: San Diego, CA
Contact:

Post by martin »

Sure, the macro reference is still a bit light on examples. Here's a macro that takes all selected bits of text and places them in a new document:

Code: Select all

$doc = Document.active 
$texts = $doc.selectedSubtexts 

New 
ForEach $text in $texts
   Type Attributed Text $text 
   Type Text “\n” 
End
Another example that collects an array of all font families in use in a document's main body:

Code: Select all

# use a hash to efficiently coalesce duplicates as we go 
$usedNameMap = Hash.new 

$doc = Document.active 
$text = $doc.text 
$charIndex = 0 
$limit = $text.length 

# inspect the attributes applied to every character in the text 
While $charIndex < $limit 
   $attributes = $text.displayAttributesAtIndex($charIndex) 
   $fontName = $attributes.fontFamilyName 

   # if the font was defined, mark it as used 
   If Defined $fontName 
      $usedNameMap{$fontName} = 1 
   End 

   # move to the next area that has different attributes 
   $range = $text.rangeOfDisplayAttributesAtIndex($charIndex) 
   $charIndex = $range.bound 
End 

# turn the hash into a proper array for display 
$fontNames = $usedNameMap.keys 
Prompt $fontNames
Post Reply