3

Webブラウザで(もちろんluaで)いくつかのURLを開きたいVLC拡張機能を書いています。これまでのところ、lua スクリプトから Web ブラウザーを開くための関連コードを見つけることができませんでした。このタスクを実行する方法はありますか (たとえば、再生中のファイルの Google 検索など)。

ダイアログ ボックスを使用して 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(\\これは、Windows でのみ使用されます)。これで、サポートする OS だけが残るはずunameです。次に、飛躍的に飛躍し、Mac が「Darwin」として識別されると仮定します。したがって、そうでないものはすべて *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 に答える