Can't find the path!

94 views
Skip to first unread message

Israel Hands

unread,
Apr 5, 2021, 6:18:41 AM4/5/21
to leo-editor
I have an @path node pointing to a directory on a disk. 

I want to make a new sub directory which the Path plugin allows me to do.
But when I save the outline the log file warns me-

Path will be created if a file is saved on it

So the OS hasn't at this actually created the directory. If I make a subnode of this directory in the hope it will create a file it doesn't seem to do anything. And making an @auto node throws up an error message.

I'm I missing a way of creating a directory and populating it with files from within Leo?

ta

IH


Edward K. Ream

unread,
Apr 5, 2021, 6:56:23 AM4/5/21
to leo-editor
On Mon, Apr 5, 2021 at 5:18 AM Israel Hands <alis...@mcgh.ee> wrote:

[Am] I missing a way of creating a directory and populating it with files from within Leo?

Have you tried this?

@bool create-nonexistent-directories = True

Edward

jkn

unread,
Apr 5, 2021, 11:19:35 AM4/5/21
to leo-editor
ooh, I didn't know about that setting - thanks!

J^n

Edward K. Ream

unread,
Apr 5, 2021, 4:03:24 PM4/5/21
to leo-editor
On Mon, Apr 5, 2021 at 10:19 AM jkn <jkn...@nicorp.f9.co.uk> wrote:
ooh, I didn't know about that setting - thanks!

You're welcome.

Edward

Israel Hands

unread,
Apr 6, 2021, 5:28:36 AM4/6/21
to leo-editor
Thanks Edward - I have added the line about create non existing directories to the mysettings files.

Here's the current behaviour...

I have an @path node representing a directory

1 I right click on the @path node and select active-path-make-dir from the path option. Up comes the name directory dialogue.

2 I type a name for the directory and a sub-node appears beneath the @path node. Named in the style /New-Directory/

3 I save the outline - but my new directory is not created at this point - I get a note in the log file saying - Path will be created if a file is saved on it
I only get this warning once further saves are silent on the matter.

4 I create a node - Active_Path_Test  - under the /New-Directory/ node and save the outline - no OS action as yet!

5 I right click on my new node and select path/active-path-act-on-node   - no dice - log warning 'Create parent directories first.'

6 I rename node @auto Active_Path_Test - and save the outline - at this point file and directory are created.

7 I create a new file in the directory from the OS side.

8 Right click on the /New-Directory/ node and select  path/active-path-act-on-node and my OS created file appears as a node - empty at the moment

9 I right click on my OS-File node and select  path/active-path-act-on-node and the content of the file appears.


Sorry for the long winded 'path' to here but I guess what is confusing me is that nodes which create files on the Leo side have to need to have a an '@auto' prefix whereas the glory of Active Path is that files grabbed from the OS don't - is that expected behaviour? 

Thanks for all your patience.

ta

IH





tbp1...@gmail.com

unread,
Apr 6, 2021, 8:15:52 AM4/6/21
to leo-editor
Here's what I have been doing to create files in directory trees.  It seems much simpler.

1. Create a node with the headline @path d:\temp\aaa
2. Create a path with the headline @path bbb. Indent it right, under the aaa one.
3. ... and so on
4. However far down in the tree you like, create the file(s) you want to have there.  Say its node is @file test.txt.
5. Save the outline.

After saving, the whole directory tree with the file(s) will be there:
d:\temp
   - aaa
      - bbb
         ....
            test.txt

There is no need to fuss with active-path-do-something, etc.

BTW, I have put a button into MyLeoSettings.leo that will open a file manager window (File Explorer on Windows) at the current directory of the selected node, whatever it is.  This has been awfully handy.  The command is in an @button node in the @settings tree:

@button Show Current Dir
    # Body text
    from subprocess import run
    cmd = ['explorer.exe', '.']
    run(cmd)

This is for Windows.  The details would be different for Linux. This does the same job on my Linux Mint VM, where the file manager is called nemo:

from subprocess import run
cmd = ['nemo', '.']
run (cmd)


Edward K. Ream

unread,
Apr 6, 2021, 10:14:19 AM4/6/21
to leo-editor
On Tue, Apr 6, 2021 at 7:15 AM tbp1...@gmail.com <tbp1...@gmail.com> wrote:

Here's what I have been doing to create files in directory trees.  It seems much simpler.

Thanks for this tip.  I don't create many directories myself, and so I just create the directories first in the file manager.

BTW, I have put a button into MyLeoSettings.leo that will open a file manager window (File Explorer on Windows) at the current directory of the selected node, whatever it is.  This has been awfully handy. 

Great idea.  However, I don't understand how '.' gets bound to the directory of the selected node. Here is tested code for Windows that works as I expect.

import os
from subprocess import run
path = g.fullPath(c, p) or os.path.abspath('.')

cmd = ['explorer.exe', '.']
run(cmd)

What am I missing?

Edward

Jacob Peck

unread,
Apr 6, 2021, 11:28:10 AM4/6/21
to Leo-editor Group
I believe '.' is interpreted in the context of the OS -- which on both Linux and Windows evaluates to the same as the current working directory.

I am unsure how or why the 'run(cmd)' portion is executed in the context of the current node's path, but if it is, '.' should expand at the OS level to the full current path.

--
You received this message because you are subscribed to the Google Groups "leo-editor" group.
To unsubscribe from this group and stop receiving emails from it, send an email to leo-editor+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/leo-editor/CAMF8tS11Vw%3DbB95eyQhXp%2B1FfFAQVxC1zhxH75CfjPzp-4YqGw%40mail.gmail.com.

tbp1...@gmail.com

unread,
Apr 6, 2021, 11:29:50 AM4/6/21
to leo-editor
On Tuesday, April 6, 2021 at 10:14:19 AM UTC-4 Edward K. Ream wrote:
On Tue, Apr 6, 2021 at 7:15 AM tbp1...@gmail.com <tbp1...@gmail.com> wrote:

BTW, I have put a button into MyLeoSettings.leo that will open a file manager window (File Explorer on Windows) at the current directory of the selected node, whatever it is.  This has been awfully handy. 

Great idea.  However, I don't understand how '.' gets bound to the directory of the selected node. Here is tested code for Windows that works as I expect.

I don't understand it either, but it works as I showed on both Linux and Windows.  Sure makes for simple code!  These commands have worked this way for a long time (years).  A great undocumented feature!

You can even copy the path of a selected node to the clipboard, which is sometimes useful:

import os, pyperclip
pyperclip.copy(os.getcwd()) # works on both Windows and Linux

pyperclip gives access to the system clipboard. You can install it using pip.

jkn

unread,
Apr 6, 2021, 1:23:35 PM4/6/21
to leo-editor

Neat! I will try this.

BTW, I think 'xdg-open' as a command should do this in a window-manager-agnostic way under (most?) linuxes. I'll give it a try...

tbp1...@gmail.com

unread,
Apr 6, 2021, 1:49:32 PM4/6/21
to leo-editor
It worked on two VMs I tried (Mint and Debian - I only have Debian-derived VMs at the moment).  Glad to learn about xdg-open, thanks.

Edward K. Ream

unread,
Apr 6, 2021, 4:14:46 PM4/6/21
to leo-editor
On Tue, Apr 6, 2021 at 10:29 AM tbp1...@gmail.com <tbp1...@gmail.com> wrote:


On Tuesday, April 6, 2021 at 10:14:19 AM UTC-4 Edward K. Ream wrote:
On Tue, Apr 6, 2021 at 7:15 AM tbp1...@gmail.com <tbp1...@gmail.com> wrote:

BTW, I have put a button into MyLeoSettings.leo that will open a file manager window (File Explorer on Windows) at the current directory of the selected node, whatever it is.  This has been awfully handy. 

Great idea.  However, I don't understand how '.' gets bound to the directory of the selected node. Here is tested code for Windows that works as I expect.

I don't understand it either, but it works as I showed on both Linux and Windows.  Sure makes for simple code!  These commands have worked this way for a long time (years).  A great undocumented feature!

I strongly suspect that the code works by accident because loading a .leo file effectively changes ".".  But outlines like LeoPyRef.leo contains files in several directories. There is no way the OS can know how to relate '.' to the various directories.

In short, I would recommend my code ;-)

Edward

tbp1...@gmail.com

unread,
Apr 6, 2021, 5:16:54 PM4/6/21
to leo-editor
Well, it's more than that.  Using "." works for @path subtrees that are not even on the same drive (that is, Windows drive) as the leo outline they are in.  It's not just that "." sends you to the directory, say for the workbook in %USERPROFILE%\.leo (Windows).  In the workbook outline at that location, for any of the following nodes begin selected, the command(s) go to the right directory:

- head
- @path d:\temp
    @path dir_1
       @path dir_2
            @clean test.txt

tbp1...@gmail.com

unread,
Apr 6, 2021, 5:18:14 PM4/6/21
to leo-editor
But I suppose it would be better to use a more comprehensive method in case you accidentally delete this "feature" in the future.

tbp1...@gmail.com

unread,
Apr 6, 2021, 5:58:43 PM4/6/21
to leo-editor
But in your snippet, you also use "." without (apparently) changing it.

import os
from subprocess import run
path = g.fullPath(c, p) or os.path.abspath('.')

cmd = ['explorer.exe', '.']
run(cmd)

How is that supposed to work?  Wouldn't you want to use path instead of "."?

tbp1...@gmail.com

unread,
Apr 6, 2021, 10:40:58 PM4/6/21
to leo-editor
OK, after some more testing and thought:

1. Edward's example can't work as claimed, even if '.' is replaced by path.  The expression for path  returns a path that a) has forward slashes and won't work in Windows when you open explorer on it - confirmed by test with both a console and in Leo - and 2) If the node contains an external file, the path is to that file and so Explorer wouldn't get opened on its directory anyway.  Instead, the system's default program for that file type will be invoked by Explorer.
2. By actual test, I get the same result whether I comment path out or not.
3. If I do replace "." with path, explorer opens but not at the intended directory because of the forward slashes in path.

So to make it work in Windows, we have to 1) fix the path separators, and 2) test whether the path is to a file or directory, and if it's a file then find its parent directory.

tbp1...@gmail.com

unread,
Apr 6, 2021, 10:51:34 PM4/6/21
to leo-editor
Maybe this is beating the a dead horse, but os.getcwd() returns the actual directory of the selected node - verified by test.  It is as if when a node is selected, Leo changes the current directory to the path for that node. That would explain why "." works as well.

I don't know if this behavior was intended, but it makes some things convenient.  For example if you have a file or script that wants to save something - maybe an image file it generated - then it will go into the @path directory for that node instead of into Leo's home directory (or wherever the outline exists).  I now realize that I have made use of that behavior without realizing that it might have been different.

Edward K. Ream

unread,
Apr 7, 2021, 10:48:52 AM4/7/21
to leo-editor
On Tue, Apr 6, 2021 at 4:58 PM tbp1...@gmail.com <tbp1...@gmail.com> wrote:
But in your snippet, you also use "." without (apparently) changing it.

Oops.

Edward

Edward K. Ream

unread,
Apr 7, 2021, 10:50:46 AM4/7/21
to leo-editor
On Tue, Apr 6, 2021 at 9:51 PM tbp1...@gmail.com <tbp1...@gmail.com> wrote:
Maybe this is beating the a dead horse, but os.getcwd() returns the actual directory of the selected node - verified by test.  It is as if when a node is selected, Leo changes the current directory to the path for that node. That would explain why "." works as well.

Hmm. Thanks for all your thorough comments. The takeaway: a command that opens the directory of c.p is useful.

Edward

Edward K. Ream

unread,
Apr 7, 2021, 10:58:24 AM4/7/21
to leo-editor
On Tuesday, April 6, 2021 at 9:51:34 PM UTC-5 tbp1...@gmail.com wrote:
Maybe this is beating the a dead horse, but os.getcwd() returns the actual directory of the selected node - verified by test.  It is as if when a node is selected, Leo changes the current directory to the path for that node. That would explain why "." works as well.

Here is my latest suggestion, incorporating your suggestions. It's only advantage, apparently, is that it is more explicit:

import os
from subprocess import run
path = os.path.normpath(g.fullPath(c, p) or os.getcwd())
cmd = ['explorer.exe', path]
run(cmd)

Tested only on Windows.  The call to normpath should allow it to work everywhere.

Edward

tbp1...@gmail.com

unread,
Apr 7, 2021, 2:51:01 PM4/7/21
to leo-editor
Yes, normpath uses backslashes on Windows.  But it only returns something for a file, not for a node that is an @path node and not a file.  For opening a directory, that's not quite what we want, and we still need the os.getcwd() to get the non-file directory paths anyway.  So we might as well just stick with getcwd() (or ".", which is the same thing).

Here is the simplest code I have been able to devise that does exactly what I want - getting the directory of a node -  if you want to avoid using "."

import os
nodepath = os.path.normpath(g.fullPath(c, p))
path = os.path.abspath(nodepath)
# can verify with g.es(path)

g.fullPath(c, p) returns nothing if the argument is a directory. Calling os.path.normpath on that empty string produces "."  You can use abspath() to turn "." into a full path expression, but it was originally "." anyway.

OTOH, if it should happen in the future that the when a node is selected the current directory no longer gets set to its path, the above path expression should still work.  It would also work for some deviant OS that doesn't use "." for the working directory.

If you want to get the file path if the node is an external file node, or the directory path if it's not, this will do the job:

os.path.abspath(g.fullPath(c, p))

Edward K. Ream

unread,
Apr 7, 2021, 5:20:27 PM4/7/21
to leo-editor
On Wed, Apr 7, 2021 at 1:51 PM tbp1...@gmail.com <tbp1...@gmail.com> wrote:
Here is the simplest code I have been able to devise that does exactly what I want - getting the directory of a node -  if you want to avoid using "."

Thanks. Looks good to me.

Edward
Reply all
Reply to author
Forward
0 new messages