How to convert Perl script macros for opening documents

Everything related to our flagship word processor.
Post Reply
mikecass
Posts: 5
Joined: 2016-02-27 13:18:36

How to convert Perl script macros for opening documents

Post by mikecass » 2016-03-09 06:12:18

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

capvideo
Posts: 20
Joined: 2008-03-16 16:41:16
Contact:

Re: How to convert Perl script macros for opening documents

Post by capvideo » 2016-03-09 06:57:37

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: Select all

 #!/usr/bin/perl

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

mikecass
Posts: 5
Joined: 2016-02-27 13:18:36

Re: How to convert Perl script macros for opening documents

Post by mikecass » 2016-03-09 08:08:55

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.)

User avatar
martin
Official Nisus Person
Posts: 4368
Joined: 2002-07-11 17:14:10
Location: San Diego, CA
Contact:

Re: How to convert Perl script macros for opening documents

Post by martin » 2016-03-09 11:02:30

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: Select all

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: Select all

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: Select all

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

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.

mikecass
Posts: 5
Joined: 2016-02-27 13:18:36

Re: How to convert Perl script macros for opening documents

Post by mikecass » 2016-03-09 12:36:18

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

User avatar
martin
Official Nisus Person
Posts: 4368
Joined: 2002-07-11 17:14:10
Location: San Diego, CA
Contact:

Re: How to convert Perl script macros for opening documents

Post by martin » 2016-03-09 13:02:26

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

Post Reply