Substitute selection by a file

Get help using and writing Nisus Writer Pro macros.
Post Reply
tomoyuki-kosaka
Posts: 41
Joined: 2009-02-17 17:44:12

Substitute selection by a file

Post by tomoyuki-kosaka »

I made a macro, its simple one.

Code: Select all

$doc1 = Document.active
$file = Choose File '~/Documents', 'Choose File to insert'
$doc2 = Document.open $file
$text = $doc2.allTexts
Document.setActive $doc1
$doc1.insertText $text, 'm'
The purpose of this macro is "Select region to substitute by a file".
However, it has a problem.
When the text inserted, there are unexpected texts are added to front and end of the inserted text.
Front one is ("
End one is ", "", "")

I hope to know the reason of the unexpected result and I'd like to make a macro which I expected.
Please help me.
Kino
Posts: 400
Joined: 2008-05-17 04:02:32

Re: Substitute selection by a file

Post by Kino »

You get those odd characters because allTexts returns an array, something like ("main body text", "header1", "header2", "header3", "footer1", "footer2", "footer3", "endnotes", "footnotes") and some text objects in your file, e.g. endnotes, are empty.

Try to use $text = $doc2.text instead of $text = $doc2.allTexts.
tomoyuki-kosaka
Posts: 41
Joined: 2009-02-17 17:44:12

Re: Substitute selection by a file

Post by tomoyuki-kosaka »

Kino, I appreciate your kindly help.
Your explanation completely understands me about the unexpected result from the macro I made.
Now, the revised macro is work fine as expected !

Thank you for your devotion to this forum.
User avatar
martin
Official Nisus Person
Posts: 5227
Joined: 2002-07-11 17:14:10
Location: San Diego, CA
Contact:

Re: Substitute selection by a file

Post by martin »

Yes, we all appreciate Kino's devotion to this forum and to helping fellow users out :)
User avatar
loulesko
Posts: 124
Joined: 2008-01-20 11:08:35
Location: California
Contact:

Re: Substitute selection by a file

Post by loulesko »

What a great macro, I made a few additions. I put in a test if the user cancels the macro exits, otherwise NWP crashes, also I have the insert file open hidden and then close after grabbing the text, and last, I use options "sa" for insert so it matches the style of the document that you are inserting into. Your preference may be different.

Code: Select all

$doc1 = Document.active
$file = Choose File '~/Documents', 'Choose File to insert'
if !$file
	exit
end
$doc2 = Document.open $file, false
$text = $doc2.text
$doc2.close
Document.setActive $doc1
$doc1.insertText $text, 'sa'	

Thanks to tomoyuki-kosaka and kino.

Lou
Post Reply