Substitute selection by a file

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

Substitute selection by a file

Post by tomoyuki-kosaka » 2010-06-25 02:40:19

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 » 2010-06-25 06:16:12

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: 37
Joined: 2009-02-17 17:44:12

Re: Substitute selection by a file

Post by tomoyuki-kosaka » 2010-06-27 18:22:08

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

Re: Substitute selection by a file

Post by martin » 2010-06-28 10:57:05

Yes, we all appreciate Kino's devotion to this forum and to helping fellow users out :)

User avatar
loulesko
Posts: 89
Joined: 2008-01-20 11:08:35
Location: California
Contact:

Re: Substitute selection by a file

Post by loulesko » 2011-04-17 10:07:11

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