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:
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.