Code Monkey home page Code Monkey logo

open-golang's Introduction

open

Description

Open a file, directory, or URI using the OS's default application for
that object type. Optionally, you can specify an application to use.

This is a proxy for the following commands:

        OSX: "open"
    Windows: "start"
Linux/Other: "xdg-open"

This is a golang port of the node.js module:
https://github.com/pwnall/node-open

Documentation

http://godoc.org/github.com/skratchdot/open-golang/open

Import

import "github.com/skratchdot/open-golang/open"

Usage

open google.com in the user's default browser (method 1):

open.Run("https://google.com/")

open google.com in the user's default browser (method 2):

open.Start("https://google.com")

you can listen for errors

err := open.Run("https://google.com/")

you can specify the program to use

open.RunWith("https://google.com/", "firefox")	

Functions

func Run(input string) error

Open a file, directory, or URI using the OS's default application for
that object type. Wait for the open command to complete.

func RunWith(input string, appName string) error

Open a file, directory, or URI using the specified application. Wait for
the open command to complete.

func Start(input string) error

Open a file, directory, or URI using the OS's default application for
that object type. Don't wait for the open command to complete.

func StartWith(input string, appName string) error

Open a file, directory, or URI using the specified application. Don't
wait for the open command to complete.

License

Copyright (c) 2013 skratchdot
Licensed under the MIT license.

open-golang's People

Contributors

adamtang79 avatar brianredbeard avatar byron avatar fffw avatar grymoire7 avatar oxtoacart avatar seanhoughton avatar skratchdot avatar sorcix avatar thundergroove avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

open-golang's Issues

Choose between several open browser profiles

Typically I run 2-3 profiles in {firefox,chromium} to distuinigish between {work,personal,other projects}. Is there a way of discovering the running profiles in order to choose which browser to open?

Open folder in foreground

When opening a folder location, the window (at least on windows) appears in background behind other applications. Is there any way to put it in foreground?

Run() fails on Windows in Git Bash / Cygwin / MSYS environments

We have a CLI tool that runs code like this:

if err := open.Run(url); err != nil {
  utils.Log("Unable to launch a browser")
  utils.LogDebugError(err)
}

It works fine in Linux, MacOS and in a Windows command prompt. However, it fails when it executes inside Git Bash (which is essentially a MSYS/Cygwin-like environment). The failure message is "Incorrect function".

My suspicion here is that the Windows implementation simply doesn't work in MSYS/Cygwin-style environments. Simply running start $URL in Git Bash opened up a browser as expected. Also, prefixing the command with winpty worked (which is included with Git Bash by default โ€“ see https://github.com/rprichard/winpty).

Add a function to "Wait" for opened program exit (macOS)

Hello,

I was wondering if you were open to add a function or a flag to Wait for the opened program to close.
It's possible on macOS, with -W (-n and -F could also be used):

-W Causes open to wait until the applications it opens (or that were already open) have exited. > Use with the -n flag to allow open to function as an appropriate app for the $EDITOR environment

-n Open a new instance of the application(s) even if one is already running.`

-F Opens the application "fresh," that is, without restoring windows. Saved persistent state is lost, except for Untitled documents.`

I have a use case of this and discovered your library, but I resorted to going back to exec.Command() to be able to specify this.

Now, xdg-open doesn't have a wait for feature, and I don't think windows does either.
So I understand if this feature is not desired.

Kind regards,

incognito window and env

hey,

I wanted to be able to open a new incognito window.

I also wanted to pass in environment variable. The golang runtime might pass them around, but i feel like this is insecure. is it ? its rather important i can make sure my environment variable reaches the started window.

Like #13 says i think some options capabilities might do it.

Needs release with proper semver tag

This needs to be released with a proper semver tag.
go 1.19

$ go get -v github.com/haya14busa/goplay/cmd/goplay
github.com/haya14busa/goplay/cmd/goplay imports
github.com/skratchdot/open-golang/open: cannot find module providing package github.com/skratchdot/open-golang/open

Opening folders does not work on Windows

I can get open-golang to open files (a pdf in my browser) and urls, but not folders. I want to open a folder in Windows Explorer.

The readme says:

Open a file, directory, or URI using the OS's default application

so I assume directories should be supported.

// In cmd.exe, I setup and run the program.
// Install dependencies:
//   go get "github.com/skratchdot/open-golang/open/..."
// Ensure directory exists:
//   mkdir c:\scratch
// Build:
//   go build open_with_open.go
//   open_with_open.exe
package main

import (
    "fmt"
    "github.com/skratchdot/open-golang/open"
)

func main() {
    // None of these options work.
    exe_path := `c:/scratch`
    exe_path = `c:/scratch/`
    exe_path = `c:\scratch`
    // I see this output.
    fmt.Printf("Trying to open folder: %v\n", exe_path)
    // nothing happens except command prompt loses focus. I see no error.
    err := open.Start(exe_path)
    if err != nil {
        panic(err)
    }
}

I want the equivalent of this in a command prompt:

start c:\scratch

Is open-golang the wrong package? (I'm new to golang, so forgive my ignorance.)

It doesn't open file if I run my script from systemctl

I use next service file;

[Unit]
Description=Goserver

[Service]
Type=simple
Restart=always
RestartSec=3

Environment=GOPATH=/home/user/goserver/
Environment=BROWSER=gvfs-open
WorkingDirectory=/home/user
ExecStart=/usr/bin/go run /home/user/goserver/totalmanager.go

[Install]
WantedBy=multi-user.target

The script with open-golang is inside totalmanager.go

If I run my script "go run goserver/totalmanager.go" it works!

But if I use "sudo systemctl start totalmanager.service" it doesn't work!
May be something wrong with Environment?

Can't find xdg-open in production

The open method references xdg-open relative to package source. So the tests pass, but this doesn't work in a shipping product where the executable may be moved or not even be shipped with the source.

open.go:66> app := path.Join(path.Dir(file), "..", "vendor", "xdg-open")

In shipping code, I use:

open.go:66> app := "./xdg-open"

and ship xdg-open alongside the executable.

A different solution would be to default to "xdg-open" (assume it's in the path) and allow it to be settable by the user to another location.

Files don't open on windows when called externally (but don't error either)

I've written some very simple code to just open a text file

	err = open.Run("C:/test.txt")
	if err != nil {
		return err
	}

When I run this executable from the command line, it works fine. When I run another executable which then runs this executable, the code no longer works, but there is no error.

The weird thing is that when mypath is a directory, windows will happily open file explorer at that directory, even when being called from another executable:

	err = open.Run("C:/")
	if err != nil {
		return err
	}

^ This works fine. But any file will not.

If there's some permissions issue going on that's fine but shouldn't this return an error if it's not able to complete?
Any insight into what's going on here would be welcomed too.

Nothing happens when trying to open a jpeg

Hey,

I've been trying to open a simple JPEG. but I get no error and nothing happens. xdg-open is installed and I've tested to manually open the JPEG, which worked just fine. Any clue why this might happen?

open.Run fails without details

When using open.Run to open an application in macOS, the error printed is exit status 1

This does not mean much, is it possible to enhance the error making it more meaningful ?

On Windows CMD shell opens briefly

Hi, I am a newb to both golang and Windows development so forgive a possibly stupid question.

First, this is a very useful library - thanks to dev(s).

Invoking Start or Run causes a blank CMD shell to briefly appear with focus. It does go away quickly but seems to upset users.

Is there a way to suppress the CMD shell in a way that at least it is not visible to the user. Something appearing on the taskbar would be fine.

Thank you!

Please provide support for mobile browsers

Hi, I'm working on application for mobile and use rclone library to sync files from cloud providers. The rclone library depends on yours, and uses to open OAuth requests to upstream provider. Since there is no support for mobile within your library I can't proceed using rclone. I would really appreciate if you'll add support to open urls in mobile environment. I know that it is possible since I'm using fyne.io For example, you can see how they are doing open call for android and ios.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.