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

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

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

Post by js » 2008-04-16 07:39:59

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
Official Nisus Person
Posts: 4453
Joined: 2002-07-11 17:14:10
Location: San Diego, CA

Post by martin » 2008-04-16 17:15:41

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 = 
$texts = $doc.selectedSubtexts 

ForEach $text in $texts
   Type Attributed Text $text 
   Type Text “\n” 
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 = 

$doc = 
$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 

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

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

Post Reply