7

I'd like to open a given URL from a small command-line app I wrote in Dart. Any simple way to do it? This would be similar to Desktop#browse(URI) in Java.

P.Y.
  • 137
  • 5

3 Answers3

8

Try this code:

import "dart:io";

void runBrowser(String url) {
  var fail = false;
  switch (Platform.operatingSystem) {
    case "linux":
      Process.run("x-www-browser", [url]);
      break;
    case "macos":
      Process.run("open", [url]);
      break;
    case "windows":
      Process.run("explorer", [url]);
      break;
    default:
      fail = true;
      break;
  }

  if (!fail) {
    print("Start browsing...");
  }
  • Thanks! Based on @günter-zöchbauer reply, using xdg-open is the way to go for Linux. – P.Y. Oct 09 '15 at 17:45
5

You need to do launch it using Process.run() or Process.start() and you have to take care of OS differences yourself.

On
- Linux you can use Linux: command to open URL in default browser (needs to be installed but it usually is by default)
- Windows https://superuser.com/questions/36728/can-i-launch-urls-from-command-line-in-windows
- OSX http://osxdaily.com/2011/07/18/open-url-default-web-browser-command-line/

Community
  • 1
  • 1
Günter Zöchbauer
  • 623,577
  • 216
  • 2,003
  • 1,567
2

The invoke the default browser on Windows:

Process.run("start", [url], runInShell: true);

(Tested on Windows 7 only though I'm afraid)

Argenti Apparatus
  • 3,857
  • 2
  • 25
  • 35