Page 1 of 1

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

Posted: 2008-04-16 07:39:59
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?

Posted: 2008-04-16 17:15:41
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