Reply to topic  [ 9 posts ] 
Switching to a Specified Document . . . 
Author Message

Joined: 2010-02-22 15:06:48
Posts: 98
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:
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


2010-05-31 07:56:47
Profile

Joined: 2008-05-17 04:02:32
Posts: 400
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:
$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.


2010-05-31 09:11:43
Profile

Joined: 2010-02-22 15:06:48
Posts: 98
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:
#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


2010-05-31 12:02:51
Profile
Official Nisus Person
User avatar

Joined: 2002-07-11 17:14:10
Posts: 4251
Location: San Diego, CA
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:
$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).

Quote:
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.


2010-05-31 20:33:13
Profile WWW

Joined: 2007-04-12 14:59:36
Posts: 229
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".


2010-06-01 05:35:01
Profile

Joined: 2008-05-17 04:02:32
Posts: 400
You can get the path of "myDocumentScappings.rtf" by . . .
Code:
$path = '~/Documents/XXX/myDocument.rtf'
$scrapPath = $path.copy
$scrapPath.findAndReplace '(?=\.[^.]+$)', 'Scrappings', 'E'
exit $scrapPath # returns "~/Documents/XXX/myDocumentScrappings.rtf"


2010-06-01 06:36:55
Profile
Official Nisus Person
User avatar

Joined: 2002-07-11 17:14:10
Posts: 4251
Location: San Diego, CA
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:
$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)


2010-06-01 11:04:30
Profile WWW

Joined: 2007-04-12 14:59:36
Posts: 229
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.


2010-06-03 17:11:33
Profile
Official Nisus Person
User avatar

Joined: 2002-07-11 17:14:10
Posts: 4251
Location: San Diego, CA
Here's a snippet that either opens the file at $path, or saves and opens a new empty file at $path:
Code:
$path = '~/Desktop/whatever.rtf'
If File.existsAtPath($path)
   Open $path
Else
   New
   Save As $path
End


2010-06-03 21:51:35
Profile WWW
Display posts from previous:  Sort by  
Reply to topic   [ 9 posts ] 

Who is online

Users browsing this forum: Bing [Bot] and 3 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB® Forum Software © phpBB Group
Designed by ST Software