Read file content instead to execute


Since as the program folder are differents between Windows and Mac, i wrote the code below.

app =

if app.empty?
  app = UI.openpanel("Pick app"), "w"){|f|
  f.puts "#{app}"

If the file is empty, everything works fine but if the file is not empty, the app is launched. I would like just read your content to insert into (batch / command) script.


FYI: You can call shell commands from inside Ruby with a %x string (which is an alias for the backquote method.)

ver = %x{ver}
which is the same as:
ver = `ver`


I am using system for now but i will take a look how %x works.

I don’t know why app = "C:\\path\\to\\executable.exe", works fine but app = don’t.

I would like to read the stored path to prevent the user repeat the task.

if RUBY_PLATFORM !~ /darwin/i
  # WIN
  path = UI.openpanel("Pick program","","Executable Files|*.bat;*.cmd;*.com;*.exe;*.ps1;||")
  # nil if user cancelled, otherwise a path string
  return if path.nil?
  # run the app
  # OSX
  path = UI.openpanel("Pick app","*.app")
  # nil if user cancelled, otherwise a path string
  return if path.nil?
  # run the app

I am not sure about the Mac. One of the mac guys can weigh in.


The return value from the UI.openpanel method is already a path string to a valid file. is a File class method that opens the file. It is usually used with text files.

It is not for “reading” strings. You will find string methods in the String class.


@DanRathbun, UI.openpanel makes to write a line empty after the file path?

I found that line empty at end from the file and when i delete the empty line, the script works.


I am sorry. I do not understand your question.

Can you simply explain what you wish to do ?


Sorry Dan. I tried to summarize because my english isn’t very good.

I need a file .txt with a program path inside, like “C:\program path\blender.exe”. This path can be different on each computer. Mac or PC.

The first time, the user pick the Blender executable with UI.openpanel and creates a .txt to prevent the user repeat the task.

After this path.txt created, i need to read the content and to create a batch file in Temp folder with the content “C:\program path\blender.exe-b -P “C:\path\to\” and a VBS for a silent work.

Python file, VBS and Batch in Temp folder is fine, has already been created.

The first time, the whole system works fine. The second time no.

In this moment, when the path.txt is created, It have a line with the executable path and one second line empty. This line empty have a behavior like “press Enter” and runs Blender and the blender file isn’t created.

If i delete this line empty and save the file, Blender not run and a Blender file is created properly.


I think your problem may be that puts appends a newline end to what it writes into the text file. You can remove this with String#chomp. Alternatively, you can avoid the newline by using print instead of puts (provided $\ is still at its default value of nil).


How about:

app = File.exist?(@path)
unless app
  app = UI.openpanel("Pick app")
  if app
    fi =, "w")
    app = true

This make a new file at @path if it doesn’t exist, if it does then nothing happens…
The reference app is set true/false…

You seem to be making this more complicated than it needs to be…


Or, unless you expect non-standard installation of apps, just check which system you are on and use the standard install path for that system.


on a mac
open -a Blender
finds the installed version, if it’s been opened at least once…

As system returns true/false I would use something like

unless system("open -a  Blender")
blender = UI.openpanel("Select", "/Applications")
%x(open #{blender})


John raises a good point: if the app has been normally installed so that it is in the system’s search path, you shouldn’t need to get the full absolute path to run it.


@TIG - really looks better.
I am a baby with programming yet. Everithing looks complicated. :grin:

@slbaumgartner - yes, it is for standard and non-standard installations. I will study about chomp.

@john_drivenupthewall - i will find some virtual machine for try that way on Mac.

Thank all you guys. I will try your tips and go back with news.