Module: CommandKit::OpenApp

Includes:
Env::Path, OS
Defined in:
lib/command_kit/open_app.rb

Overview

Allows opening a file or a URI with the system's preferred application for that file type or URI scheme.

Examples

open_app_for "movie.avi"
open_app_for "https://github.com/postmodern/command_kit.rb#readme"

Since:

  • 0.2.0

Instance Attribute Summary

Attributes included from Env::Path

#path_dirs

Attributes included from Env

#env

Attributes included from OS

#os

Instance Method Summary collapse

Methods included from Env::Path

#command_installed?, #find_command

Methods included from OS

#bsd?, #freebsd?, #linux?, #macos?, #netbsd?, #openbsd?, #unix?, #windows?

Methods included from CommandKit::OS::ModuleMethods

#included

Instance Method Details

#initialize(**kwargs) ⇒ Object

Initializes the command and determines which open command to use.

Parameters:

  • kwargs (Hash{Symbol => Object})

    Additional keyword arguments.

Since:

  • 0.2.0



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/command_kit/open_app.rb', line 30

def initialize(**kwargs)
  super(**kwargs)

  @open_command = if macos?
                    'open'
                  elsif linux? || bsd?
                    if command_installed?('xdg-open')
                      'xdg-open'
                    end
                  elsif windows?
                    if command_installed?('invoke-item')
                      'invoke-item'
                    else
                      'start'
                    end
                  end
end

#open_app_for(file_or_uri) ⇒ Boolean?

Opens a file or URI using the system's preferred application for that file type or URI scheme.

Examples:

Open a file:

open_app_for "movie.avi"

Open a URI:

open_app_for "https://github.com/postmodern/command_kit.rb"

Parameters:

  • file_or_uri (String, URI)

    The file path or URI to open.

Returns:

  • (Boolean, nil)

    Specifies whether the file or URI was successfully opened or not. If the open command could not be determined, nil is returned.

Since:

  • 0.2.0



65
66
67
68
69
# File 'lib/command_kit/open_app.rb', line 65

def open_app_for(file_or_uri)
  if @open_command
    system(@open_command,file_or_uri.to_s)
  end
end