Read file content instead to execute


#1

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

app = File.read(@path)

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

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.


#2

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`


#3

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 = File.read(@path) don’t.

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


#4
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
  UI.openURL(path)
else
  # OSX
  path = UI.openpanel("Pick app","*.app")
  # nil if user cancelled, otherwise a path string
  return if path.nil?
  # run the app
  %x{path}
end

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


#5

The return value from the UI.openpanel method is already a path string to a valid file.

File.read(pathstring) 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.


#6

@DanRathbun, UI.openpanel makes File.open 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.


#7

I am sorry. I do not understand your question.

Can you simply explain what you wish to do ?


#8

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\file.py” 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.


#9

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).


#10

How about:

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

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…


#11

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.


#12

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 Blender.app", "/Applications")
%x(open #{blender})
end

#13

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.


#14

@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.