| 1 | package osx |
| 2 | |
| 3 | import ( |
| 4 | "os/exec" |
| 5 | "runtime" |
| 6 | ) |
| 7 | |
| 8 | // OpenDefault opens the specified file or URL with the default associated application. |
| 9 | // |
| 10 | // You may use it to open a web site: |
| 11 | // OpenDefault("https://google.com") |
| 12 | // |
| 13 | // Or open a file: |
| 14 | // OpenDefault("/home/bob/story.txt") |
| 15 | // |
| 16 | // Or open a folder in your default file manager: |
| 17 | // OpenDefault("/home/bob") |
| 18 | // |
| 19 | // For details, see https://stackoverflow.com/a/39324149/1705598 |
| 20 | func OpenDefault(fileOrURL string) error { |
| 21 | var cmd string |
| 22 | var args []string |
| 23 | |
| 24 | switch runtime.GOOS { |
| 25 | case "windows": |
| 26 | cmd = "cmd" |
| 27 | args = []string{"/c", "start"} |
| 28 | case "darwin": |
| 29 | cmd = "open" |
| 30 | default: // "linux", "freebsd", "openbsd", "netbsd" |
| 31 | cmd = "xdg-open" |
| 32 | } |
| 33 | args = append(args, fileOrURL) |
| 34 | return exec.Command(cmd, args...).Start() |
| 35 | } |