Run notepad from python script?


#1

I’d like to know how to run programs from a python script, should this work?


import rhinoscriptsyntax as rs
import os


filename = rs.OpenFileName("Open", "Text Files (*.txt)|*.txt||")

if filename:
    os.execl("C:\Windows\notepad.exe",filename)

Thanks Mark


#2

Hi Mark, try below, it uses Rhino’s built in command _Run:

import rhinoscriptsyntax as rs

def RunNotepad():
    cmd = "_Run C:/Windows/notepad.exe"
    rs.Command(cmd, False)

RunNotepad()

Note that if your path contains spaces, it is saver to surround it with quotes.

c.


#3

Thanks clement, I got that to work ok.
I’ve had problems with the Run command in RhinoScript in the past though but it will do for this.

Just done some tests and the problem with the Run command is if there are spaces in the program path and the file path Rhino sees the spaces in the path as enter. I tried add quotes around the program name and the file I’m trying to open but that doesn’t work either.

Thanks Mark


#4

Looks like I got around this problem in Rhinoscript using ShortPath, is there anything similar in python.

Thanks Mark.


#5

Hi Hughes,

Add chr(34) to the end and beginning of your string path.


#6

Hi djordje

Tried that it doesn’t work. The Run command doesn’t work if the are spaces in the program you are calling and spaces in the path to the file you’re trying to open.

_Run “C:\Xpert DNC\Program\xdncedit.exe c:\temp\temp.txt” works.

_Run “C:\Xpert DNC\Program\xdncedit.exe c:\temp\New Folder\temp.txt” doesn’t work.
_Run “C:\Xpert DNC\Program\xdncedit.exe”“c:\temp\New Folder\temp.txt” doesn’t work.

_Run “C:\windows\Notepad.exe c:\temp\New Folder\temp.txt” works.

The script below works ok with files in subdirectories with spaces in the name, but if you change the Editor to call a program in a subdirectory with spaces it doesn’t work.

import rhinoscriptsyntax as rs


filename = rs.OpenFileName("Open", "Text Files (*.txt)|*.txt||")

if filename:
    Editor = '"C:\windows\Notepad.exe '
    #Editor = '"C:\Xpert DNC\Program\xdncedit.exe '
    cmd = '_Run '+ Editor + filename + '"'
    rs.Command(cmd, False)


#7

Here’s something to try, this script will let you select a Rhino file the start Rhino with the file. It works if there are no spaces in the path to the file you’re trying to open.

import rhinoscriptsyntax as rs


filename = rs.OpenFileName("Open", "Rhino Files (*.3dm)|*.3dm||")

if filename:
    #Editor = '"C:\windows\Notepad.exe '
    Editor = '"C:\Program Files\Rhinoceros 5 (64-bit)\System\Rhino.exe '
    cmd = '_Run '+ Editor + filename + '"'
    rs.Command(cmd, False)

Thanks Mark.


#8

you could do this:

import System

#path=filepath

System.Diagnostics.Process.Start("notepad",path)

I haven’t tried it specifically with notepad, but it should work.


#9

Thanks defterGoose that works. Here’s the example that starts Rhino with a file.

import rhinoscriptsyntax as rs
import System

filename = rs.OpenFileName("Open", "Rhino Files (*.3dm)|*.3dm||")

if filename:
    Editor = 'C:\Program Files\Rhinoceros 5 (64-bit)\System\Rhino.exe'
    System.Diagnostics.Process.Start(Editor,'"'+filename+'"')

Mark.


#10

I am not entirerly shure but i guess you need to add a space between the app and filename. This aside, i wonder why you wanted to define an editor at all, if you run a Rhino filename and the extension is associated with the proper type of exe, it should open the file right away. eg. try this from the macro editor:

_Run """C:\Users\UserName\Desktop\File.3dm"""

what i found curious is that i could not run your first example. If i run it, the command line displays:

Unknown command: otepad.exe

In the example i´ve posted, i´ve just used forward slashes instead of backward slashes. If you try my example above with backward slashes, it fails with the same error in the commandline.

c.


#11

The problem with the slashes is python sees \n as a new line, if you use \Notepad.exe it works.

I only used notepad as an example anyone could try, I just wanted some way to run an external program and pass a file for it to open. defterGoose’s post works so I guess my problem’s solved.

Thanks Mark.


#12

that explains it. Thanks.

c.


#13

Backslashes in Python “escape” the following character - hence the “otepad.exe” - so if you need to hard code file paths, you can use double backslashes:

C:\\Programs\\YourProgram.exe

There are also some methods in os.path module to deal with some of this.

–Mitch