How to program Merge Document options

Get help using and writing Nisus Writer Pro macros.
Post Reply
rizzo124
Posts: 10
Joined: 2015-03-30 08:08:06

How to program Merge Document options

Post by rizzo124 » 2015-04-13 22:55:58

If there a way to programmatically provide the options for the Merge Document command dialog box? I guess this is a general question for other commands that have option dialog boxes.

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

Re: How to program Merge Document options

Post by phspaelti » 2015-04-15 00:47:49

Generally the answer is that there are no provisions for controlling dialog boxes. In the majority of cases this is not a problem, since the macro language usually has equivalent commands that avoid the need to go via the GUI. In some cases it may be possible to send the commands via the keyboard (with commands like "Press Button" and "Send Selector", but often this does not work well. The Merge dialog is not amenable to this kind of thing, and the macro language does not have any commands to do a mail merge. It may have been felt that this isn't the kind of thing that people will usually want to do via macro. Of course one could just write a macro that actually did the same thing (i.e. create multiple copies of a document with substitutions from a list.).
philip

rizzo124
Posts: 10
Joined: 2015-03-30 08:08:06

Re: How to program Merge Document options

Post by rizzo124 » 2015-04-15 08:05:57

phspaelti - thanks for the response. I was hoping that wasn't the answer but I had the sinking feeling that was it. I'm actually trying to automate the assembly of different types of documents using data from FilemakerPro that kicks off the process. I've got a battery of template docs that are assembled individually as called for by the user. In all cases it's merge of 1 document. I'm trying to program the input and output file locations via a Nisus macro where the macro itself gets kicked off by an Applescript. I noticed that it seems the file information in the merge dialog seems to stick with the template document and thus a name can be pre-programmed but the path has to be fixed.

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

Re: How to program Merge Document options

Post by phspaelti » 2015-04-15 08:44:36

The basic code for this would look like this:

Code: Select all

$csvPath = '~/folder/myCSVFile.csv'
$savePath = '~/otherFolder/mySaveFile.rtf'

Merge Document
Send Text $csvPath
Send Selector 'insertTab:'
Send Text $savePath
Press Button 'Merge'
I haven't tried this all the way, but provided the merge box has been set to use the csv file option it will put things in the correct place. Your macro just needs the correct file path info.
Still I would consider writing a macro which reads the csv file and then creates and saves the document on its own without using mail merge. It would probably be a more logical and reliable method. I'm pretty sure one could get the macro to use the Merge template docs, if you prefer to write the docs that way. If you need any help in doing that, let me know.
philip

rizzo124
Posts: 10
Joined: 2015-03-30 08:08:06

Re: How to program Merge Document options

Post by rizzo124 » 2015-04-15 20:43:56

Thanks for posting the code. I see now how to overcome the lack of dialog options in Nisus by just directing text input into the boxes and tabbing, etc. as if you're doing it manually. I actually went a bit further. I was able to run the macro in a blank doc, and the macro opened the template doc, filled in the merge dialog boxes and wrote the file to the directed path and then closed the doc.

Code: Select all

$templatePath = '/Volumes/ZPG/Templates/TracDocs/LC application filed.rtf'
$csvPath = '/Users/rizzo/Documents/Testing/nisus-output-1.csv'
$savePath = '/Volumes/ZPG/Templates/Document Pickup'
$saveFileName = 'LC application filed'

Open $templatePath
Merge Document
Send Text $csvPath
Send Selector 'insertTab:'
Send Text $savePath
Send Selector 'insertTab:'
Send Text $saveFileName
Press Button 'Merge'
Close
next step is figure out how to launch a nisus macro from applescript with a parameter which is the document name. Can you recommend a resource for applescript?

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

Re: How to program Merge Document options

Post by phspaelti » 2015-04-15 21:41:08

So you can write an applescript something like this

Code: Select all

tell application "Nisus Writer Pro"
	set docName to "LC application filed"
	Do Menu Macro with macro "Macro.run 'Merge to Doc with Name', Hash.new('docName','" & docName & "')"
end tell
Where "Merge to Doc with Name" is the name of your macro. And that macro should have lines like this:

Code: Select all

$args = Macro.arguments
$saveFileName = $args{'docName'}
philip

rizzo124
Posts: 10
Joined: 2015-03-30 08:08:06

Re: How to program Merge Document options

Post by rizzo124 » 2015-04-17 15:56:02

I tried doing this as an applescript but got this error.

-----
There was an error on line 1 in the macro "Unsaved Script"
Unknown menu item path:
traction-merge
-------

Not sure where unsaved script came from. The script is called something else and it's saved.

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

Re: How to program Merge Document options

Post by phspaelti » 2015-04-17 16:06:59

rizzo124 wrote:I tried doing this as an applescript but got this error.

-----
There was an error on line 1 in the macro "Unsaved Script"
Unknown menu item path:
traction-merge
-------

Not sure where unsaved script came from. The script is called something else and it's saved.
The "Unsaved Script" is referring to the argument of the "do Menu Macro" command. This command treats the argument as a macro that has been created "on the fly", and is therefore an "Unsaved Script".

I don't understand the "traction-merge". Is that supposed to be the name of your Nisus macro? But if it is, then it shouldn't be giving you this error, unless there is a mis-spelling or the like.
You should try pasting the exact text of your applescript here, so that we can see where the error is.
philip

rizzo124
Posts: 10
Joined: 2015-03-30 08:08:06

Re: How to program Merge Document options

Post by rizzo124 » 2015-04-17 20:12:54

It was a typo - you were correct in your suspicion.

Here's what I have for the applescript:

Code: Select all

tell application "Nisus Writer Pro"
	set docName to "LC IDS filed"
	set docPath to "TU Delft/0005/IDS"
	Do Menu Macro with macro "Macro.run 'traction-macro', Hash.new('docName','" & docName & "', 'docPath', '" & docPath & "')"
end tell
and here's the Nisis writer pro macro:

Code: Select all

$args = Macro.arguments
$saveFileName = $args{'docName'}
$docLocation = $args{'docPath'}

$templatePath = "/Volumes/ZPG/Templates/TracDocs/$saveFileName.rtf"
$csvPath = "/Volumes/ZPG/Templates/Exported Data/traction_output.csv"
$savePath = "/Volumes/ZPG/$docLocation"


Prompt "savefilename= $saveFileName"
Prompt "templatepath= $templatePath"
Prompt "savepath= $savePath"

Open $templatePath
Merge Document
Send Selector 'insertBackspace:'
Send Text $csvPath
Send Selector 'insertTab:'
Send Text $savePath
Send Selector 'insertTab:'
Send Text $saveFileName
Press Button 'Merge'
Close


For some reason, the merged doc is stored in the same old place and not in the dynamically set docLocation which comes in as an argument in the hash. The Prompts were put in to check the values and they are all correct. It's the 'send text' steps that aren't working apparently.

rizzo124
Posts: 10
Joined: 2015-03-30 08:08:06

Re: How to program Merge Document options

Post by rizzo124 » 2015-04-17 23:02:37

After making some progress, I wrote a Filemaker script to programmatically set the docName and docPath variables and kickoff the applescript. Now I'm getting an error from Nisus that it could not access the template .rtf document because 'permission was denied'. NSCocoaErrorDomain - code 257. Is this related to sandboxing features?

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

Re: How to program Merge Document options

Post by phspaelti » 2015-04-18 04:26:12

rizzo124 wrote:Now I'm getting an error from Nisus that it could not access the template .rtf document because 'permission was denied'. NSCocoaErrorDomain - code 257. Is this related to sandboxing features?
Probably.
You can add a File.requireAccessAtPath command, but it will prompt you to confirm the access. Maybe enabling access to the folder where you keep the templates might work around this.
philip

rizzo124
Posts: 10
Joined: 2015-03-30 08:08:06

Re: How to program Merge Document options

Post by rizzo124 » 2015-04-18 16:03:18

The latest instructions from Nisus seems to have overcome the permissions issue. Permission is granted for any folder and all nested subfolders and is sticky. However, the Merge command doesn't seem to work properly. It merges but seems to ignore the path settings that are placed in the dialog box by the macro. Something weird is definitely going on here.

rizzo124
Posts: 10
Joined: 2015-03-30 08:08:06

Re: How to program Merge Document options

Post by rizzo124 » 2015-04-18 16:33:55

Does Nisus macro language have commands like 'substitute' that can do the work of the 'merge' command. I'm not getting a warm and fuzzy feeling from trying to do the Merge command via the macro lines.

Alternatively, I can just have the Merge command in the macro always merge to the same folder and then after move the file to the proper subfolder location. I guess that can be done via AppleScript as well as Nisus macro?

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

Re: How to program Merge Document options

Post by phspaelti » 2015-04-18 19:44:44

rizzo124 wrote:However, the Merge command doesn't seem to work properly. It merges but seems to ignore the path settings that are placed in the dialog box by the macro. Something weird is definitely going on here.
The only thing I can imagine right now is that there is some kind of timing issue. Perhaps adding a delay (a "Wait" statement) might help, but I really don't know. I think Nisus should add a proper "Merge Document" command to the macro language to avoid needing to go through the dialog.
rizzo124 wrote:Does Nisus macro language have commands like 'substitute' that can do the work of the 'merge' command. I'm not getting a warm and fuzzy feeling from trying to do the Merge command via the macro lines.
I guess 'substitute' would be 'Find and Replace', which is kind of Nisus' bread and butter :)

I just tried writing a 'home brewed' Merge Document function. It came out like this:

Code: Select all

$mergeData = Hash.new('data label 1', 'some data', 'data label 2', 'some data', …)

$doc = Document.open '~/merge templates folder/merge template.dot'
$mergePHs = $doc.text.findAll "(Merge Record Number)", 'Ea'
foreach $ph in reversed $mergePHs
$n = Encode RTF $ph.subtext
$n = Cast to String $n
$n.find 'MERGEFIELD "(?<mf>.+?)"', '$E'
$doc.text.replaceInRange $ph.range, $mergeData{$mf}
end
Important note: The "(Merge Record Number)" has to be some merge place holder that was inserted via the Insert menu. For Nisus Find and Replace all merge place holders look the same, which is why I used the RTF code to tell which is which.

Since the template file is a ".dot" file, opening it will create an untitled copy, and you can just substitute the place holders with data. This macro works for a single merge, which I believe is what you were looking for. You could pass the merge data in using the same argument hash as was done for the file path and file name. Alternatively you could open a .CVS file and read the data out of that. At the end of this macro you could then save the open file to whatever path and close it.

Hope this helps
philip

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

Re: How to program Merge Document options

Post by phspaelti » 2015-04-18 23:42:06

I am appending here a complete "home brewed" Merge Document macro. It works with data from a CSV file and will handle multiple lines of data. One limitation is that it won't work correctly, if the data contains commas (even if the data is quoted). Quotes will not be removed from the data either.
Attachments
Merge Document (homemade).nwm
(6.38 KiB) Downloaded 163 times
philip

Post Reply