Path to user directory without username

Rhino for Windows - path to user directory without having to specify username?

On Mac, the “default” scripts directory is

~/Library/Application Support/McNeel/Rhinoceros/Scripts

where ~ signifies the user directory, if the user is logged on, this shorthand works as a path to the directory without having to specify the username - useful for publicly distributed aliases.

Does such a shorthand exist for Windows? In normal Windows search, %AppData% gets you to that particular hidden user directory - but how to have that kind of functionality with a Rhino alias? I’m looking for a way to create a “universal” path to a specified folder/file that avoids having to specify a username (which I will not know).

Thx, --Mitch

Hey Mitch,

%USERPROFILE%

just checked …

import os
print os.path.expanduser("~\AppData\Local\Temp\TempScript.py")

works on Windows.

Hmm, can’t get that to work…

! _-RunPythonScript "%USERPROFILE%\AppData\Roaming\McNeel\Rhinoceros\5.0\TestFolder\TestScript.py"

1 Like

Yeah, I’m looking at the os.path stuff, but I need to do this outside of a script as an alias…

Hmm, now I am not getting this to work on Mac either…

Works:
! _-RunPythonScript "/Users/myusername/Library/Application Support/McNeel/Rhinoceros/Scripts/Scriptname.py"

Doesn’t:
! _-RunPythonScript "~/Library/Application Support/McNeel/Rhinoceros/Scripts/Scriptname.py"

:confused:

Sorry, I misread. I don’t think that would have ever worked on windows or on the mac.

Your mac example is on the path and the Windows example is almost on the path so you can run it like this:

!_-RunPythonScript "..\TestFolder\TestScript.py"

Hmm, I don’t know why I thought it worked on Mac…

Well, nothing that I tried so far is working on either platform right now…

That’s strange.

Since you know that C:\Users\username\AppData\Roaming\McNeel\Rhinoceros\5.0\scripts is on the path then any path relative to it should work. For example:

C:\Users\username\AppData\Roaming\McNeel\Rhinoceros\5.0\TestFolder\MyScript.py
!_-RunPythonScript "..\TestFolder\MyScript.py"

C:\Users\username\AppData\Roaming\McNeel\Rhinoceros\5.0\scripts\samples\advanced
!_-RunPythonScript ".\Samples\Advanced\CustomGetPoint.py"

That doesn’t work for you?

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 ~ )