Reply to topic  [ 5 posts ] 
Fast closing nameless window 
Author Message

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


2009-02-10 06:40:50
Profile

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


2009-02-10 10:25:37
Profile
Official Nisus Person
User avatar

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


2009-02-10 14:34:08
Profile WWW

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


2009-02-11 01:36:00
Profile

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


2009-07-14 10:35:56
Profile
Display posts from previous:  Sort by  
Reply to topic   [ 5 posts ] 

Who is online

Users browsing this forum: No registered users 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