Fast closing nameless window

Get help using and writing Nisus Writer Pro macros.
Post Reply
js
Posts: 259
Joined: 2007-04-12 14:59:36

Fast closing nameless window

Post by js »

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: Select all

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

Re: Fast closing nameless window

Post by Kino »

js wrote:What went wrong?
I don't know but the following seems to work.

Code: Select all

 $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
User avatar
martin
Official Nisus Person
Posts: 5227
Joined: 2002-07-11 17:14:10
Location: San Diego, CA
Contact:

Re: Fast closing nameless window

Post by martin »

There are a couple of problems with the original code you've posted:
js wrote:

Code: Select all

$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: Select all

$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: Select all

$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: Select all

$doc = Document.active
If $doc.filePath == undefined
So, your macro becomes:

Code: Select all

$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: Select all

$doc = Document.active
If $doc.filePath == undefined
	$doc.clearUndoHistory
	Close
End
js
Posts: 259
Joined: 2007-04-12 14:59:36

Re: Fast closing nameless window

Post by js »

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

Re: Fast closing nameless window

Post by Kino »

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: Select all

$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: Select all

$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: Select all

$docs = Document.openDocuments
foreach $doc in $docs
	if $doc.filePath == undefined
		$doc.close true
	end
end
Post Reply