3

我正在编写一个 VLC 扩展,我想在 Web 浏览器中打开一些 url(当然是在 lua 中)。到目前为止,我还没有找到任何从 lua 脚本打开 Web 浏览器的相关代码。有什么方法可以执行此任务(例如,谷歌搜索正在播放的文件)?

我可以使用对话框创建指向 url 的链接,但我想跳过此步骤并使其在没有任何用户输入的情况下打开。

我是 lua 和制作 VLC 扩展的初学者(几天前才开始),从那时起一直在尝试。

4

1 回答 1

4

确切的命令因操作系统而异:

  • 在 Windows 上:
    start http://example.com/
  • 在 *nix 上(最便携的选项):
    xdg-open "http://example.com/"
  • 在 OSX 上:
    open http://example.com/

以下 Lua 示例应该可以在 Windows、Linux 和 OSX 上运行(尽管我无法测试 OSX)。
它首先检查 Lua 的package.config目录\\分隔符(afaik 仅在 Windows 上使用)。那应该只给我们留下支持uname. 然后我飞跃并假设 Mac 将识别为“达尔文”,因此任何不是 *nix 的东西。

显然,这还不够详尽。

-- Attempts to open a given URL in the system default browser, regardless of Operating System.
local open_cmd -- this needs to stay outside the function, or it'll re-sniff every time...
function open_url(url)
    if not open_cmd then
        if package.config:sub(1,1) == '\\' then -- windows
            open_cmd = function(url)
                -- Should work on anything since (and including) win'95
                os.execute(string.format('start "%s"', url))
            end
        -- the only systems left should understand uname...
        elseif (io.popen("uname -s"):read'*a') == "Darwin" then -- OSX/Darwin ? (I can not test.)
            open_cmd = function(url)
                -- I cannot test, but this should work on modern Macs.
                os.execute(string.format('open "%s"', url))
            end
        else -- that ought to only leave Linux
            open_cmd = function(url)
                -- should work on X-based distros.
                os.execute(string.format('xdg-open "%s"', url))
            end
        end
    end

    open_cmd(url)
end
于 2013-09-18T05:22:57.657 回答