Page 1 of 1

check active doc

Posted: 2015-04-06 04:25:51
by js
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

Re: check active doc

Posted: 2015-04-06 17:15:58
by phspaelti
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.

Re: check active doc

Posted: 2015-04-08 03:41:14
by js
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?

Re: check active doc

Posted: 2015-04-09 02:31:35
by phspaelti
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.

Re: check active doc

Posted: 2015-04-09 07:55:10
by js
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.

Re: check active doc

Posted: 2015-04-09 17:01:36
by martin
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.

Re: check active doc

Posted: 2015-04-09 21:09:33
by phspaelti
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?

Re: check active doc

Posted: 2015-04-10 03:47:15
by js
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.

Re: check active doc

Posted: 2015-04-10 19:26:38
by martin
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.

Re: check active doc

Posted: 2015-04-10 19:27:30
by martin
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.