check active doc

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

check active doc

Post by js » 2015-04-06 04:25:51

I would like to check if the actually open doc is "~/Documents/mydoc". And if not do suchandsuch.
Unfortunately I don‘t know how to solve this simple task. What is wrong with:

Code: Select all

$docPath =  "~/Documents/mydoc"
$actDoc = Document.active
$actDocPath = $actDoc.filePath
if ! $actDocPath =  $docPath
	Document.open $docPath
else
	do suchandsuch
end

User avatar
phspaelti
Posts: 912
Joined: 2007-02-07 00:58:12
Location: Japan

Re: check active doc

Post by phspaelti » 2015-04-06 17:15:58

For a start, you want to remember that the comparison operator is '=='. A single '=' is an assignment. An assignment will always be true.
On this topic you may also want to note that instead of writing

Code: Select all

if ! $a == $b
you could write

Code: Select all

if $a != $b
That is the '!=' is the 'not equal' operator.

Apart from this there will be several issues. The .filePath property always returns the full path, i.e., the one that starts '/Users/…'. File paths aren't really objects, they are just text. So you need to make sure that they have the exact same form for the comparison to work. So you would have to compare the full path, and you will also need to use the file extension with the file name. So your comparison would have to be:

Code: Select all

if $actDocPath != "/Users/myUser/Documents/mydoc.rtf"
If you want to use the abbreviate path you can use the .abbreviatedPath command like this:

Code: Select all

if $actDocPath.abbreviatedPath != "~/Documents/mydoc.rtf"
There are other file path commands to let you isolate other parts of the file path. Check the Macro Reference for the full details.
philip

js
Posts: 229
Joined: 2007-04-12 14:59:36

Re: check active doc

Post by js » 2015-04-08 03:41:14

Thank you Philip

I tried to consider what you kindly explained. But now if the macro attempts to open myDoc to compare it with the active doc, Nisus answers with a message I don't understand:
Could not access the file "Users/home/Documents/myDoc" because _the file does not exist: ~/Library/Containers/com.nisus.NisusWriter/Data/Users/home/Documents/myDoc [NSCocoaErrorDomain - code 260].
As if myDoc in the Documents folder was supposed to have a parallel life in the Library Containers of Nisus Writers. Maybe somebody from Nisus could help?

User avatar
phspaelti
Posts: 912
Joined: 2007-02-07 00:58:12
Location: Japan

Re: check active doc

Post by phspaelti » 2015-04-09 02:31:35

js wrote:I tried to consider what you kindly explained. But now if the macro attempts to open myDoc to compare it with the active doc, Nisus answers with a message I don't understand:
Could not access the file "Users/home/Documents/myDoc" because _the file does not exist: ~/Library/Containers/com.nisus.NisusWriter/Data/Users/home/Documents/myDoc [NSCocoaErrorDomain - code 260].
As if myDoc in the Documents folder was supposed to have a parallel life in the Library Containers of Nisus Writers. Maybe somebody from Nisus could help?
The path you have there looks impressive indeed. You seem to have stuck two paths together. The path "~/Library/Containers/com.nisus.NisusWriter/Data/" + the path you actually want. You should post the code here that you actually wrote, otherwise it will be impossible to diagnose what went wrong.

Here is how I would have (re-)written your original code:

Code: Select all

$docPath =  "~/Documents/mydoc.rtf"
$actDoc = Document.active
if $actDoc.filePath.abbreviatedPath !=  $docPath
   Document.open $docPath
else
   #do suchandsuch
end
Note that I added the extension ".rtf" to $docPath.

Also one question about your code:
Why are you using Document.open? You can just use Open. Document.open will be convenient if you want to 'catch' the document object. Otherwise you don't need it.
philip

js
Posts: 229
Joined: 2007-04-12 14:59:36

Re: check active doc

Post by js » 2015-04-09 07:55:10

Thanks for taking time to help Philip.
What I actually wrote (here replacing actual home and actual doc name for “home” and “myDoc”) was this:

Code: Select all

$docPath =  "User/home/Documents/myDoc.rtf"
$actDoc = Document.active
$actDocPath = $actDoc.filePath

if $actDocPath !=  $docPath
	Document.open $docPath
else
	exit # to be replaced by an action
end
This produced that strange message from Nisus. But now, to my surprise, the macro works fine: all I did was replacing „User/home" by the abbreviated form. Why should that work? My macro (unlike yours) does not demand ".abbreviatedPath"

Anyway. I don't get it. But I thought I told you, in case you see my mistake.

User avatar
martin
Official Nisus Person
Posts: 4261
Joined: 2002-07-11 17:14:10
Location: San Diego, CA
Contact:

Re: check active doc

Post by martin » 2015-04-09 17:01:36

js wrote:As if myDoc in the Documents folder was supposed to have a parallel life in the Library Containers of Nisus Writers. Maybe somebody from Nisus could help?
Your musings about a double life are not actually far from the truth. As part of OSX's sandboxing implementation, there is a whole folder hierarchy inside NWP's sandbox that mirrors your user's home folder. I don't know exactly when or why OSX will add and remove links from this shadow hierarchy, but I suspect it's to persist access to external data as necessary.
$docPath = "User/home/Documents/myDoc.rtf"
...
This produced that strange message from Nisus. But now, to my surprise, the macro works fine: all I did was replacing „User/home" by the abbreviated form
The reason your path produced an error is because that path will never exist. Absolute paths must begin with an initial slash, and the folder is "Users" not "User". So either of these should have worked:

Code: Select all

$docPath =  "/Users/home/Documents/myDoc.rtf"
$docPath =  "~/Documents/myDoc.rtf"
The path you tried to use, with no initial slash or tilde is a path relative to the current working directory. In the context of NWP the working directory is its sandbox container.

I hope that's all clear. Please let me know if you have any questions.

User avatar
phspaelti
Posts: 912
Joined: 2007-02-07 00:58:12
Location: Japan

Re: check active doc

Post by phspaelti » 2015-04-09 21:09:33

Well, now we understand why your updated macro didn't work. And I learned something too. I wrongly claimed earlier that a comparison between file paths would simply compare text strings. Apparently NWP is a bit smarter than that and tries to interpret abbreviated paths and relative paths too, as long as one of the two is a file path object.

But that still leaves a mystery about why your original code didn't work right. Just to quote it again:

Code: Select all

$docPath =  "~/Documents/mydoc"
$actDoc = Document.active
$actDocPath = $actDoc.filePath
if ! $actDocPath =  $docPath
   Document.open $docPath
else
   do suchandsuch
end
Now as I mentioned earlier the extension is necessary, and the comparison operator has to be '==' not '='. So I update the code to:

Code: Select all

$docPath =  "~/Documents/mydoc.rtf"
$actDoc = Document.active
$actDocPath = $actDoc.filePath
if ! $actDocPath == $docPath
   Document.open $docPath
else
   do suchandsuch
end
And this still doesn't work!
If I change the comparison by dropping the '!' and using '!=' instead, it works fine. But what I don't understand is that if the

Code: Select all

if ! $a == $b
syntax is bad, why doesn't it throw an error? Perhaps this is a 'two-operator' violation, and NWP is evaluating the '!' before the '==', but in that case it should throw an error. Also NWP doesn't accept parentheses to disambiguate such expressions.
So, Martin, is this a bug?
philip

js
Posts: 229
Joined: 2007-04-12 14:59:36

Re: check active doc

Post by js » 2015-04-10 03:47:15

This is very helpful Martin. "User" instead of users" was just a slip in my mail for this list. But I did not realize that the abbreviation sign ~ also includes a starting slash.
That the sandbox has a mirror of the users file structure seems surprising. So these mirror structures should exist for every single application that is sandboxed.

User avatar
martin
Official Nisus Person
Posts: 4261
Joined: 2002-07-11 17:14:10
Location: San Diego, CA
Contact:

Re: check active doc

Post by martin » 2015-04-10 19:26:38

phspaelti wrote:If I change the comparison by dropping the '!' and using '!=' instead, it works fine. But what I don't understand is that if the

Code: Select all

if ! $a == $b
syntax is bad, why doesn't it throw an error? Perhaps this is a 'two-operator' violation, and NWP is evaluating the '!' before the '==', but in that case it should throw an error. Also NWP doesn't accept parentheses to disambiguate such expressions.
So, Martin, is this a bug?
This is a bug, sorry. But it's just been fixed in a newer public beta:

http://nisus.com/files/pro/NisusWriterPro-v211-pb4.zip

That beta also includes a convenient new file path comparison command:

Code: Select all

File.isEquivalentPaths($a, $b)
It handles all the business of tildes and whatnot.

User avatar
martin
Official Nisus Person
Posts: 4261
Joined: 2002-07-11 17:14:10
Location: San Diego, CA
Contact:

Re: check active doc

Post by martin » 2015-04-10 19:27:30

js wrote:That the sandbox has a mirror of the users file structure seems surprising. So these mirror structures should exist for every single application that is sandboxed.
That is true. Every app's sandbox will have a partial mirror of the user's home folder. However, don't worry about sucking up disk space, since they're all links of some kind, not file copies.

Post Reply