Path to user directory without username

OK, I didn’t have the right number of dots (periods)… "..\TestFolder\MyScript.py" works with two or more dots, I only had one.

I guess don’t understand what the dots represent actually. Seems to work with more dots than you need, but not less. And the “path” to the ...\Scripts folder in Windows - does that depend on having set a search path to that location in the Python editor beforehand, or is it “hard-coded” into the basic Rhino install?

OTOH, still not having any luck on the Mac side.

Check out paragraph (section) 1.4 here:

http://www.ee.surrey.ac.uk/Teaching/Unix/unix1.html

This is ancient history stuff from unix, thence MS-DOS, thence Windows command line, and practically every other OS.

‘.’ represents the current directory
’…’ represents the previous directory

So the path for
C:\Users\username\_tst\hello.py

that’s relative to
C:\Users\username\AppData\Roaming\McNeel\Rhinoceros\5.0\scripts

is
!_-RunPythonScript "..\..\..\..\_tst\hello.py"

This, of course, means one step up in the directory hierarchy, not “previously used” in a time sense.

on the mac the path for
~/ac/_tmp/hello.py

that’s relative to
~/Library/Application Support/McNeel/Rhinoceros/scripts

is
"../../../../../ac/_tmp/hello.py"

you may have problems on the mac if your file or directory names have spaces.

Thanks for the informative lesson guys! --Mitch

Still can’t get this to work on Mac. There’s no way to know where the current directory is, it could be anywhere. I need an alias in the form of an absolute path from the user’s home directory to the file in the scripts folder.

–Mitch

I don’t know of a way to expand the home directory var like you want to.

But I’m not sure why you can’t get it to work with a relative path because the path is relative to a directory that’s on the path: ~/Library/Application Support/McNeel/Rhinoceros/scripts which is static and not the current directory.

This script would do it:

import os
import Rhino
p = os.path.expanduser("~/ac/_tmp/hello.py")
Rhino.ApplicationSettings.CommandAliasList.Add(
  'myalias', "!-_RunPythonScript \"{0}\"".format(p))

but of course where does the user put that script? …

Alain

Well, I guess I’m just being thick (as a brick) here… What I would like to do is as follows:

  1. Add a folder under ~/Library/Application Support/McNeel/Rhinoceros/scripts called, say, “junk”
  2. Be able to create an alias that will lead to this folder and launch a script inside it no matter what the user’s home directory is named or where the current directory marker is.

! _RunPythonScript "~/Library/Application Support/McNeel/Rhinoceros/scripts/junk/junkscript.py" should work, but doesn’t.

I don’t understand how a relative path can work unless you know where you are in the first place; and as far as I understand, you don’t have this info and it changes constantly. Maybe I just don’t understand. To get from here . which is your current directory, to here ~ which is your home directory, requires a number of ..\ which is one step up, but how many do you need?

–Mitch

I think this system is designed so that anyone can be easily confused even if they are not as thick as a brick. Seemed to make sense in 1968, though.

I’m a bit confused over what you want to do, though. You say you want to put the script in a folder hierarchy that starts at ~/ Do you mean that each user (each ~/ ) on the computer will have it’s own copy of the script? Or is your goal to have one copy of the script under some one user’s directory tree and then provide access to it to it by any other user on the machine? If the second, I’m not sure why you’d do that instead of putting the script in some generally accessible folder.

Have you ever worked your way up through the directory (folder) structure on your Mac to your hard drive root using Finder? Using the Menu bar, click “Go”, “Enclosing Folder”. This will take you up one level, just like …

You will eventually get to a directory (folder) whose entries are all the users on the computer. It will be the “Users” directory (folder). Above that is the Macintosh HD directory, which is analogous to the C:\ directory on Windows.

So every user’s ~ directory is an absolute node in the directory structure. To get to another user’s directory it’s just ~/…/newguy/and so on. Any user’s home directory will always be the same absolute path from Macintosh HD no matter what his current directory is.

Of course, you probably won’t have any permissions to use anything in the new guy’s directory tree. The permissions thing is why I don’t think you’ll have much happiness from the second approach.

Please forgive me for using the Unix/Linux/MS-DOS/Windows term “directory” so often instead of the Apple Mac term “folder”. They’re interchangeable and mean the same thing.

i think ~/ is a unix thing?

python (& apparently rhino aliases) don’t have automatic path expansion and it needs to be explicitly called when desiring shell-like path exapansion.

https://docs.python.org/2/library/os.path.html

but with rhino only, i can’t figure a way to do what you’re trying to do… (make a macro -RunPythonScript "~/Desktop/test.py" )

i’m not sure what the final goal of yours is but it sounds like you’re tyring to distribute a script to people which can be called via an alias. ?

which got me to thinking you could make a python script to ‘install’ and assign an alias. (maybe)… the only caveat is the user would need to run the installer script from within rhino using runPythonScript…

something like:

• run the script
• it asks where the desired script is… rs.OpenFileName() (it’s probably in the user’s download folder so maybe make that the default location when the dialog comes up)
• ask for the desired alias name… rs.GetString()
• do what alain was showing to get relative install location…

path = os.path.expanduser("~/Library/Application Support/McNeel/Rhinoceros/junkfolder")

• copy the .py from the downloads folder to path … shutil.copy(file, path) ?
• assign the alias name in rhino

rs.AddAlias(alias, "-RunPythonScript " + path)

so that’s possibly way off topic but something the maybe mess around with one day plus, (if it works like i think) it’s an example of the hoop jumping that might have to happen in order to use a relative path in a rhino alias.
(the actual alias in rhino would show the user name in it instead of ~ …but python can find out what the desired path is using ~ )

except what Mitch is saying is that doesn’t work… if you put ~/desktop in a rhino alias, rhino doesn’t know what or where ~/ is… i’d have to type Users/jeff/Desktop… you’d have to put Users/AIW/Desktop… and mitch would be Users/Helvetosaur/Desktop

we can’t all three use ~/Desktop

mitch’s question is asking what can all three of us type, exactly the same, and the path leads to our respective/individual desktops…


in terminal, we could all three use ~/Desktop
in python, it’d have to be os.path.expanduser("~/Desktop")
in rhino alias-- what do you type? mitch (nor i) can figure it out if it’s even possible to begin with.

You’re not being thick. This is not a user friendly hack that will hopefully do what you need for now.
In a near future release of Rhino the Python path will be configurable.

For your last example:

!-_RunPythonScript "./junk/junkscript.py"

and

!-_RunPythonScript "junk/junkscript.py"

should both work because the ~/Library/Application Support/McNeel/Rhinoceros/scripts path will be searched.

hth
Alain

hey that works!
:smile:

(as long as the folder is somewhere after the Scripts folder… but still. helpful)

My bad. I forgot that Windows didn’t implement it. I guess that in this day and age I expect this kind of stuff to be universal.

I still think the main hiccup will be overcoming access permissions when one user tries to use something in another user’s path.

This is not a real problem as Windows Rhino offers several ways of getting scripts to users, including custom toolbars and plug-ins, as well as being able to easily specify search paths to a specific folder. Mac Rhino does not have any of that yet unfortunately.

–Mitch

So, finally getting back to this after a summer full of other work… Thanks @Alain, this seems to be working correctly now! I’m almost done here, 75 tools ready to distribute, just need to do a bit more testing and then put it on a web page somewhere.

Quick minor question, if I don’t have any spaces in my python filenames, will something like !-_RunPythonScript ./junk/junkscript.py (without enclosing the path in quotes) be reliable? Seems to work here, it’s just a detail that will make it easier for me to get the paths to a text file automatically out of Excel. If not, I’ll figure out a way to get them in there.

Many thanks, --Mitch

Hey Mitch,

I prefer to keep the quotes just in case but it should be ok.

Alain

Hello,

Are they any way to make scripts recognize the windows user variables ?

Something like:
!-_RunPythonScript “%AppData%\McNeel\Rhinoceros\7.0\Plug-ins\PythonPlugins\myplugin\myscript.py”

would be ideal

Hi @gaspard.bourgeois,

I’ve logged your request.

https://mcneel.myjetbrains.com/youtrack/issue/RH-65032

– Dale

1 Like