Convert Figma logo to code with AI

deanishe logoalfred-workflow

Full-featured library for writing Alfred 3 & 4 workflows

2,983
237
2,983
21

Top Related Projects

:metal: A collection of Alfred 3 and 4 workflows that will rock your world

Quick Overview

Alfred-workflow is a Python library designed to simplify the process of creating workflows for Alfred, a productivity application for macOS. It provides a high-level interface to common Alfred-related tasks, making it easier for developers to create powerful and efficient workflows.

Pros

  • Simplifies workflow creation with a user-friendly API
  • Extensive documentation and examples for easy learning
  • Supports both Python 2 and 3
  • Includes built-in caching and settings management

Cons

  • Limited to Alfred workflows, not applicable for other automation tools
  • Requires knowledge of Python programming
  • May have a learning curve for developers new to Alfred workflow creation

Code Examples

  1. Creating a simple workflow:
from workflow import Workflow, ICON_WEB

def main(wf):
    wf.add_item('Hello World', 'This is my first workflow',
                valid=True, icon=ICON_WEB)
    wf.send_feedback()

if __name__ == '__main__':
    wf = Workflow()
    wf.run(main)
  1. Using the cache:
from workflow import Workflow

def get_data():
    def wrapper():
        # Fetch data from an API
        return {'key': 'value'}

    return wf.cached_data('my_data', wrapper, max_age=600)

if __name__ == '__main__':
    wf = Workflow()
    data = get_data()
    # Use the data in your workflow
  1. Working with settings:
from workflow import Workflow

def main(wf):
    # Get a setting
    api_key = wf.settings.get('api_key', 'default_key')

    # Set a setting
    wf.settings['user_name'] = 'John Doe'

    # Settings are automatically saved

if __name__ == '__main__':
    wf = Workflow()
    wf.run(main)

Getting Started

  1. Install the library:

    pip install Alfred-Workflow
    
  2. Create a new Python file for your workflow:

    from workflow import Workflow
    
    def main(wf):
        # Your workflow logic here
        pass
    
    if __name__ == '__main__':
        wf = Workflow()
        wf.run(main)
    
  3. Add your workflow logic inside the main function.

  4. Set up your Alfred workflow to run your Python script.

Competitor Comparisons

:metal: A collection of Alfred 3 and 4 workflows that will rock your world

Pros of alfred-workflows

  • Collection of multiple workflows, providing a variety of functionalities
  • Easy to browse and find specific workflows for different needs
  • Community-driven with contributions from various developers

Cons of alfred-workflows

  • Less focused on providing a comprehensive framework for workflow development
  • May require more manual setup and configuration for each individual workflow
  • Potentially less consistent coding style and practices across different workflows

Code Comparison

alfred-workflow:

import sys
from workflow import Workflow, ICON_WEB, web

def main(wf):
    # The Workflow instance will be passed to the function
    # you call from `Workflow.run`
    # Your imports here if you want to catch import errors
    # ...
    # Your code here
    # ...
    # Add an item to Alfred feedback
    wf.add_item('Item title', 'Item subtitle', valid=True)
    # Send the results to Alfred
    wf.send_feedback()

if __name__ == '__main__':
    wf = Workflow()
    sys.exit(wf.run(main))

alfred-workflows (example from a specific workflow):

var alfy = require('alfy');

alfy.output([
    {
        title: 'Unicorn',
        subtitle: '🦄',
        arg: 'unicorn',
        icon: {
            path: 'unicorn.png'
        }
    }
]);

The alfred-workflow repository provides a more structured Python-based framework for developing Alfred workflows, while alfred-workflows is a collection of various workflows implemented in different languages and styles.

Convert Figma logo designs to code with AI

Visual Copilot

Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.

Try Visual Copilot

README

Alfred-Workflow logo

Alfred-Workflow

A helper library in Python for authors of workflows for Alfred 3 and 4.

Build Status Coverage Status Development Status Latest Version Supported Python Versions

Supports Alfred 3 and Alfred 4 on macOS 10.7+ (Python 2.7).

Alfred-Workflow takes the grunt work out of writing a workflow by giving you the tools to create a fast and featureful Alfred workflow from an API, application or library in minutes.

Always supports all current Alfred features.

Features

  • Auto-saved settings API for your workflow
  • Super-simple data caching with expiry
  • Fuzzy filtering (with smart diacritic folding)
  • Keychain support for secure storage of passwords, API keys etc.
  • Lightweight web API with Requests-like interface
  • Background tasks to keep your workflow responsive
  • Simple generation of Alfred JSON feedback
  • Full support of Alfred's AppleScript/JXA API
  • Catches and logs workflow errors for easier development and support
  • "Magic" arguments to help development/debugging
  • Unicode support
  • Pre-configured logging
  • Automatically check for workflow updates via GitHub releases
  • Post notifications via Notification Center

Alfred 4+ features

  • Advanced modifiers
  • Alfred 4-only updates (won't break older Alfred installs)

Contents

Installation

Note: If you're new to Alfred workflows, check out the tutorial in the docs.

With pip

You can install Alfred-Workflow directly into your workflow with:

# from your workflow directory
pip install --target=. Alfred-Workflow

You can install any other library available on the Cheese Shop the same way. See the pip documentation for more information.

It is highly advisable to bundle all your workflow's dependencies with your workflow in this way. That way, it will "just work".

From source

  1. Download the alfred-workflow-X.X.X.zip from the GitHub releases page.
  2. Extract the ZIP archive and place the workflow directory in the root folder of your workflow (where info.plist is).

Your workflow should look something like this:

Your Workflow/
    info.plist
    icon.png
    workflow/
        __init__.py
        background.py
        notify.py
        Notify.tgz
        update.py
        version
        web.py
        workflow.py
    yourscript.py
    etc.

Alternatively, you can clone/download the Alfred-Workflow repository and copy the workflow subdirectory to your workflow's root directory.

Usage

A few examples of how to use Alfred-Workflow.

Workflow script skeleton

Set up your workflow scripts as follows (if you wish to use the built-in error handling or sys.path modification):

#!/usr/bin/python
# encoding: utf-8

import sys

# Workflow3 supports Alfred 3's new features. The `Workflow` class
# is also compatible with Alfred 2.
from workflow import Workflow3


def main(wf):
    # The Workflow3 instance will be passed to the function
    # you call from `Workflow3.run`.
    # Not super useful, as the `wf` object created in
    # the `if __name__ ...` clause below is global...
    #
    # Your imports go here if you want to catch import errors, which
    # is not a bad idea, or if the modules/packages are in a directory
    # added via `Workflow3(libraries=...)`
    import somemodule
    import anothermodule

    # Get args from Workflow3, already in normalized Unicode.
    # This is also necessary for "magic" arguments to work.
    args = wf.args

    # Do stuff here ...

    # Add an item to Alfred feedback
    wf.add_item(u'Item title', u'Item subtitle')

    # Send output to Alfred. You can only call this once.
    # Well, you *can* call it multiple times, but subsequent calls
    # are ignored (otherwise the JSON sent to Alfred would be invalid).
    wf.send_feedback()


if __name__ == '__main__':
    # Create a global `Workflow3` object
    wf = Workflow3()
    # Call your entry function via `Workflow3.run()` to enable its
    # helper functions, like exception catching, ARGV normalization,
    # magic arguments etc.
    sys.exit(wf.run(main))

Examples

Cache data for 30 seconds:

def get_web_data():
    return web.get('http://www.example.com').json()

def main(wf):
    # Save data from `get_web_data` for 30 seconds under
    # the key ``example``
    data = wf.cached_data('example', get_web_data, max_age=30)
    for datum in data:
        wf.add_item(datum['title'], datum['author'])

    wf.send_feedback()

Web

Grab data from a JSON web API:

data = web.get('http://www.example.com/api/1/stuff').json()

Post a form:

r = web.post('http://www.example.com/',
             data={'artist': 'Tom Jones', 'song': "It's not unusual"})

Upload a file:

files = {'fieldname' : {'filename': "It's not unusual.mp3",
                        'content': open("It's not unusual.mp3", 'rb').read()}
}
r = web.post('http://www.example.com/upload/', files=files)

WARNING: As this module is based on Python 2's standard HTTP libraries, on old versions of OS X/Python, it does not validate SSL certificates when making HTTPS connections. If your workflow uses sensitive passwords/API keys, you should strongly consider using the requests library upon which the web.py API is based.

Keychain access

Save password:

wf = Workflow()
wf.save_password('name of account', 'password1lolz')

Retrieve password:

wf = Workflow()
wf.get_password('name of account')

Documentation

The full documentation, including API docs and a tutorial, can be found at deanishe.net.

Dash docset

The documentation is also available as a Dash docset.

Licensing, thanks

The code and the documentation are released under the MIT and Creative Commons Attribution-NonCommercial licences respectively. See LICENCE.txt for details.

The documentation was generated using Sphinx and a modified version of the Alabaster theme by bitprophet.

Many of the cooler ideas in Alfred-Workflow were inspired by Alfred2-Ruby-Template by Zhaocai.

The Keychain parser was based on Python-Keyring by Jason R. Coombs.

Contributing

Adding a workflow to the list

If you want to add a workflow to the list of workflows using Alfred-Workflow, don't add it to the docs! The list is machine-generated from Packal.org and the library_workflows.tsv file. If your workflow is available on Packal, it will be added on the next update. If not, please add it to library_workflows.tsv, and submit a corresponding pull request.

The list is not auto-updated, so if you've released a workflow and are keen to see it in this list, please open an issue asking me to update the list.

Bug reports, pull requests

Please see the documentation.

Contributors

Workflows using Alfred-Workflow

Here is a list of some of the many workflows based on Alfred-Workflow.