Reply to topic  [ 2 posts ] 
Could we get a simple example or two for the use of objects? 
Author Message

Joined: 2007-04-12 14:59:36
Posts: 229
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?


2008-04-16 07:39:59
Profile
Official Nisus Person
User avatar

Joined: 2002-07-11 17:14:10
Posts: 4251
Location: San Diego, CA
Post 
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:
$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:
# 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


2008-04-16 17:15:41
Profile WWW
Display posts from previous:  Sort by  
Reply to topic   [ 2 posts ] 

Who is online

Users browsing this forum: No registered users and 2 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB® Forum Software © phpBB Group
Designed by ST Software