Reply to topic  [ 7 posts ] 
Trouble getting the .removeStyle command to work 
Author Message

Joined: 2015-01-18 10:38:33
Posts: 5
Hello friends,

I am new to the world of Nisus and working on my first macro so this may be a very stupid question. I need to write a macro which deletes some paragraph styles from the document it operates on.

I cannot figure out why this code does not work:

Code:
$doc = Document.active
$doc.removeStyle ‘Header’


I get either: "Expected a Style object, not a Text object"

or: "Invalid variable assignment" (If I don't use the single quotes).

a similar command,

Code:
$doc.addNewStyle ‘Paragraph’, ‘Title’


works as I would expect... it creates the paragraph style "Title"

I've combed the manual reference but can't figure out what I'm missing? What am I doing wrong?


2015-01-18 10:52:20
Profile
User avatar

Joined: 2007-02-07 00:58:12
Posts: 876
Location: Japan
kaderan wrote:
Hello friends,
I am new to the world of Nisus and working on my first macro so this may be a very stupid question. I need to write a macro which deletes some paragraph styles from the document it operates on.

Welcome!

kaderan wrote:
I cannot figure out why this code does not work:

Code:
$doc = Document.active
$doc.removeStyle ‘Header’


I get either: "Expected a Style object, not a Text object" or: "Invalid variable assignment" (If I don't use the single quotes).

As, the error message says, that command requires a Style object. The word 'Header' (in quotes) is not a Style object. It is 'merely' a text string.
To get a Style object you can use the following command:
Code:
$headerStyle = $doc.styleWithName 'Header'

This will return the Style object in the variable $headerStyle. (You can choose any variable name for this as long as it starts with a '$' symbol.)
Once you have the Style object you can remove it like this:
Code:
$doc.removeStyle $headerStyle


kaderan wrote:
a similar command,

Code:
$doc.addNewStyle ‘Paragraph’, ‘Title’


works as I would expect... it creates the paragraph style "Title"

Well, that is because the style creates a Style object, and the argument that it requires just has to be a string (for the name of the new object).
Hope this clears things up!
Happy coding.

PS: Note that in the Macro Language Reference, the command always specifies the type of argument that it takes. This may not have seemed obvious to you at first. It's easy to overlook. For example with .styleWithName it says name, while with .removeStyle it says style. This distinction—name vs. style—is significant. The following explanation is always very dry, but for .removeStyle it says:
Quote:
Removes the given Style object from the document. [emphasis mine]

etc. So once you get the idea, the Reference is very helpful for this kind of problem.

_________________
philip


2015-01-20 17:13:46
Profile

Joined: 2015-01-18 10:38:33
Posts: 5
Thank you! Works like a charm. If only the macro reference file was as clear as your explanation!


2015-01-22 09:28:28
Profile

Joined: 2015-01-18 10:38:33
Posts: 5
Whoops, I ran into a snag:

The example code below

Code:
$headerStyle = $doc.styleWithName 'Heading 1'
$doc.removeStyle $headerStyle


works great as long 'Heading 1' exists in the document. If 'Heading 1' does not exist, macro fails with "Expected a Style object."

My understanding from the reference manual was that the command would have no effect if the style was not present.

Quote:
.removeStyle style v2.0.5
Removes the given [Referenced content is missing.] object from the document. If the style was not part of this document, this command has no effect.


Am I missing something? Can I make my macro ignore this error in cases when a removeStyle command is attempted in a document where the given style does not exist?


2015-01-22 10:24:02
Profile
User avatar

Joined: 2007-02-07 00:58:12
Posts: 876
Location: Japan
You are right that .removeStyle will work (fail) silently if you ask it to remove a style that is not present in the document. The problem is that $headerStyle in the code above will not be a style. Since the style is not present in the document, .styleWithName will return 'undefined'. So in the next step you are asking to remove the style 'undefined' from the document. And since 'undefined' is not a style object, Nisus gives you the 'Expected Style object' message.

So one way to fix this is like this:
Code:
$headerStyle = $doc.styleWithName 'Heading 1'
if $headerStyle
     $doc.removeStyle $headerStyle
end

_________________
philip


2015-01-22 18:26:34
Profile
User avatar

Joined: 2007-02-07 00:58:12
Posts: 876
Location: Japan
Here for some more related 'tricks'.

Since you are simply wanting to get rid of the style, you don't really need to put it in a variable. So the following works as well:
Code:
$doc.removeStyle $doc.styleWithName('Heading 1')

Note that in this case you will need the (usually optional) parentheses around the argument.

Of course that code will have the same problem you discovered, in that it will throw an error if there is no style 'Heading 1' in the document.
Another alternative is to write like this:
Code:
if $h1Style = $doc.styleWithName 'Heading 1'
     $doc.removeStyle $h1Style
end


Finally there are other ways to get style objects. For instance you can get the styles in the document using commands like $doc.allStyles. Also if you want to replace s style you don't really need to remove it first; you can just replace it. So it might be worth thinking about what you are trying to achieve.

So as an illustration, here is a macro that will remove all unused styles in the document.
Code:
$doc = Document.active
foreach $style in $doc.allStyles
   $found = Find $style
   if ! $found
      $doc.removeStyle $style
   end
end

_________________
philip


2015-01-22 18:44:48
Profile

Joined: 2015-01-18 10:38:33
Posts: 5
Thanks so much, phspaelti. That's exactly what I needed.


2015-01-24 04:22:36
Profile
Display posts from previous:  Sort by  
Reply to topic   [ 7 posts ] 

Who is online

Users browsing this forum: No registered users and 1 guest


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:  
cron
Powered by phpBB® Forum Software © phpBB Group
Designed by ST Software