Trouble getting the .removeStyle command to work

Get help using and writing Nisus Writer Pro macros.
Post Reply
kaderan
Posts: 5
Joined: 2015-01-18 10:38:33

Trouble getting the .removeStyle command to work

Post by kaderan »

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: Select all

$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: Select all

$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?
User avatar
phspaelti
Posts: 1313
Joined: 2007-02-07 00:58:12
Location: Japan

Re: Trouble getting the .removeStyle command to work

Post by phspaelti »

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: Select all

$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: Select all

$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: Select all

$doc.removeStyle $headerStyle
kaderan wrote: a similar command,

Code: Select all

$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:
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
kaderan
Posts: 5
Joined: 2015-01-18 10:38:33

Re: Trouble getting the .removeStyle command to work

Post by kaderan »

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

Re: Trouble getting the .removeStyle command to work

Post by kaderan »

Whoops, I ran into a snag:

The example code below

Code: Select all

$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.
.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?
User avatar
phspaelti
Posts: 1313
Joined: 2007-02-07 00:58:12
Location: Japan

Re: Trouble getting the .removeStyle command to work

Post by phspaelti »

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: Select all

$headerStyle = $doc.styleWithName 'Heading 1'
if $headerStyle
     $doc.removeStyle $headerStyle
end
philip
User avatar
phspaelti
Posts: 1313
Joined: 2007-02-07 00:58:12
Location: Japan

Re: Trouble getting the .removeStyle command to work

Post by phspaelti »

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: Select all

$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: Select all

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: Select all

$doc = Document.active
foreach $style in $doc.allStyles
	$found = Find $style
	if ! $found
		$doc.removeStyle $style
	end
end
philip
kaderan
Posts: 5
Joined: 2015-01-18 10:38:33

Re: Trouble getting the .removeStyle command to work

Post by kaderan »

Thanks so much, phspaelti. That's exactly what I needed.
Post Reply