nisus.com
https://www.nisus.com/forum/

Import Style via Macro
https://www.nisus.com/forum/viewtopic.php?f=17&t=6691
Page 1 of 1

Author:  loulesko [ 2017-03-05 10:26:00 ]
Post subject:  Import Style via Macro

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:
$doc = Document.active
$doc.addStyles 'Manuscript Draft', 'replace'


Thank you.

Lou

Author:  phspaelti [ 2017-03-05 17:36:05 ]
Post subject:  Re: Import Style via Macro

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

Author:  loulesko [ 2017-03-05 22:28:20 ]
Post subject:  Re: Import Style via Macro

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

Author:  phspaelti [ 2017-03-05 22:57:35 ]
Post subject:  Re: Import Style via Macro

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

Author:  loulesko [ 2017-03-06 10:11:05 ]
Post subject:  Re: Import Style via Macro

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

Page 1 of 1 All times are UTC - 8 hours
Powered by phpBB® Forum Software © phpBB Group
http://www.phpbb.com/