Import Style via Macro

Get help using and writing Nisus Writer Pro macros.
Post Reply
User avatar
loulesko
Posts: 89
Joined: 2008-01-20 11:08:35
Location: California
Contact:

Import Style via Macro

Post by loulesko » 2017-03-05 10:26:00

Hi there,

Is there a way to import a style sheet from my Style Library and have it replace the existing styles? I have tried the following but it didn't work.

Code: Select all

$doc = Document.active
$doc.addStyles 'Manuscript Draft', 'replace'
Thank you.

Lou

User avatar
phspaelti
Posts: 912
Joined: 2007-02-07 00:58:12
Location: Japan

Re: Import Style via Macro

Post by phspaelti » 2017-03-05 17:36:05

Hello Lou,
in your code, what is 'Manuscript Draft'? Well, actually, I know what Nisus thinks it is. Nisus thinks it's a text object containing the text string 'Manuscript Draft' and having whatever attributes are present in your macro file. So for example, if your macro code has a paragraph style 'macro code' applied to that line, your code will import the paragraph style 'macro code' into $doc (and replace any pre-existing style with the same name). That is not what you are looking for, I believe.

Rather, let's assume you have a document open called 'Manuscript Draft.rtf' and you want to import its styles into $doc, you should do something like this:

Code: Select all

$doc = Document.active
$styleDoc = Document.withDisplayName 'Manuscript Draft.rtf'
$doc.addStyles $styleDoc.text, 'replace'
Obviously the code for retrieving the document with the styles may need some adjustment depending on your case (e.g., getting a document from a file path, or the style library, etc.)

Anyhow the important point is that the '.addStyles' command requires that first argument to be a text object. If you enclose that argument in quotes, then it is a literal, i.e., the text in the command is itself the text object. Hope that is clear.
philip

User avatar
loulesko
Posts: 89
Joined: 2008-01-20 11:08:35
Location: California
Contact:

Re: Import Style via Macro

Post by loulesko » 2017-03-05 22:28:20

Thank you very much Philip. Below is what I ended up with, which works super well. The additional code that you see was taken from one of your responses to another question. So thank you for that as well.

What this does is allow me to write with a stylesheet that works best visually for the screen, and then when I want to print a copy with specific formatting I can "swap" the style, print, and then undo. This just makes NWP even more epic than it already was.

I couldn't have pulled this off without your help Philip. I owe you a cocktail.

Code: Select all

$doc = Document.active

$dm = DocumentManager.instance
$styleLib = $dm.groupWithName 'Style Library'
$styleLibFiles = $styleLib.allFilePaths
$styleLibFileNames = $styleLibFiles.arrayByMakingValuesDoCommand 'lastFilePathComponentWithoutExtension'

$styleFile = Prompt Options "Choose a stylesheet.", '', 'Choose', $styleLibFileNames

$ind = $styleLibFileNames.indexOfValue $styleFile
if $ind == -1
die "There is no file called $styleFile in your style libarary."
end

$stylePath = $styleLibFiles[$ind]
File.requireAccessAtPath $stylePath
$styleSheet = Document.open $stylePath, false
$doc.addStyles $styleSheet.text, 'replace'
$styleSheet.close

User avatar
phspaelti
Posts: 912
Joined: 2007-02-07 00:58:12
Location: Japan

Re: Import Style via Macro

Post by phspaelti » 2017-03-05 22:57:35

I'm glad you got such a nice looking macro.

Actually now it occurs to me that there is one potential issue with what I told you earlier. The code line

Code: Select all

$doc.addStyles $styleDoc.text
will, I believe, only give you the correct result if all the desired styles are indeed applied somewhere in the text. However if the document $styleDoc is just an empty document with styles, but no text, this might not import anything. (I could be wrong about this. I wrote all this without testing.)

An alternative that should work for such empty style files is this:

Code: Select all

$doc.addStyles $styleDoc.allStyles
This is because the '.addStyles' command allows for arrays of styles as an argument. This latter method also allows you to import just the paragraph styles, or just the character styles, etc.
philip

User avatar
loulesko
Posts: 89
Joined: 2008-01-20 11:08:35
Location: California
Contact:

Re: Import Style via Macro

Post by loulesko » 2017-03-06 10:11:05

Hi Philip,

It seems to work either way, but I went with your second suggestion, it looks more complete to me from a code perspective.

Woo hoo. :D

Thank you!

Lou

Post Reply