Run a specific macro whenever a specific file is opened?

Get help using and writing Nisus Writer Pro macros.
Post Reply
MyronGochnauer
Posts: 5
Joined: 2009-08-12 02:59:46

Run a specific macro whenever a specific file is opened?

Post by MyronGochnauer » 2016-12-11 06:28:03

Is there any way to make NWP run a specific macro whenever a specific file is opened?
Example:
Select a word or phrase (perhaps in Safari) and copy to clipboard
Open "Serial_numbers.rtf" in NWP
NWP automatically runs "Find clpbrd txt.nwm"
"Find clpbrd txt.nwm" opens Find dialogue, pastes clipboard text, etc.

One way to do this would be to write an AppleScript that passed the file and macro names to NWP. Can this be done? I assume NWP can be started from the terminal and passed a file(name) to open. But can it be passed a *macro(name)* to execute and not simply to load for editing?

User avatar
phspaelti
Posts: 918
Joined: 2007-02-07 00:58:12
Location: Japan

Re: Run a specific macro whenever a specific file is opened?

Post by phspaelti » 2016-12-11 17:15:48

Hello Myron,

1. You are aware that Nisus shares the find expression with other applications? So if you do a <command>F search in your browser then that find expression will also be transferred to the find box in Nisus.

2. Nisus does not have the ability to automatically run macros.

3. On the specific question whether an applescript can launch a macro the answer is "yes". Specifically the command is called "Do Menu Macro with macro". So assuming you have a macro "Find clpbrd txt" in your macro menu, you could use an applescript like this to run it.

Code: Select all

tell application "Nisus Writer Pro"
   activate
   Do Menu Macro with macro "Find clpbrd txt"
 end tell
philip

adryan
Posts: 178
Joined: 2014-02-08 12:57:03
Location: Australia

Re: Run a specific macro whenever a specific file is opened?

Post by adryan » 2016-12-11 22:24:05

G’day, Myron, Philip et al

I for one was not aware that Nisus shares Find expressions with other applications, so thanks for that tip, Philip. I suspect it’s the easiest way of doing what you want, Myron.

Another possibility is to construct a Service (“Look up in my_doc”, say) accessible via the browser’s main menu or via a contextual menu. I haven’t had experience of creating Services.

I think Philip is right about Nisus not being able to run macros automatically. However, there are a couple of ways to link macro actions to a specific file. (There are probably more if one delves into the Terminal.)

One is to open the file from within a macro. You run the macro, it opens the file, then performs the desired actions on the file.

Another way is to extend the AppleScript approach suggested by Philip. The idea is to get the script to identify the frontmost Nisus document (after perhaps opening it if its icon was dropped on a standalone AppleScript application icon). Suitable control statements would then choose a Do Menu Macro command line from whatever set of such command lines you include in the script. The appropriate Nisus macro would thus be executed on the Nisus document in question. This approach has the advantage that you can drop any Nisus document file icon on a single AppleScript application icon, and the script does the rest. You don’t have to trawl through the hierarchical Macro menu structure in Nisus Writer to find the macro that matches the file.

Cheers,
Adrian
MacBook Pro (mid-2014)
macOS Sierra 10.12.6
Nisus Writer user since 1996

Þorvarður
Posts: 251
Joined: 2012-12-19 05:02:52

Re: Run a specific macro whenever a specific file is opened?

Post by Þorvarður » 2016-12-14 00:52:15

Hello everybody,
MyronGochnauer wrote:One way to do this would be to write an AppleScript that passed the file and macro names to NWP. Can this be done?
If you just want to select text in Safari and automatically find that text in a certain NWP document, you can do that with a combination of AppleScript and a Nisus macro. The easiest way is probably to use Automator for this and save it as a Service.
After you have saved it, it should appear in System Preferences > Keyboard > Shortcuts > Services where you can assign a shortcut to it.

Here's the idea:
[1] You can begin the AppleScript with executing the menu command "Use Selection for Find" in Safari (Edit > Find > Use Selection to Find). This is better than copy the text to the clipboard, as you suggested, because Safari shares the search expression with Nisus, thus making it easy to find multiple occurrences in the Nisus document by hitting the commandKey and "g" (= Find Next. ) Make sure the menu command "Use Selection to Find" has a keyboard shortcut, and use that shortcut in the script to execute the command. The usual macOS shortcut for this menu command is Command + e across all applications.
[2] Next, let the script open a specified Nisus file and
[3] now ask NWP to run a macro to find the text.

And here's a step by step instruction:
1. In Automator, choose "New" from the file menu.
2. Choose "Service" as a type.
3. Click on Utilities in the Actions pane and drag "Run AppleScript" into the empty space on the right.

You will see this:

Code: Select all

on run {input, parameters}
	(* Your script goes here *)
	return input
end run
4. Select all and replace it with:

Code: Select all

on run {input, parameters}
	
	-- This script puts a text selection in Safari to Safari's find box and then finds that selection in a specified Nisus file
	
	tell application "System Events" to keystroke "e" using command down -- assumes that the menu command "Use Selection for Find" in Safari has been assigned the keystroke shurtcut Command+e
	
	tell application "Finder"
		activate
		open document file "Serial_numbers.rtf" of folder "Desktop" of folder "XXX" of folder "Users" of startup disk
	end tell
	
	tell application "Nisus Writer Pro"
		activate
		-- Delay seems to be necessary for Nisus. Otherwise the selection will not be found. 0.2 seems to be the minimum on my computer. Increase if necessary…
		delay 0.2
		Do Menu Macro with macro "Find Next from Safari"
	end tell
	
	return input
end run
5. You must replace the folder name "XXX" with the name of your home folder, and adjust the file path to the file "Serial_numbers.rtf".
6. Make sure that at the top of the window Service receives "no input" and "in Safari.app" are selected.

7. Then save the Service with a meaningful name. It will be saved in this location:
Users <Your Home folder <Library <Services

8. Now create and install this Nisus macro with the name "Find Next from Safari".

Code: Select all

# This macro is the 2nd part of a Service called "Select in Safari and Find in NWP file"
Select All
Select Start
$searchExpression = Read Find Expression
$found = Find Next
if ! $found
	prompt "Oops! Sorry…, " & "'" & $searchExpression & "'" & " couldn't be found!"
	exit
End
9. Go to System Preferences > Keyboard > Shortcuts > Services and assign a key shortcut to the Service.

Every time you now select text in Safari and hit the key combination, the Service will be triggered and the text will be found in the NWP document.

adryan
Posts: 178
Joined: 2014-02-08 12:57:03
Location: Australia

Re: Run a specific macro whenever a specific file is opened?

Post by adryan » 2016-12-16 23:53:19

G'day, all

Thanks to Þorvarður for the quick tutorial on Service creation.

However, it bothered me that the proposed solution was specific to Safari, when it might be preferable to have a Service that takes selected text from any application and then looks it up in a particular Nisus document.

The Use Selection for Find command (with Cmd+E shortcut) is also available in Preview, albeit with a peculiarity. You can select a single word in a Preview document, invoke the command, then copy the contents of the Preview Find field and paste them into a Nisus Writer document. You can also copy the original selection in the body of the Preview document and paste it into a Nisus Writer document. The results are not always the same! In some cases, the second procedure can transfer unsuspected trailing characters (eg, space plus paragraph return) along with the text of interest. However, the Use Selection for Find command seems to have stripped them from the text it displays in the Preview Find field. This is probably a peculiarity of the document I had before me (which was a sort of bilingual dictionary), but such behavior has consequences for the design of a more universally applicable text-finding Service.

Other applications (such as iCab) have no such Use Selection for Find command.

With these considerations in mind, here is the code for a Nisus macro that seems to work. The filepath for the target Nisus document will need to be altered as desired. The filename of the macro (here cited as "Find Clipboard Contents in Test") can also be altered to reflect the target document.

Code: Select all

#NisusMacroProperty Name: Find Clipboard Contents in Test
# This macro finds in a Nisus document (whose path is here designated as “~/Desktop/test.rtf”) the contents of the active clipboard. It is mainly for use when those contents were derived from some other application.
# When the macro is called from a Service, we may need to edit the text selection because text selections obtained by Automator from (for example) Preview may have unwanted trailing stuff.
$text_selection = Read Clipboard
# Remove unwanted trailing stuff. You might want to add other characters (eg, \\t) here as well.
$text_selection.find '(.*)[^ \\n]', 'E$'
$find_expression = $0
# Now we can use this as the Find expression for use in the target document.
Write Find Expression $find_expression
# Nominate the path to the target document.
$path_to_test_file = '~/Desktop/test.rtf'
# Open the target document and ensure it is the frontmost one.
Document.open $path_to_test_file, 'true'
# Find the text in the target document, with error message if text not found.
Select Document Start
$found = Find Next
If ! $found
	Prompt "Text not found."
	Exit
End
In Automator, the Service is set (at the top) to receive selected text in any application. The code for the Run AppleScript action is as follows. (The filename of the macro may need to be altered.)

Code: Select all

on run {input, parameters}
	
	set the clipboard to input as text
	tell application "Nisus Writer Pro"
		activate
		Do Menu Macro with macro "Find Clipboard Contents in Test"
	end tell
	
	return input
end run
I have named the Service "Look up in Test", to reflect the name of the target Nisus document. It should work with text selected in any application.

It's important to note, too, that the solution provided by Þorvarður will not necessarily work as intended if the target Nisus document is already open but not the frontmost Nisus document at the time the Service is called. The code I have given here attempts to address that problem.

Cheers,
Adrian
MacBook Pro (mid-2014)
macOS Sierra 10.12.6
Nisus Writer user since 1996

Þorvarður
Posts: 251
Joined: 2012-12-19 05:02:52

Re: Run a specific macro whenever a specific file is opened?

Post by Þorvarður » 2016-12-18 08:28:04

Hello Adrian,

Thank you for the enhanced macro.

There is a slight problem with your Service, at least on Sierra 10.12.1. It won't appear unter the Services menu in Preview and Skim. It appears in PDFpenPro and PDF Expert as expected and in Safari, iCab, TextEdit, Pages and MS Word (I didn't test more.) This limitation seems to be Preview and Skim related somehow. Both do not display any text related Services at all, i.e. Services that appear unter "Text" in the Services menu.
adryan wrote:It's important to note, too, that the solution provided by Þorvarður will not necessarily work as intended if the target Nisus document is already open but not the frontmost Nisus document at the time the Service is called.
I could not verify that. This small piece of code in the Service itself should always make it the frontmost document, no matter whether it's open or not.

Code: Select all

tell application "Finder"
		activate
		open document file "Serial_numbers.rtf" of folder "Desktop" of folder "XXX" of folder "Users" of startup disk
	end tell

adryan
Posts: 178
Joined: 2014-02-08 12:57:03
Location: Australia

Re: Run a specific macro whenever a specific file is opened?

Post by adryan » 2016-12-18 13:34:42

G’day, Þorvarður et al

My version of the Service works for me in Preview under Yosemite. If it doesn’t work in a Preview document, I guess the first thing I would do would be to check whether the Preview document itself recognizes a selection as text, by trying the Use Selection for Find command and/or by checking whether other text-based Services (such as Summarize) appear in the Services menu. This would be in case there was something peculiar about the particular Preview document. If Þorvarður was running my Service on the same document he used successfully with his own, one has to wonder about some change between Yosemite and Sierra.

With respect to frontmost documents, I tested things once more, this time in a little more detail. I wrote a one-line Nisus macro called "Find First Word" which does exactly what it says in the frontmost Nisus document. I then wrote the following AppleScript script:–

Code: Select all

tell application "Finder"
	activate
	open document file "test.rtf" of folder "Desktop" of folder "XXXX" of folder "Users" of startup disk
end tell

tell application "Nisus Writer Pro"
	activate
	Do Menu Macro with macro "Find First Word"
end tell
Again under Yosemite, the Finder tell block ensures that the file is open, but it is not the frontmost item, because it is opened behind the frontmost Finder window. It does appear to be the frontmost Nisus document, as can be verified by halting execution of the script before the Nisus Writer tell block and making Nisus Writer the active application.

When the full script is run (ie, both tell blocks), the file is again the frontmost Nisus document, but — wait for it! — it is not necessarily the object of the Nisus macro! This depends on whether or not the file is already open at the time the script is run. If the test file was already open, everything works as expected. However, if the test file was not already open, the macro finds the first word in what was the frontmost Nisus document before the test file was opened.

To my mind, this behavior points to a problem with Nisus Writer’s AppleScript implementation, at least in Yosemite. I found it all very confusing when I was constructing the Service; hence my insertion of a target file activation command in the Nisus macro.

I think this is an important issue because it lies at the heart of the applicability of Do Menu Macro commands in Nisus Writer tell blocks in AppleScript scripts. If nothing else, it may mean that the Nisus Writer Pro User Guide should include advice to consider incorporation of an appropriate target file activation command in any Nisus macro destined to be called by AppleScript.

Cheers,
Adrian
MacBook Pro (mid-2014)
macOS Sierra 10.12.6
Nisus Writer user since 1996

Þorvarður
Posts: 251
Joined: 2012-12-19 05:02:52

Re: Run a specific macro whenever a specific file is opened?

Post by Þorvarður » 2016-12-18 21:15:00

Hello Adrian,
If it doesn’t work in a Preview document, I guess the first thing I would do would be to check whether the Preview document itself recognizes a selection as text, by trying the Use Selection for Find command and/or by checking whether other text-based Services (such as Summarize) appear in the Services menu.
The "Use Selection for Find"-command works fine. As I said earlier, NO text based Services appear in the Services menu when text is selected in Preview. This is how it looks like:
1.png
1.png (71.45 KiB) Viewed 3765 times
If I use PDFpenPro and PDF Expert instead of Preview, then a bunch of text based Services appear, which indicates that there is something wrong with Preview in Sierra.

I googled and, lo and behold, quickly found the confirmation, but, unlike by some users below, opening the Inspector panel in Preview doesn't make any difference by me. Still no text services.

https://discussions.apple.com/thread/77 ... 0&tstart=0


I plan to update my system to version 10.12.2 later today after making a fresh clone. I will report if the problem has been fixed.


Cheers,
Þorvarður

Þorvarður
Posts: 251
Joined: 2012-12-19 05:02:52

Re: Run a specific macro whenever a specific file is opened?

Post by Þorvarður » 2016-12-20 03:54:41

Just finished updating to system 10.12.2. Still no text services in Preview. Problem persists.

Here is what Christiaan M. Hofman, a developer of Skim (http://skim-app.sourceforge.net), said:
"Yes, it does seem that PDFkit on Sierra is a completely different framework from earlier versions. And it is badly broken and badly lacking. I don’t see how a lot of features, in particular UI updating and UI is even possible in this framework as documented. Moreover, about most of the documentation does not seem to have written. This is truly amazingly bad behavior, this framework seems to be extremely bad designed, extremely buggy, so overall a long way away from release status. Whoever wrote that piece of crap, and whoever approved of releasing it should be fired right away, and Apple needs to release 10.12.1 with the old PDFKit immediately. Sorry for the rant, but Apple really lost it this time. And they already have a very bad history with PDFKit, but this takes the cake. They may have finally killed Skim this time."
http://www.mnott.de/how-to-workaround-t ... in-sierra/

and

https://sourceforge.net/p/skim-app/bugs/1109/

See also:
https://discussions.apple.com/message/3 ... ge30825690

adryan
Posts: 178
Joined: 2014-02-08 12:57:03
Location: Australia

Re: Run a specific macro whenever a specific file is opened?

Post by adryan » 2016-12-20 12:23:42

G’day, Þorvarður et al

Thanks for following through on that. What a pain!

As I said in the “Yosemite forever” thread in The Blank Page forum here, Yosemite is as far along the path as I’ve gone at this stage, not wishing to push my luck with what I perceive to be very problematic subsequent versions of the operating system. Even then, I think that Mavericks is better.

One thing I’ve noticed recently with Preview under Yosemite is that quitting Preview does not necessarily close all open Preview documents and quit the application entirely. Instead, it closes one document, leaves the others and the application itself open, and requires subsequent calling of the Quit command to dismiss everything. What on earth is Apple doing?

A glimmer of hope is that your method of calling System Events in relation to Safari might also work in relation to Preview under Sierra. If so, this could be incorporated somehow in the Service I described — albeit with Preview as a special case — and so make text selection from all the applications of interest available to Nisus Writer.

Cheers,
Adrian
MacBook Pro (mid-2014)
macOS Sierra 10.12.6
Nisus Writer user since 1996

Þorvarður
Posts: 251
Joined: 2012-12-19 05:02:52

Re: Run a specific macro whenever a specific file is opened?

Post by Þorvarður » 2016-12-21 22:28:48

Hello Adrian and others,
Yosemite is as far along the path as I’ve gone at this stage, not wishing to push my luck with what I perceive to be very problematic subsequent versions of the operating system.
I can understand that. Apart from changing the name from Mac OS to macOS, there have been no new features in Sierra worth talking about except perhaps the new reference sources for the Dictionary. I'm an addicted dictionary junkie, I need those dictionaries.

To be fair, Sierra apparently allows better integration between single devices. I have neither an iPhone nor an iPad, so that doesn't mean much to me. I played with Siri for two or three days; then I came to the conclusion that I don't need a talking application to tell me what time it is and where the nearest restaurant is. I already know that.

I bear a grudge against Apple for removing useful features such as a colored sidebar and custom icons in sidebar, while at the same time they make it impossible for developers to implement those feature in utilities (like TotalFinder and XtraFinder). I know I could use Path Finder to get back colored sidebar, but that would be a bit of an overkill for me.

There used to be a small free plug-in available called BetterDictionary to bookmark words in the Dictionary. Good if you are learning a foreign language. It was working perfectly in OS 10.10.3, but stopped working in 10.10.4, and the developer has no idea why and simply gave up. Every new update had killed his plug-in. In an email to me from January 27, 2015 he wrote: "with every release creating these kinds of extensions gets harder and harder, and now on Yosemite, it's downright barbaric".

http://pooriaazimi.github.io/BetterDictionary/
One thing I’ve noticed recently with Preview under Yosemite is that quitting Preview does not necessarily close all open Preview documents and quit the application entirely. Instead, it closes one document, leaves the others and the application itself open, and requires subsequent calling of the Quit command to dismiss everything.
That's indeed odd.

I have a question. User developed services are saved to this location: Users > Home folder > Library > Services and can be accessed there; but where are services created by individual applications stored? For example, Nisus installs a text service called "New Nisus Document With Selection". I searched for this service using the "Find Any File" application (donationware; http://apps.tempel.org/FindAnyFile/), but I can't find it. It seems not to be inside NWP either. Any idea where it is? If we find it, then perhaps we can modify it.

__________
Þorvarður

adryan
Posts: 178
Joined: 2014-02-08 12:57:03
Location: Australia

Re: Run a specific macro whenever a specific file is opened?

Post by adryan » 2016-12-22 05:59:21

G’day, Þorvarður et al

I did a quick manual trawl of what I considered to be likely places for Services in the three Libraries but could find none that related to Nisus or to another application I have (a bilingual dictionary) that also installs a Service. Finder searches also failed to find any Services relating to these applications.

Terminal searches using “sudo find” unearthed lots of technical files, but none I thought might represent a Service. Mind you, some filenames were inscrutable (eg, containing 30-character alphanumeric strings), and Terminal flagged quite a few files as “Permission denied”.

When the Nisus Writer installer package is opened in BBEdit, you are confronted with gobbledegook. The word “Nisus” appears twice, but “service” never. (All searches have been case-insensitive.)

If you open the installer package normally, the Installer application opens. Its menu has a Show Files command that produces a list containing the paths to hundreds of files. Unfortunately, the Installer application has no Find facility, nor can one select the whole list and copy it to an application that does. (I leave it as an exercise for the reader to determine whether setting an application to search its own package contents is a good idea or not. Of course, this is a trick question!) All I could do was scroll down and down and down, hoping something relevant would grab my attention. It didn’t. Of interest, though, was that the names of the pre-installed macros are all readily visible. But it would be very easy to miss a single Service-related file using this approach.

So that’s as far as I’ve got.

Cheers,
Adrian
MacBook Pro (mid-2014)
macOS Sierra 10.12.6
Nisus Writer user since 1996

Vanceone
Posts: 117
Joined: 2013-05-03 07:06:31

Re: Run a specific macro whenever a specific file is opened?

Post by Vanceone » 2016-12-23 08:28:20

Application installed services are actually coded inside the application itself and registered with the system. It's dead certain that in this case, the Nisus services are actually written in Objective-C as part of the main application. It's all keyed to the clipboard when an application provides the Service, so there's lots of monkeying around with the Clipboard that takes place whenever you use a Nisus (or any other application provided) Service.

Yes, PDFKit broke, badly, in Sierra. They are apparently gradually fixing lots of bugs. As far as I can tell, right now the only PDF functionality that actually works is from Quartz display services and is part of the old C style coding.... long story short, it's a mess. Pretty much any application that manipulates PDF's in Sierra has had issues; PDFPenPro and a couple of others may well have no problems due to rolling their own PDF implementation, which would of course still work.

All of that ranting aside, I'm not quite sure of the issues here. Applescript in Yosemite and later has immense power (subject of course to the blasted Sandbox).

The issue seems to be that Preview does not recognize Services. Well, why use a Service then? All the Service here is doing is running an Applescript.

So just build an Applescript. Instead of using Automator, open Script Editor (found in Applications/Utilities), put in the code:

Code: Select all


   
tell application "Nisus Writer Pro"
	activate
	Do Menu Macro with macro "Find Clipboard Text in Test"
end tell
   

and save it somewhere (script format is fine). We'll be moving it. Name it something nice. Before leaving Script Editor, turn on the Script menu in Preferences. Its in the "general" tab, labeled "Show Script Menu in menu bar". Upon clicking it, you'll see a new wavy menu on the right side of the menu bar, along with all the other icons there.

Now, we need to move our new script to the right place. I recommend your ~/Library/Scripts folder. Just put it inside there, and viola, our new applescript command shows up.

Now, unfortunately, since we are not using a Service we actually have to copy the text to the clipboard first before we run this command. So, select text, command c, run the macro, and it will work. (I hope!)

adryan
Posts: 178
Joined: 2014-02-08 12:57:03
Location: Australia

Re: Run a specific macro whenever a specific file is opened?

Post by adryan » 2017-01-23 23:22:20

G’day, all

In a previous posting on this topic, I wrote: “One thing I’ve noticed recently with Preview under Yosemite is that quitting Preview does not necessarily close all open Preview documents and quit the application entirely. Instead, it closes one document, leaves the others and the application itself open, and requires subsequent calling of the Quit command to dismiss everything.”

Update: I’ve found that this can happen with Numbers also. I won’t go into the details unless someone is really curious, but it involved opening a Numbers document from within an AppleScript script. Two Numbers icons ended up in the Applications compartment of the Dock. The only situation I can think of where this might happen legitimately is when there is more than one version of Numbers on the computer, which was not the case here.

If Apple has any interest in attracting or keeping business customers especially, it cannot afford to have any glitches in Preview or Numbers.

Cheers,
Adrian
MacBook Pro (mid-2014)
macOS Sierra 10.12.6
Nisus Writer user since 1996

Post Reply