Launch an Application in RUBY
-
Have you tried with absolute path? Your relative path might be referring to the wrong place.
Do you get any errors? Or does it silently fail? -
The API's
UI.openURL()
will open any 'location' using its default application - it doesn't need to be a web 'url' like
UI.openURL("http://forums.sketchucation.com/")
it can be the path to a file on your PC - say a .txt file, that then opens with Notepad.exe.
UI.openURL("C:/Users/TIG/Desktop/My Text.txt")
If the filepath is an .exe file it opens the application
UI.openURL("C:/Windows/System32/notepad.exe")
On a PC at least you can even just specify the exe with no path and it'll open
UI.openURL("Notepad.exe")
You can't give an .exe 'arguments' with 'openURL', so if you want to open a particular file with an application that isn't its default OR open a file with its app' but with arguments, on a PC you first make a temporary .cmd file [.command files on Mac], then add a line opening the application desired and the argument, use UI.openURL to 'open' the cmd file - it'll run it and then open the file with the non-default application. To tidy up you should delete the temporary cmd file at the end...tmp_cmd="My_tmp_file_"+Time.now.to_i.to_s+".cmd" ### safest to make a uniquely named temp file cmd_path="C;/Temp/"+tmp_cmd cmd_line="\"C;/Program Files/Notepad++/Notepad++.exe\" \"C;/Users/TIG/Desktop/My Text.txt\"\nexit" ### note escape \" around paths with spaces in ### the exit line ensures it closes afterwards... ### you could add '@echo off\n' etc at the start to run non-verbosely in the terminal window. cmd=File.new(cmd_path, "w") cmd.puts(cmd_line) cmd.close UI.openURL(cmd_path) ### the txt file now opens with Notepad++.exe NOT it's default Notepad.exe sleep(1) File.delete(cmd_path) ### we erase tmp_cmd file after waiting a second for it to run ### omit this line to see the file that's made in C;/Temp/
You can adapt this method to open any application needing arguments
-
Thanks for the reply guys,
it silently fails using my way.
Tig I tryed your way however the location of the file needing to be run wont always be in the same place however it will be in the same folder of the program everytime so I cannot use the full path will this still working using "../../program.exe"?
-
Can you clarify ?
If you know where this file is can't you use the full path ?
mdir=File.dirname(model.path)
gives you a folder thenFile.join(mdir, myfilename)
gives the file's path ??
If it's not the model's folder you are referring to then you might have it coded somewhere so you can use that ? -
@simonstaton said:
Tig I tryed your way however the location of the file needing to be run wont always be in the same place however it will be in the same folder of the program everytime so I cannot use the full path will this still working using "../../program.exe"?
But you should try with the full path to ensure that the method work. Because if it does, then you know it is your relative path is incorrect. It might be that the working directory is not the one you assume it is.
You could try File.expand_path() on your relative path and see what absolute path it translates to.
-
Hi Tig,
sorry im a bit of a newb when it comes to this stuff
I will try and explain from the sketchup folder where you have components etc the file I want to launch is "client/googleearth.exe" however the plugin that is calling it is client "Plugins/Utilities/Toolbar.rb" from the sketchup folder. So what command would I use to call the googleearth.exe from toolbar.rb
-
@thomthom said:
@simonstaton said:
Tig I tryed your way however the location of the file needing to be run wont always be in the same place however it will be in the same folder of the program everytime so I cannot use the full path will this still working using "../../program.exe"?
But you should try with the full path to ensure that the method work. Because if it does, then you know it is your relative path is incorrect. It might be that the working directory is not the one you assume it is.
You could try File.expand_path() on your relative path and see what absolute path it translates to.
ohh I see now ok I will give that a go.
-
ok using the full path didnt work
-
ge=UI.openURL("C:\\Program Files\\Google\\Google Earth\\client\\googleearth.exe")
or
ge=UI.openURL("C:/Program Files/Google/Google Earth/client/googleearth.exe")
works and opens googleearth
ge=UI.openURL('googleearth.exe')
doesn't work as it's not been registered to open without a full path [on PC]
Note how 'ge
' will returntrue
if it succeeds, andfalse
if fails - soif not ge
popup a warning message and then run UI.openpanel() for the user to point your tool to googleearth ? -
ok that works with the full path however, google earth is not installed on the clients machine it will be inside sketchup in a way so when they install sketchup it will be inside the sketchup folder and where the sketchup folder is depends on where they have installed it on there machine. our clients are not pc friendly at all so keeping it to the basics is probably best
-
@simonstaton said:
ok that works with the full path however, google earth is not installed on the clients machine it will be inside sketchup in a way so when they install sketchup it will be inside the sketchup folder and where the sketchup folder is depends on where they have installed it on there machine. our clients are not pc friendly at all so keeping it to the basics is probably best
You can find where Sketchup is installed using
Sketchup.find_support_file('')
which returns something like
C:/Program Files/Google/Google SketchUp 7
-
ah there we go solved, now how would I merge the two? this dosnt seem to be working:
ge=UI.openURL(Sketchup.find_support_file('')"/client/googleearth.exe")
-
dont worry i needed to put a "+" between to two solved! thanks tig
Advertisement