AddAlias, LastLoadedScript and AddStartupScript in Python: creating commands

(this topic was split from Which rhinoscriptsyntax functions do you need to have implemented next?)

What it does with Rhinoscripts - as far as I understand…

When you drag and drop an .rvb file into the Rhino window, it loads it, but does not run it. After that, the LastLoadedScriptFile is the one you just dropped into Rhino so now Rhino knows the path to the file. After that it adds an alias using that path and adds it to the scripts that are loaded (but not run) when Rhino starts. Thus, the script is now sort of “installed”, i.e. when Rhino opens, the script is loaded, and then running the alias runs the script.

This is what an excerpt from an .rvb file set up to drag and drop looks like:

Rhino.AddAlias "MyScriptName", "_NoEcho _-Runscript MyScriptName"
Rhino.AddStartupScript Rhino.LastLoadedScriptFile

Python scripts would need to be treated a bit differently - the problem is that Python scripts are not included in Rhino’s startup scripts. So you’re right, AddStartupScript() probably isn’t necessary. However, that isn’t really a problem, we don’t really need the scripts to be loaded at startup, just a drag and drop method of creating an alias that has the full path to the script file just dropped in…

I’m not that fond of the drag and drop method of script installation myself, but for distribution, it simplifies the instructions for how to use greatly…

Thx, --Mitch

@Helvetosaur Isn’t the purpose of this the same of what you’d get with:

Purpose, perhaps yes. But I’m NEVER going to go through all that mess just to hand out a small script to someone. That’s just way too complicated, it really needs to be simplified if you want people to use it IMO.

–Mitch

This in interesting. If an expert users such as you says this, we’d need to at least discuss this and see if maybe we do need an alternative approach… I’ll split this topic.

I agree; it was an idea of mine that never really turned into something useful. We should probably rethink how to simplify the sharing of small scripts. It seems like a little more thought should be put into this than just rehashing these functions in python.

My first thought is that we could provide a simple UI to create an alias that turns around and calls
-_RunPythonScript path\to\script\file.py
The alias could simply be the name of the python script that is dropped on Rhino. You can change it later by editing aliases in the options dialog.

Yeah, that was pretty much what I was suggesting.

If you wanted to make a nice, user-friendly UI, you could throw up a getstring dialog when the script is dropped onto Rhino with the script file name as default filled in and highlighted - and let the user either hit Enter to accept or type a new alias name. In any case there needs to be a check to see if the alias exists already and either flat-out refuse to overwrite (tell the user the alias is already registered and to chose a different one), or offer the user to replace the existing alias-script link with the new one, or choose another name and keep the original.

–Mitch

Hi Mitch, all

FWIW I’s like to share my workaround method to automatically register an alias when running a python script:

if( __name__ == '__main__' ):
    # get the name of this scriptfile
    script_name = os.path.basename(os.path.realpath(__file__)).split('.')[0]
    # construct a string to be set as the macro
    macro = '! _-RunPythonScript "{}"'.format(os.path.realpath(__file__))
    # register an Alias with the name of the script ( excluding extension)
    if not rs.IsAlias(script_name):
        rs.AddAlias(script_name,macro)

-Willem

1 Like