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

How to convert Perl script macros for opening documents
https://www.nisus.com/forum/viewtopic.php?f=18&t=6312
Page 1 of 1

Author:  mikecass [ 2016-03-09 06:12:18 ]
Post subject:  How to convert Perl script macros for opening documents

How can I convert my old Perl Script macros for opening documents so that they work in NWP 2.1?

Here's the one I used most frequently, but I have several others I'd also like to make work again.

#!/usr/bin/perl

`open -a "Nisus Writer Pro" '~/Documents/MIKE/JOURNALS/2010s/2016/02.16.rtf`

# end of script

Author:  capvideo [ 2016-03-09 06:57:37 ]
Post subject:  Re: How to convert Perl script macros for opening documents

mikecass wrote:
How can I convert my old Perl Script macros for opening documents so that they work in NWP 2.1?

Here's the one I used most frequently, but I have several others I'd also like to make work again.

#!/usr/bin/perl

`open -a "Nisus Writer Pro" '~/Documents/MIKE/JOURNALS/2010s/2016/02.16.rtf`

# end of script


There is a typo in that script. At the beginning of your filename, you have a single straight quote, but none at the end. If you remove that, it should work. I’ve tested it on a file of my own.

You don’t want to surround the filename with single straight quotes, because that will disable expansion of the tilde into your home directory.

Code:
 #!/usr/bin/perl

`open -a "Nisus Writer Pro" ~/Documents/MIKE/JOURNALS/2010s/2016/02.16.rtf`

Author:  mikecass [ 2016-03-09 08:08:55 ]
Post subject:  Re: How to convert Perl script macros for opening documents

Thanks, Capvideo, but with the straight quote removed I now get the message

Perl reported an error:
Can't find string terminator "`" anywhere before EOF at /Users/cass_mm/Documents/Nisus Documents/Macros/open journal.pl line 3.

(I've never understood macros; I've simply copied other Nisus users' macros and inserted my own folder names and file names.)

Author:  martin [ 2016-03-09 11:02:30 ]
Post subject:  Re: How to convert Perl script macros for opening documents

Unless you have a reason for using Perl, I'd convert your macro to a regular Nisus Writer macro. The code is pretty simple:
Code:
Document.open "~/Documents/MIKE/JOURNALS/2010s/2016/02.16.rtf"

However, whether you're using Perl or a Nisus Writer macro, you will need to take into account sandboxing restrictions. Basically the system (OSX) will deny access to the desired file unless Nisus Writer has been given access to it by some kind of user interaction– usually by selecting the file in a standard system dialog.

A revised Nisus Writer macro that correctly takes into account sandboxing would be:
Code:
File.requireAccessAtPath "~/Documents/MIKE/JOURNALS/2010s/2016/02.16.rtf"
Document.open "~/Documents/MIKE/JOURNALS/2010s/2016/02.16.rtf"

That macro will ask you to select the target file the first time you use it. After that, on any subsequent uses, the macro will open the target file without interruption.

If you do use a macro like that, you probably want to declare the target file's path once. That will eliminate the redundancy and make it easier to edit:
Code:
$path = "~/Documents/MIKE/JOURNALS/2010s/2016/02.16.rtf"
File.requireAccessAtPath $path
Document.open $path


If you have a lot of these files and plan to create macros for them all, another way to comply with sandboxing is to declare that you need access to the enclosing folder, eg:
Code:
File.requireAccessAtPath "~/Documents/"
Document.open "~/Documents/MIKE/JOURNALS/2010s/2016/02.16.rtf"

In this way you'll only be prompted to grant access to the Documents folder once, no matter how many macros you have.

I hope that helps, and wasn't confusing. Please let me know if you have any questions.

Author:  mikecass [ 2016-03-09 12:36:18 ]
Post subject:  Re: How to convert Perl script macros for opening documents

Thank you very much, Martin. I am "good to go"!

Author:  martin [ 2016-03-09 13:02:26 ]
Post subject:  Re: How to convert Perl script macros for opening documents

mikecass wrote:
Thank you very much, Martin. I am "good to go"!

Excellent! :)

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