Batch save Python script on a Mac

I’m trying to write a batch open and save script in Python to run on a Mac. I’d like it to open and saveas (or export) files automatically without input from the user. The best I can get it to do is bring up the open file dialog box. At that point the user has to select a file to open and hit enter. The Command() function is supposed to be able to take an argument specifying the file path but this doesn’t seem to work on a Mac. Does anybody have any ideas?

`import rhinoscriptsyntax as rs
import os

def BatchSaveAs():    
    #Get folders
    folder = rs.BrowseForFolder(message = "Select folder to process")
    if not folder: return
   
    saveFolder=rs.BrowseForFolder(message = "Select folder to save files")
    if not saveFolder: return
    
    found = False ; counter = 0 ;
    for filename in os.listdir(folder):
        if filename.endswith(".STEP"):
            found = True ; counter += 1
            fullpath = os.path.join(folder,filename).lower()
            rs.EnableRedraw(False)
            #Close the current file
            rs.DocumentModified(False)
            rs.Command("_-New _None",False)
            
            #Open file to convert
            rs.Command ("_-Open " + folder + "_Enter")
            comm="_-SaveAs"
            rs.Command(comm + chr(34) + saveFolder + chr(34) + ".3dm" + "_Enter")
            
    if not found:
        print "No .STEP files found in selected folder!"
    else:
        print "{} files converted to 3dm format".format(counter)
        
    #open a blank document to finish
    rs.DocumentModified(False)
    rs.Command("_-New _None",False)
    rs.EnableRedraw(True)
        
BatchSaveAs()

upon an initial skim, the first problem i see is it appears you’re trying to open a .STEP with rhino.

Rhino can’t open a .StEP file directly… you’d need to import the file into a blank .3DM then save.

I’m using version 5.1 (5B161) and you can open a .STEP file directly with the Open command. What I’d like to do is open a file without the user interface. As it stands, the script will prompt the user for a source and target folder, which is what I want, then it brings up the open dialog box and stops while it waits for the user to select a file and hit enter. My impression is that it’s impossible to get around this manual interaction with the dialog box.

1 Like

hmm. i did not realize that you could Open a .step until now… my bad.

fwiw, i understand what you’re after… i haven’t tried the script or analyzed it too much… i won’t have time to try to help today… maybe tonight/tmmr
(and even then, i don’t know if i’ll be of much help :wink: )

Did you make any progress with this please?

I would like if possible, to batch (re-) save Rhino files into a previous version.

This has been difficult on Mac up until recently because of the MDI - my old batch scripts worked, but opened as many new windows as files to be converted. I just tried with Python RhinoCommon File3dm - which allows you to read and write files without actually having to go through the opening and closing process - the following seems to work:

BatchSaveAsVersion.py (1.7 KB)

It will ask you for the 3dm file version to save to, and will be sticky within the same session. It does not require you to close the current file you are working on (although I would save it first anyway).

2 Likes