nisus.com
https://www.nisus.com/forum/

Fast closing nameless window
https://www.nisus.com/forum/viewtopic.php?f=17&t=3327
Page 1 of 1

Author:  js [ 2009-02-10 06:40:50 ]
Post subject:  Fast closing nameless window

I often open a new document to paste some text into it. Later I would like to close the window quickly without being asked any questions. I thought this could be done with a macro like this (checking first if the window has a name. In this case I don‘t want the macro to execute to not inadvertently delete the document):

Code:
$x = Document.active
if $x = "document <untitled>"
   Select All
   Delete
   Close
else
   exit
end



It seems that the condition is not understood. What went wrong?

Author:  Kino [ 2009-02-10 10:25:37 ]
Post subject:  Re: Fast closing nameless window

js wrote:
What went wrong?

I don't know but the following seems to work.
Code:
 $docs = Document.openDocuments
foreach $doc in $docs
   $path = $doc.filePath
   if $path == undefined  # i.e. if it is an unsaved document not having a path
      Document.setActive $doc
      Menu ':File:Close'
      if $doc.hasChanges
         Press Button "Don't Save"
      end
   end
end

Author:  martin [ 2009-02-10 14:34:08 ]
Post subject:  Re: Fast closing nameless window

There are a couple of problems with the original code you've posted:
js wrote:
Code:
$x = Document.active
if $x = "document <untitled>"

First "=" is an assignment, not a comparison. So really that "if" statement will always be true. Also, the capitalization and spacing of the string is not correct. What you meant was:
Code:
$x = Document.active
if $x == "Document<Untitled>"

This will also fail however, because a Document object is not a string, and will never equal one. If you really wanted to get it to work, you'd need to do:
Code:
$doc = Document.active
$doc = Cast to String $doc
If $doc == "Document<Untitled>"

But really this is the wrong way to approach the problem. A full proof way to check if the document has been saved is to use the "filePath" property as Kino did:
Code:
$doc = Document.active
If $doc.filePath == undefined

So, your macro becomes:
Code:
$doc = Document.active
If $doc.filePath == undefined
   Select All
   Delete
   Close
End

However, I might recommend this instead, which is more complete and faster:
Code:
$doc = Document.active
If $doc.filePath == undefined
   $doc.clearUndoHistory
   Close
End

Author:  js [ 2009-02-11 01:36:00 ]
Post subject:  Re: Fast closing nameless window

Thank you very much, Kino and Martin. I am glad to say that your help offered much more than solving my little problem. It's such a good way to get acquainted with that fabulous macro language without making a fulltime job of learning it.

Author:  Kino [ 2009-07-14 10:35:56 ]
Post subject:  Re: Fast closing nameless window

As you would have already noticed, now you can make this kind of macros simpler by using close command newly available in Nisus Writer Pro 1.3.

Close the frontmost document ignoring your changes:
Code:
$doc = Document.active
if $doc != undefined  # i.e. unless there is no open document...
   $doc.close true
end

Close the unsaved new frontmost document ignoring your changes:
Code:
$doc = Document.active
if $doc != undefined  # i.e. unless there is no open document...
   if $doc.filePath == undefined
      $doc.close true
   end
end

Delete true if you want to be asked about the destiny of your changes, i.e. what you have typed, pasted &c. and has never been saved.

Close all new and unsaved document windows:
Code:
$docs = Document.openDocuments
foreach $doc in $docs
   if $doc.filePath == undefined
      $doc.close true
   end
end

Page 1 of 1 All times are UTC - 8 hours
Powered by phpBB® Forum Software © phpBB Group
http://www.phpbb.com/