I am trying to create a tool which will open a .html file, however I need help with the piece of code which will open said .html file. I have this code below...
help_file = Sketchup.find_support_files("html", "Plugins")
if help_file
# Print out the help_file full path
UI.messagebox(help_file)
# Open the help_file in a web browser
UI.openURL("file://" help_file)
else
UI.messagebox("Failure")
end
The output from this piece of code is shown in the screenshot below.
https://i.stack.imgur.com/ixLIT.png
That is what I had expected. No .html opened in Chrome either because there's two .html files. So now I take a step further and try to specify which .html file I would like opened. I would like to open the 'basic.html' (it's a blank .html file so far) and I change my code accordingly (the first line to be specific).
help_file = Sketchup.find_support_files("basic.html", "Plugins")
if help_file
# Print out the help_file full path
UI.messagebox(help_file)
# Open the help_file in a web browser
UI.openURL("file://" help_file)
else
UI.messagebox("Failure")
end
Unfortunately, I didn't get the output I had hoped for. This is what I ended up with.
https://i.stack.imgur.com/4xyQT.png
the basic.html didn't open in Chrome either, which was a bummer.
Below is what my files in the Plugins folder looks like, incase you wanted to see that.
https://i.stack.imgur.com/OW7xM.png
What is the problem I am facing?
CodePudding user response:
How to test code below:
- Create .rb file (name it 'open-html.rb' for example) and copy & paste the code below. Then place the file in the SketchUp Plugins folder.
- Activate the tool inside Sketchup by going to
Plugins or Extension Menu
->Open basic.html inside the plugins folder
- Done! If you have an HTML file named 'basic.html' inside the plugins folder then this script will open it.
Possible Solution #1
module DevName
module PluginName
class Main
def activate
@dlg = UI::HtmlDialog.new(html_properties_activate)
plugins_folder = "file:///#{Sketchup.find_support_file('Plugins').gsub(/ /, ' ')}" #/
html_file = File.join(plugins_folder, 'basic.html')
@dlg.set_url(html_file)
@dlg.show
@dlg.center
end
def html_properties_activate
{
dialog_title: 'Dialog Example',
preferences_key: 'com.sample.plugin',
scrollable: true,
resizable: true,
width: 420,
height: 320
}
end
end
end
end
unless file_loaded?(__FILE__)
menu = UI.menu('Plugins')
menu.add_item('Open basic.html inside the plugins folder') do
Sketchup.active_model.select_tool(DevName::PluginName::Main.new)
end
file_loaded(__FILE__)
end
Possible Solution #2 (Better Solution)
I prefer finding the location of 'basic.html' using the relative path of your '.rb' script and not using the 'Plugins' folder path.
Also, when opening a local HTML file it is best to use .set_file
instead of .set_url
.
module DevName
module PluginName2
class Main
def activate
@dlg = UI::HtmlDialog.new(html_properties_activate)
path = File.dirname(__FILE__)
html_file = File.join(path, '/basic.html')
@dlg.set_file(html_file)
@dlg.show
@dlg.center
end
def html_properties_activate
{
dialog_title: 'Dialog Example',
preferences_key: 'com.sample.plugin',
scrollable: true,
resizable: true,
width: 420,
height: 320
}
end
end
end
end
unless file_loaded?(__FILE__)
menu = UI.menu('Plugins')
menu.add_item('Open basic.html inside plugins folder Solution #2') do
Sketchup.active_model.select_tool(DevName::PluginName2::Main.new)
end
file_loaded(__FILE__)
end