Setting working directory on file opening

I want to set the working folder to the current document folder on startup, as I am constantly bouncing from project to project. I made a simple python script and called it on startup using a command script in Tools–>Options–>General, but it looks like that gets activated “too early” to work. So what’s the best way to do this, and to have it get set every time a document is opened instead of just on startup?

Hi @JimCarruthers,

have you tried to subscribe to scriptcontext.doc.EndOpenDocument and put the code to setup the working folder in the event handler ? You might then set this script in the startup command list, eg. something like below:

import Rhino
import scriptcontext
import rhinoscriptsyntax as rs
import os

def MySetupWorkingFolderEvent(sender, e):
    try:
        if e.FileName:
            folder, filename= os.path.split(e.FileName)
            rs.WorkingFolder(folder)
    except Exception as ex:
        print ex

def RunCommand():
    key = "MySetupWorkingFolderEvent"
    if not scriptcontext.sticky.has_key(key):
        scriptcontext.sticky[key] = MySetupWorkingFolderEvent
        scriptcontext.doc.EndOpenDocument += MySetupWorkingFolderEvent
        
if __name__=="__main__":
    RunCommand()

it might get problematic as it also sets the working folder when opening a new empty file (template).
_
c.

1 Like

Well it took me close to 6 months to get around to testing this but it seems to work! Yeah I need to do something different when starting a new emptly file, I can probably handle that…