Reply to topic  [ 6 posts ] 
How to convert Perl script macros for opening documents 
Author Message

Joined: 2016-02-27 13:18:36
Posts: 5
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


2016-03-09 06:12:18
Profile

Joined: 2008-03-16 16:41:16
Posts: 20
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`

_________________
Mimsy Were the Borogoves


2016-03-09 06:57:37
Profile WWW

Joined: 2016-02-27 13:18:36
Posts: 5
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.)


2016-03-09 08:08:55
Profile
Official Nisus Person
User avatar

Joined: 2002-07-11 17:14:10
Posts: 4251
Location: San Diego, CA
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.


2016-03-09 11:02:30
Profile WWW

Joined: 2016-02-27 13:18:36
Posts: 5
Thank you very much, Martin. I am "good to go"!


2016-03-09 12:36:18
Profile
Official Nisus Person
User avatar

Joined: 2002-07-11 17:14:10
Posts: 4251
Location: San Diego, CA
mikecass wrote:
Thank you very much, Martin. I am "good to go"!

Excellent! :)


2016-03-09 13:02:26
Profile WWW
Display posts from previous:  Sort by  
Reply to topic   [ 6 posts ] 

Who is online

Users browsing this forum: No registered users and 11 guests


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:  
cron
Powered by phpBB® Forum Software © phpBB Group
Designed by ST Software