Page 1 of 1

Macro command to Import from Style Library…

Posted: 2018-10-25 02:13:21
by vwnisus
I am new to Nisus Writer Pro Macros (upgraded to version 3.0).

I need open rtf files provided to me and wish to then carry out some formatting to which I wish to apply some styles which I have saved in style library.

Is there a macro command to import a specific style collection from the style library?

Use the menu command method does not appear to work here.

Any help gratefully received.

Re: Macro command to Import from Style Library…

Posted: 2018-10-25 04:10:44
by phspaelti
Hello vw,
there is no specific command to import styles. The macro language has a command to add styles to a document though. You'll need the document object. So it looks like this:

Code: Select all

$doc = Document.active
$doc.addStyles $myStyle
This command can also take an argument to tell it what to do in case of a style conflict. For example "replace" will override the conflict.

So you can get styles from your Style Library by opening the desired Style Library Document, getting the styles you want, and then adding them to the other document. The whole will look something like this:

Code: Select all

$editDoc = Document.active
$styleLibDoc = Document.open "style/library/file/path"
$editDoc.addStyles $styleLibDoc.allStyles, "replace"
$styleLibDoc.close

Re: Macro command to Import from Style Library…

Posted: 2018-11-02 05:15:10
by vwnisus
Philip,

Thank you for providing working code. I was able to implement it successfully (although I can understand how individual element work, but how you constructed the macro is beyond me).

My final macro:

Code: Select all

$editDoc = Document.active
$styleLibDoc = Document.open "/Users/victor/Dropbox/Nisus/Nisus Writer Settings/Style Library/LexisNexis Case Formatting.rtf"
$editDoc.addStyles $styleLibDoc.allStyles, "replace"
$styleLibDoc.close
Set Zoom 200
Select All
Format:Paragraph Style:LexisNexis

Format:Text Color:Black

Find and Replace ‘\n\n+’, ‘\n’, ‘Eas’

Save

Sleep 2

Close Window

Re: Macro command to Import from Style Library…

Posted: 2020-08-25 01:18:44
by vwnisus
I am trying to use a modified version of the macro code provided by Philip as follows:

Code: Select all

$folder = "~/Dropbox/nisus/Nisus Writer Settings/Style Library"
File.requireAccessAtPath($folder)

$editDoc = Document.active

$styleLibDoc = Document.open "~/Dropbox/nisus/Nisus Writer Settings/Style Library/Case report.rtf"

$editDoc.addStyles $styleLibDoc.allStyles, "replace"
$styleLibDoc.close
When I try to run the macro I am getting the following error:
There was an error on line 2 in the macro "Macro Find and Replace".

The file does not exist:

~/Dropbox/nisus/Nisus Writer Settings/Style Library
Screenshot 2020-08-25 at 10.08.08.png
Screenshot 2020-08-25 at 10.08.08.png (185 KiB) Viewed 16302 times
However, the folder and and file both do exist.
Screenshot 2020-08-25 at 10.16.19.jpg
Screenshot 2020-08-25 at 10.16.19.jpg (150.9 KiB) Viewed 16302 times
Screenshots attached. Help with this would be gratefully received.

Re: Macro command to Import from Style Library…

Posted: 2020-08-25 04:35:00
by xiamenese
I am trying to use a modified version of the macro code provided by Philip as follows:
CODE: SELECT ALL

$folder = "~/Dropbox/nisus/Nisus Writer Settings/Style Library"
File.requireAccessAtPath($folder)

$editDoc = Document.active

$styleLibDoc = Document.open "~/Dropbox/nisus/Nisus Writer Settings/Style Library/Case report.rtf"

$editDoc.addStyles $styleLibDoc.allStyles, "replace"
$styleLibDoc.close
When I try to run the macro I am getting the following error:
There was an error on line 2 in the macro "Macro Find and Replace".

The file does not exist:

~/Dropbox/nisus/Nisus Writer Settings/Style Library
In your new macro, you have "~/Dropbox…" where in the earlier versions you had "/Dropbox…" without the "~". Could that be it?

Says Mark a complete non-programmer! :D

Re: Macro command to Import from Style Library…

Posted: 2020-08-25 06:37:36
by johseb
Have you tried granting access to the specific file you are going to use (Case Report.rtf)?
The message reads "file doesn't exist" so it seems to expect a file.

In any case the Macro Reference suggests to use the built-in macro "Manage Macro File Access" to grant access once and for all.
For instance you could grant access to your entire Dropbox forlder (or even your Home) using the mentioned macro and then you should be able to run any macros that need to open files in ~/Dropbox whitout using the explicit requireAccessAtPath.

Re: Macro command to Import from Style Library…

Posted: 2020-08-25 06:53:08
by phspaelti
xiamenese wrote: 2020-08-25 04:35:00 In your new macro, you have "~/Dropbox…" where in the earlier versions you had "/Dropbox…" without the "~". Could that be it?

Says Mark a complete non-programmer! :D
Well the "~" is just an abbreviation for "/Users/victor" and that would seem to be where the "Dropbox" folder is located.

In principle this should work. And personally I just tried a parallel case myself. Did the "required access" command prompt you? Or did that go by without complaint?

But basically Mark is on to something. I would try some simple cases first.
  • Write out the full path
  • Move/Copy the file to the home folder and try it there
  • Try opening other files
  • etc.
Select just the line you want to test and use the Run Selection as Macro.

Also you might try using the following:

Code: Select all

$filePath = Choose File
$styleLibDoc = Document.open $filePath
That way the macro collects the path by itself. This makes sure that a file path error is really not the culprit.

Re: Macro command to Import from Style Library…

Posted: 2020-08-25 09:19:00
by vwnisus
Thank you all for all the replies.

I tried moving the style file to the Documents folder and changed the macro to include what Philip suggested:

Code: Select all

$filePath = Choose File
$styleLibDoc = Document.open $filePath
and it ran without problem. It also ran without problem when selecting the style file located in the DropBox folder. Again without problem.

I then tried with further version of the macro:

Code: Select all

$editDoc = Document.active
$filePath = "~/Dropbox/nisus/Nisus Writer Settings/Style Library/Case report.rtf"
$styleLibDoc = Document.open $filePath
$editDoc.addStyles $styleLibDoc.allStyles, "replace"
$styleLibDoc.close
This too ran without problem. I do not know why I had an issue before...

Re: Macro command to Import from Style Library…

Posted: 2020-08-25 17:42:51
by phspaelti
vwnisus wrote: 2020-08-25 09:19:00 I do not know why I had an issue before...
Computers move in mysterious ways…