Reply to topic  [ 10 posts ] 
check active doc 
Author Message

Joined: 2007-04-12 14:59:36
Posts: 229
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:
$docPath =  "~/Documents/mydoc"
$actDoc = Document.active
$actDocPath = $actDoc.filePath
if ! $actDocPath =  $docPath
   Document.open $docPath
else
   do suchandsuch
end


2015-04-06 04:25:51
Profile
User avatar

Joined: 2007-02-07 00:58:12
Posts: 876
Location: Japan
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:
if ! $a == $b

you could write
Code:
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:
if $actDocPath != "/Users/myUser/Documents/mydoc.rtf"

If you want to use the abbreviate path you can use the .abbreviatedPath command like this:
Code:
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


2015-04-06 17:15:58
Profile

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

Quote:
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?


2015-04-08 03:41:14
Profile
User avatar

Joined: 2007-02-07 00:58:12
Posts: 876
Location: Japan
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:
Quote:
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:
$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


2015-04-09 02:31:35
Profile

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


2015-04-09 07:55:10
Profile
Official Nisus Person
User avatar

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

Quote:
$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:
$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.


2015-04-09 17:01:36
Profile WWW
User avatar

Joined: 2007-02-07 00:58:12
Posts: 876
Location: Japan
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:
$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:
$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:
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


2015-04-09 21:09:33
Profile

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


2015-04-10 03:47:15
Profile
Official Nisus Person
User avatar

Joined: 2002-07-11 17:14:10
Posts: 4251
Location: San Diego, CA
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:
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:
File.isEquivalentPaths($a, $b)

It handles all the business of tildes and whatnot.


2015-04-10 19:26:38
Profile WWW
Official Nisus Person
User avatar

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


2015-04-10 19:27:30
Profile WWW
Display posts from previous:  Sort by  
Reply to topic   [ 10 posts ] 

Who is online

Users browsing this forum: No registered users and 2 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:  
cron
Powered by phpBB® Forum Software © phpBB Group
Designed by ST Software