Git Product home page Git Product logo

alfred's Introduction

Easy Alfred Workflow Script Filters in Go

GoDoc

This is a lean but comprehensive implementation of the Alfred Script Filter JSON Format to get Alfred workflows off the ground quickly with blazingly fast script filters in Go that can be seamlessly developed inside or outside Alfred.

It uses standard, familiar Go syntax and conventions as much as possible for rapid use by Go developers and integration with other Go code.

Installation

go get github.com/drgrib/alfred

Full Alfred JSON Support

full-json.md has examples of how to produce the full range of JSON output for Alfred's Script Filter JSON Format.

A Simple Example

Let's say we want to create a simple script filter that converts a given query to title case, lower case, or upper case, for which Go conveniently has built-in support.

Let's start by prototyping our logic in Go with a case.go file in our workflow folder. We can do this on the command line or in the editor of our choice and easily run it inside or outside of Alfred:

package main

import (
	"strings"

	"github.com/drgrib/alfred"
)

func addCases(arg string) {
	titlecase := strings.Title(arg)
	alfred.Add(alfred.Item{
		Title:    titlecase,
		Subtitle: "Title",
		Arg:      titlecase,
		UID:      "titlecase",
	})

	lowercase := strings.ToLower(arg)
	alfred.Add(alfred.Item{
		Title:    lowercase,
		Subtitle: "Lower",
		Arg:      lowercase,
		UID:      "lowercase",
	})

	uppercase := strings.ToUpper(arg)
	alfred.Add(alfred.Item{
		Title:    uppercase,
		Subtitle: "Upper",
		Arg:      uppercase,
		UID:      "uppercase",
	})
}

func main() {
	arg := "just a test"
	addCases(arg)
	alfred.Run()
}
{
    "items": [
        {
            "uid": "title",
            "title": "Just A Test",
            "subtitle": "Title",
            "arg": "Just A Test"
        },
        {
            "uid": "lower",
            "title": "just a test",
            "subtitle": "Lower",
            "arg": "just a test"
        },
        {
            "uid": "upper",
            "title": "JUST A TEST",
            "subtitle": "Upper",
            "arg": "JUST A TEST"
        }
    ]
}

Looks good. Now let's add os.Args support and test it on the command line to simulate Alfred input:

package main

import (
	"os"
	"strings"

	"github.com/drgrib/alfred"
)

// [same stuff in the middle]

func main() {
	arg := os.Args[1]
	addCases(arg)
	alfred.Run()
}
go build case.go
./case "another test"
{
    "items": [
        {
            "uid": "title",
            "title": "Another Test",
            "subtitle": "Title",
            "arg": "Another Test"
        },
        {
            "uid": "lower",
            "title": "another test",
            "subtitle": "Lower",
            "arg": "another test"
        },
        {
            "uid": "upper",
            "title": "ANOTHER TEST",
            "subtitle": "Upper",
            "arg": "ANOTHER TEST"
        }
    ]
}

Right again. Alright. Now let's drop this into our script filter:

script-filter

And give it a whirl:

test

Why not copy these to the clipboard so we can actually use them?

clipboard

With a few simple runs and a glance at the Alfred clipboard history, we can see we are ready for business:

clipboard

Easy!

alfred's People

Contributors

drgrib avatar

Stargazers

 avatar

Watchers

 avatar

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.