Reply to topic  [ 5 posts ] 
Import Style via Macro 
Author Message
User avatar

Joined: 2008-01-20 11:08:35
Posts: 89
Location: California
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


2017-03-05 10:26:00
Profile WWW
User avatar

Joined: 2007-02-07 00:58:12
Posts: 876
Location: Japan
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.

_________________
philip


2017-03-05 17:36:05
Profile
User avatar

Joined: 2008-01-20 11:08:35
Posts: 89
Location: California
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


2017-03-05 22:28:20
Profile WWW
User avatar

Joined: 2007-02-07 00:58:12
Posts: 876
Location: Japan
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.

_________________
philip


2017-03-05 22:57:35
Profile
User avatar

Joined: 2008-01-20 11:08:35
Posts: 89
Location: California
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


2017-03-06 10:11:05
Profile WWW
Display posts from previous:  Sort by  
Reply to topic   [ 5 posts ] 

Who is online

Users browsing this forum: Google [Bot] 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:  
Powered by phpBB® Forum Software © phpBB Group
Designed by ST Software