Hi Josh,
working with the object model means that you will need an object to get the attributes. There are three objects which can return attribute information.
First the
Text object will have attribute information for any location inside it. Second the
TextSelection object will have attribute information for the all the text that is part of it, but in addition will have attributes that will apply if you start typing at that location. Third the
Style object will have attribute information that is part of its definition, including attributes information that it may inherit from another style. So to get anywhere we will need one of these objects. Since you want to the information for the current selection let's start with that.
So this command retrieves the current ("active") text selection and returns it as "$sel". You can now ask for the attributes that will apply when you type at that location like this:
And within those attributes we can ask for the paragraph spacing before, and set things accordingly. So a complete macro will look like this:
Code: Select all
$sel = TextSelection.active
$attrs = $sel.typingAttributes
if $attrs.paragraphSpacingBefore == 14
Set Paragraph Spacing Before 0
else
Set Paragraph Spacing Before 14
end
Now there is one problem with the above macro. Let's assume that all your text has the style "MyStyle" applied and this style includes as part of its definition "Paragraph Spacing Before 14". Running the macro will have zero effect. Why is that? Well attributes come in two parts: first there are the attributes that have been directly applied to your text, but then there are the attributes that are the result of inheritance from other places. When you ask for the
typingAttributes you get only the attributes that are actively applied. Since in our scenario the 14pt spacing is part of the style, it is not applied. The macro will decide that the space before attribute is not set to 14 and try to apply it, and NIsus will resolve this by deciding that the spacing is already applied by the style and is redundant, and so it will 'eat it'. The paragraph will continue to have no space before attribute, and will therefore continue to display the 14pt spacing before set by the style.
To solve this Nisus has a second attribute returning command, with an added "display". Using this type of command ensures that you always get a value:
Code: Select all
$attrs = $sel.displayTypingAttributes
If you change the command in the above macro you will get a macro that consistently toggles the spacing between 0 and 14pts.
But this isn't really what you are looking for. What you want is spacing before relative to the current style. So the first thing we need is the current style:
Code: Select all
$sel = TextSelection.active
$attrs = $sel.displayTypingAttributes
$style = $attrs.paragraphStyle
Now you can ask for the space before attached to the style:
Code: Select all
$spaceBefore = $style.displayAttributes.paragraphSpacingBefore
Note that I am again using "display" attributes to ensure I get a value. Also I am stringing commands together (get the "paragraph spacing before" from the "display attributes"), just to save time/lines of codes/variables. You can do it in two steps, if you prefer, just watch the variable names!
We can now ask if the current space before is equal to that of the style, and if yes increase it by 14pts, otherwise set it back. So the whole macro might look like this:
Code: Select all
$sel = TextSelection.active
$attrs = $sel.displayTypingAttributes
$style = $attrs.paragraphStyle
$spaceBefore = $style.displayAttributes.paragraphSpacingBefore
if $attrs.paragraphSpacingBefore == $spaceBefore
$spaceBefore += 14
end
Set Paragraph Spacing Before $spaceBefore
Hope all this is clear.