js wrote:But if no document at all is open Nisus complains that the filepath property requires an object, but get‘s „undefined“ .
You see that error message because (as you say) there's no document open. Without a document object, you cannot access its "filePath" property.
The solution is to check if a document is open before trying to get its file path, eg:
Code: Select all
$actDoc = Document.active
If $actDoc
$actDocPath = $actDoc.filePath
...
End
You could omit the temporary path variable, resulting in a macro like this:
Code: Select all
$docPath = "~/Desktop/test.rtf"
$actDoc = Document.active
If ! $actDoc
Document.open $docPath
ElseIf $actDoc.filePath != $docPath
Document.open $docPath
End
Of course, as the (somewhat gruesome) saying goes, there's more than one way to skin a cat. Let's consider your other question:
My if-construction is meant to say: (If A) OR (if B) then do C. Is there another way to this?
Hrm, I was going to say that you could do this, but there's a problem. While you can use an OR operator to include two conditions in a single "if" statement, you run into the same problem with the undefined Document object. For example, this works correctly:
Code: Select all
$number = 999
If ($number < 1) || ($number > 100)
Prompt "The number $number is not from 1-100."
End
However, in your case this does NOT work properly:
Code: Select all
$docPath = "~/Desktop/test.rtf"
$actDoc = Document.active
If (! $actDoc) || ($actDoc.filePath != $docPath) # FAILS: possible undefined object
Document.open $docPath
End
The problem is that the || OR operator isn't doing something that's called
short-circuit evaluation. This is a common feature of programming language's logical operators, where subsequent expressions are only evaluated as required based on the value of earlier expressions. I found the lack of this feature in the Nisus macro language somewhat surprising, but I see that the macro reference doesn't make any claims to support it. I'll have to file an enhancement.
If you really want a single code path (a good programming habit) for opening the desired document, you can do so if you contort your code a little:
Code: Select all
$docPath = "~/Desktop/test.rtf"
$actDoc = Document.active
$actDocPath = ""
If $actDoc
$actDocPath = $actDoc.filePath
End
If $actDocPath != $docPath
Document.open $docPath
End
I hope that helps.