alfred

package module
v0.0.0-...-852dcc8 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 9, 2023 License: MIT Imports: 19 Imported by: 7

README

go-alfred

An Alfred Workflow utility library for Go

Installation

To use the go-alfred library, include github.com/jason0x43/go-alfred as a dependency in your code. The library imports as alfred.

To install the alfred utility, run:

go install github.com/jason0x43/go-alfred/alfred

Documentation

Documentation

Overview

Package alfred provides an API and various utility methods for creating Alfred workflows.

Index

Constants

View Source
const (
	// Line is an underline
	Line = "" /* 237-byte string literal not displayed */

	// MinAlfredVersion is the minimum supported version of Alfred
	MinAlfredVersion = "3.1"
)

Variables

This section is empty.

Functions

func CleanSplitN

func CleanSplitN(s, sep string, n int) []string

CleanSplitN trims leading and trailing whitespace from a string, splits it into at most N parts, and trims leading and trailing whitespace from each part

func FuzzyMatches

func FuzzyMatches(val string, test string) bool

FuzzyMatches returns true if val and test have a fuzzy match score != -1

func FuzzySort

func FuzzySort(items []Item, test string)

FuzzySort sorts an items list in-place based how well they match a given test string.

func IsDebugging

func IsDebugging() bool

IsDebugging indicates whether an Alfred debug panel is open

func LoadJSON

func LoadJSON(filename string, structure interface{}) error

LoadJSON reads a JSON file into a provided strucure.

func RunScript

func RunScript(script string) (string, error)

RunScript runs an arbitrary AppleScript.

func SaveJSON

func SaveJSON(filename string, structure interface{}) error

SaveJSON serializes a given structure and saves it to a file.

func SavePlist

func SavePlist(filename string, p Plist)

SavePlist saves a plist to an XML file

func SplitCmd

func SplitCmd(s string) (head, tail string)

SplitCmd splits the initial word (a keyword) apart from the rest of an argument, returning the keyword (head) and the rest (tail). Whitespace is trimmed from both parts.

func Stringify

func Stringify(thing interface{}) string

Stringify serializes a data object into a string suitable for including in an item Arg

func TrimAllLeft

func TrimAllLeft(parts []string) []string

TrimAllLeft returns a copy of an array of strings in which space characters are trimmed from the left side of each element in the array.

Types

type Action

type Action interface {
	Command
	Do(data string) (string, error)
}

Action is Command that does something

type ByTitle

type ByTitle Items

ByTitle is an array of Items which will be sorted by title.

func (ByTitle) Len

func (b ByTitle) Len() int

Len returns the length of a ByTitle array.

func (ByTitle) Less

func (b ByTitle) Less(i, j int) bool

Less indicates whether one ByTitle element should come before another.

func (ByTitle) Swap

func (b ByTitle) Swap(i, j int)

Swap swaps two elements in a ByTitle array.

type Command

type Command interface {
	About() CommandDef
}

Command is a Filter or Action

type CommandDef

type CommandDef struct {
	Keyword     string
	Description string
	Mods        map[ModKey]ItemMod
	IsEnabled   bool
	Arg         *ItemArg
}

CommandDef describes a workflow command

func (*CommandDef) KeywordItem

func (c *CommandDef) KeywordItem() (item Item)

KeywordItem creates a new Item for a command definition

type Filter

type Filter interface {
	Command
	Items(arg, data string) ([]Item, error)
}

Filter is a Command that creates a filtered list of items

type GitHubRelease

type GitHubRelease struct {
	DataURL    string `json:"url"`
	URL        string `json:"html_url"`
	Name       string `json:"name"`
	Prerelease bool   `json:"prerelease"`
	Tag        string `json:"tag_name"`
	Version    semver.Version
	Created    time.Time `json:"created_at"`
	Published  time.Time `json:"published_at"`
	Assets     []struct {
		URL         string `json:"url"`
		Name        string `json:"name"`
		DownloadURL string `json:"browser_download_url"`
	} `json:"assets"`
}

GitHubRelease describes a project release on GitHub

func (*GitHubRelease) IsNewer

func (g *GitHubRelease) IsNewer(ver string) (isNewer bool, err error)

IsNewer returns true if this release is newer than a given semver string

type Item

type Item struct {
	UID          string
	Title        string
	Subtitle     string
	Autocomplete string
	Arg          *ItemArg
	Icon         string
	// contains filtered or unexported fields
}

Item is an Alfred list item

func (*Item) AddCheckBox

func (i *Item) AddCheckBox(selected bool)

AddCheckBox modifies an Item to represent a selectable choice.

func (*Item) AddMod

func (i *Item) AddMod(key ModKey, mod ItemMod)

AddMod adds a an ItemMod to an Item's mod map, creating the map if necessary

func (*Item) MarshalJSON

func (i *Item) MarshalJSON() ([]byte, error)

MarshalJSON marshals an Item into JSON

type ItemArg

type ItemArg struct {
	// Keyword specifies the destination keyword for this item
	Keyword string
	// Mode specifies what mode the keyword should operate in
	Mode ModeType
	// Data is the data string that will be passed to the target Command
	Data string
}

ItemArg is an item argument

type ItemMod

type ItemMod struct {
	Arg      *ItemArg
	Subtitle string
}

ItemMod is a modifier

type Items

type Items []Item

Items is a list of items

func InsertItem

func InsertItem(items []Item, item Item, index int) Items

InsertItem inserts an item at a specific index in an array of Items.

func (Items) MarshalJSON

func (i Items) MarshalJSON() ([]byte, error)

MarshalJSON marshals a list of Items

type ModKey

type ModKey string

ModKey is a modifier key (e.g., cmd, ctrl, alt)

const (
	ModCmd   ModKey = "cmd"
	ModShift ModKey = "shift"
	ModAlt   ModKey = "alt"
	ModCtrl  ModKey = "ctrl"
)

ModKey constants

type ModeType

type ModeType string

ModeType describes the workflow's current mode

const (
	ModeDo   ModeType = "do"
	ModeTell ModeType = "tell"
	ModeBack ModeType = "back"
)

ModeType constants

type Plist

type Plist map[string]interface{}

Plist is a plist data structure

func LoadPlist

func LoadPlist(filename string) (p Plist)

LoadPlist loads a plist from an XML file

type Workflow

type Workflow struct {
	UpdateIcon string
	// contains filtered or unexported fields
}

Workflow represents an Alfred workflow

func OpenWorkflow

func OpenWorkflow(workflowDir string, createDirs bool) (w Workflow, err error)

OpenWorkflow returns a Workflow for a given directory. If the createDirs option is true, cache and data directories will be created for the workflow.

func (*Workflow) AddPassword

func (w *Workflow) AddPassword(name, password string) (err error)

AddPassword adds or updates a password in the macOS Keychain

func (*Workflow) AddUpdateItem

func (w *Workflow) AddUpdateItem(items *Items)

AddUpdateItem performs an update check and adds an update item to the given items list if one is available.

func (*Workflow) BundleID

func (w *Workflow) BundleID() string

BundleID returns a workflow's bundle ID.

func (*Workflow) CacheDir

func (w *Workflow) CacheDir() string

CacheDir returns the cache directory for a workflow.

func (*Workflow) DataDir

func (w *Workflow) DataDir() string

DataDir returns the data directory for a workflow.

func (*Workflow) GetConfirmation

func (w *Workflow) GetConfirmation(prompt string, defaultYes bool) (confirmed bool, err error)

GetConfirmation opens a confirmation dialog to ask the user to confirm something.

func (*Workflow) GetInput

func (w *Workflow) GetInput(prompt, defaultVal string, hideAnswer bool) (button, value string, err error)

GetInput opens an input dialog to ask the user for some information.

func (*Workflow) GetPassword

func (w *Workflow) GetPassword(name string) (pw string, err error)

GetPassword returns a workflow-specific password from the macOS Keychain

func (*Workflow) Name

func (w *Workflow) Name() string

Name returns a workflow's name.

func (*Workflow) Run

func (w *Workflow) Run(commands []Command)

Run runs a workflow.

A Workflow understands the following command line formats

$ ./workflow (arg|data)
$ ./workflow arg data
$ ./workflow -final data

Run takes one parameter: a list of Commands. Commands may be Filters or Actions. Filters are commands that generate lists of items, while Actions are commands that take an action.

When the mode is "tell"...

  • ...and a keyword was specified in the incoming data, the Filter matching that keyword (if there is one) is called to generate items
  • ...and no keyword was specified in the incoming data, items are generated for:
  • any Filter with a fuzzy-matching keyword
  • any Action with a fuzzy-matching keyword and an Arg in its CommandDef

func (*Workflow) SendToAlfred

func (w *Workflow) SendToAlfred(items Items, data workflowData)

SendToAlfred sends an array of items to Alfred. Currently this equates to outputting an Alfred JSON message on stdout.

func (*Workflow) ShowMessage

func (w *Workflow) ShowMessage(message string) (err error)

ShowMessage opens a message dialog to show the user a message.

func (*Workflow) UpdateAvailable

func (w *Workflow) UpdateAvailable() (release GitHubRelease, available bool)

UpdateAvailable checks once a day whether a newer version of this workflow is available on GitHub.

func (*Workflow) UpdateAvailableNow

func (w *Workflow) UpdateAvailableNow() (release GitHubRelease, available bool)

UpdateAvailableNow checks immediately whether a newer version of this workflow is available on GitHub.

func (*Workflow) Version

func (w *Workflow) Version() string

Version returns a workflow's website URL.

func (*Workflow) Website

func (w *Workflow) Website() string

Website returns a workflow's website URL.

func (*Workflow) WorkflowDir

func (w *Workflow) WorkflowDir() string

WorkflowDir returns a workflow's location.

Directories

Path Synopsis
The alfred command can be used to manage Go-based Alfred workflows.
The alfred command can be used to manage Go-based Alfred workflows.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL