我正在嘗試創建一個可以打開 .html 檔案的工具,但是我需要有關將打開所述 .html 檔案的代碼的幫助。我在下面有這個代碼......
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
這段代碼的輸出顯示在下面的螢屏截圖中。
https://i.stack.imgur.com/ixLIT.png
這正是我所期望的。也沒有在 Chrome 中打開 .html,因為有兩個 .html 檔案。所以現在我更進一步,嘗試指定我想要打開的 .html 檔案。我想打開'basic.html'(到目前為止它是一個空白的.html檔案)并相應地更改我的代碼(第一行是具體的)。
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
不幸的是,我沒有得到我希望的輸出。這就是我最終的結果。
https://i.stack.imgur.com/4xyQT.png
basic.html 也沒有在 Chrome 中打開,這很糟糕。
下面是我在 Plugins 檔案夾中的檔案的樣子,如果你想看的話。
https://i.stack.imgur.com/OW7xM.png
我面臨的問題是什么?
uj5u.com熱心網友回復:
如何測驗以下代碼:
- 創建 .rb 檔案(例如,將其命名為“open-html.rb”)并復制并粘貼以下代碼。然后將檔案放在 SketchUp Plugins 檔案夾中。
- 通過轉到
Plugins or Extension Menu->激活 Sketchup 中的工具Open basic.html inside the plugins folder - 完畢!如果插件檔案夾中有一個名為“basic.html”的 HTML 檔案,則此腳本將打開它。
可能的解決方案#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
可能的解決方案#2(更好的解決方案)
我更喜歡使用“.rb”腳本的相對路徑而不是使用“插件”檔案夾路徑來查找“basic.html”的位置。
此外,當打開本地 HTML 檔案時,最好使用.set_file而不是.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
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/440692.html
