【问题标题】:open web browser using lua in a vlc extension在 vlc 扩展中使用 lua 打开 Web 浏览器
【发布时间】:2012-06-25 04:45:23
【问题描述】:

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

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

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

【问题讨论】:

  • 任何浏览器(最好是系统默认)。我只需要打开一个网址..
  • 这取决于操作系统。我猜你必须 a) 检测操作系统主机和 b) 通过命令行执行特定于操作系统的命令。如果它可用于 vlc 扩展,您可能可以使用 os.* lua 包执行 b)。我不知道如何从 vlc 做 a)。
  • 感谢 kikito :) 我在 Windows 上工作,我只是使用 os.execute("start google.com") 并且它可以工作。

标签: lua vlc


【解决方案1】:

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

  • 在 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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-03-31
    • 2021-11-08
    • 2015-02-26
    • 1970-01-01
    • 1970-01-01
    • 2011-12-29
    • 2016-08-15
    • 1970-01-01
    相关资源
    最近更新 更多