Switching to a Specified Document . . .

Get help using and writing Nisus Writer Pro macros.
Post Reply
goldste4
Posts: 98
Joined: 2010-02-22 15:06:48

Switching to a Specified Document . . .

Post by goldste4 »

Hello,

I've written an embarrassingly simple macro that cuts selected text from the active document and pastes it in another document (set off with stars and new lines so it is more visible) — basically a way to keep a record of text that I have edited out, but might want to reuse or come back to at some point in the future.

Code: Select all

Menu “:Edit:Cut:Cut”
Menu “:Window:Next Window”
Type Text “******\n”
Menu “:Edit:Paste:Paste”
Type Text “\n\n”
Menu “:Window:Last Window”
The macro assumes that I only have two documents open: the main one I'm editing and the one that holds all the scraps (the "cut" selections). The document that holds the scraps I always name "X -- " and then the same file name as the editing document".

Now the question: is there a way to have the macro automatically switch to the "X -- . . ." document, and then back to the main one so that I can keep more than these two documents open at a time? I'm sure that there is looking through the macro manual, but I suspect that given the macro virtuosity displayed on this forum it would be quicker to ask than to spend hours in what would likely be fruitless labour.

Much thanks in advance.
Josh Goldstein
Kino
Posts: 400
Joined: 2008-05-17 04:02:32

Re: Switching to a Specified Document . . .

Post by Kino »

goldste4 wrote:Now the question: is there a way to have the macro automatically switch to the "X -- . . ." document, and then back to the main one so that I can keep more than these two documents open at a time?
For example, if your “X -- . . .” document is ~/Documents/NWP/record.rtf (“~” stands for your home directory), the following macro will do the job. Before trying it, customize the first command.

Code: Select all

$path = '~/Documents/NWP/record.rtf'

$doc = Document.active
Menu ':Edit:Cut:Cut'

Open $path
Type Text "******\n"
Menu ':Edit:Paste:Paste'
Type Text "\n\n"

Document.setActive $doc
Open $path just activates a document corresponding to $path if it is already open.
goldste4
Posts: 98
Joined: 2010-02-22 15:06:48

Re: Switching to a Specified Document . . .

Post by goldste4 »

Thanks for the quick reply; very helpful. I was able to modify what you gave me so that the "scrap" file would be based on the active document:

Code: Select all

#Get name and path or document being edited.
$doc = Document.active
$path = $doc.filePath
$name = $doc.displayName

#Assemble path and name of Scrap file (always “X -- ” + edited document); always in same folder
$xpath = $path.filePathByRemovingLastComponent
$xname = ‘X -- ’
$xname &= $name
$xpath = $xpath.filePathByAppendingComponent ($xname)

Menu ':Edit:Cut:Cut'

Open $xpath

Type Text "******\n"
Menu ':Edit:Paste:Paste'
Type Text "\n\n"

Document.setActive $doc
It works very well, but whenever there is a style in the original document that isn't in the "X -- " document, the macro interrupts to ask if I want to add or use the existing styles.

Does anyone know if there is a better way to use the paste command, than I have so far figured out, so that all the attributes of the original selection are always pasted into the X file and without being asked (or the macro interrupted)?

Also, is there anyway to ensure that the pasting will always be appended to the end of the document (sometimes I return to the X file to copy material back to the original, and don't remember to move the cursor to the bottom of the document)?

Thanks,
Josh
User avatar
martin
Official Nisus Person
Posts: 5227
Joined: 2002-07-11 17:14:10
Location: San Diego, CA
Contact:

Re: Switching to a Specified Document . . .

Post by martin »

goldste4 wrote:It works very well, but whenever there is a style in the original document that isn't in the "X -- " document, the macro interrupts to ask if I want to add or use the existing styles.

Does anyone know if there is a better way to use the paste command, than I have so far figured out, so that all the attributes of the original selection are always pasted into the X file and without being asked (or the macro interrupted)?
You'll need to use the Document object for the "X-" document to do the pasting. Oh, but actually, you'll need to bypass copy-paste, so you can use the "insert" command. Here's the snippet:

Code: Select all

$text = Read Selection # copy current document's selected text
Menu ':Edit:Delete'

Open $xpath
$xdoc = Document.active
Select Document End # move to end of x-document
$xdoc.insertText( $text, 'm' ) # insert text
The 'm' option ensures that styles with the same name are always merged (you can read about other style conflict resolution options in the Macro Language Reference).
Also, is there anyway to ensure that the pasting will always be appended to the end of the document
Probably you noticed, but the "Select Document End" command will accomplish what you need.
js
Posts: 259
Joined: 2007-04-12 14:59:36

Re: Switching to a Specified Document . . .

Post by js »

Lets say I work on an active document "myDocument" and I would like to put the scrappings into a new document (either to be called or to be newly created) with path "myDocumentScappings", in the same folder?

I find it a bit difficult, because calling the name of the active document comes with an ending, like .rtf. So I cannot simply append the string "Scrappings".
Kino
Posts: 400
Joined: 2008-05-17 04:02:32

Re: Switching to a Specified Document . . .

Post by Kino »

You can get the path of "myDocumentScappings.rtf" by . . .

Code: Select all

$path = '~/Documents/XXX/myDocument.rtf'
$scrapPath = $path.copy
$scrapPath.findAndReplace '(?=\.[^.]+$)', 'Scrappings', 'E'
exit $scrapPath # returns "~/Documents/XXX/myDocumentScrappings.rtf"
User avatar
martin
Official Nisus Person
Posts: 5227
Joined: 2002-07-11 17:14:10
Location: San Diego, CA
Contact:

Re: Switching to a Specified Document . . .

Post by martin »

I was going to say that while Kino's find/replace expression works, you might be more comfortable working with NWP's explicit file path commands. But then I wrote up the equivalent code and it's much more tedious than I thought:

Code: Select all

$path = '~/Documents/XXX/myDocument.rtf'
$fileExt = $path.filePathExtension

$scrapName = $path.lastFilePathComponentWithoutExtension
$scrapName &= 'Scrappings'
$scrapName &= $fileExt

$scrapPath = $path.filePathByRemovingLastComponent
$scrapPath = $scrapPath.filePathByAppendingComponent( $scrapName )
Prompt $scrapPath
I'll see about adding a macro command to do all that in one line of code 8)
js
Posts: 259
Joined: 2007-04-12 14:59:36

Re: Switching to a Specified Document . . .

Post by js »

Good to have more than one solution for learning the language. thank you both

In fact I had been looking for something a bit more complicated:
That the macro checks whether there is already a file with the desired new ending (like " ... myfile.scraps") in the same folder, and if not, creates one.
User avatar
martin
Official Nisus Person
Posts: 5227
Joined: 2002-07-11 17:14:10
Location: San Diego, CA
Contact:

Re: Switching to a Specified Document . . .

Post by martin »

Here's a snippet that either opens the file at $path, or saves and opens a new empty file at $path:

Code: Select all

$path = '~/Desktop/whatever.rtf'
If File.existsAtPath($path)
	Open $path
Else
	New
	Save As $path
End
Post Reply