Jump to content

Multiple commands/titles (aliases)


Recommended Posts

I'd to use aliases for my commands. E.g. support both `on` and `activate` as commands after the keyword (`bt`)

I'm using the script filter json format and tried using the title field as list of strings (works but does show `(` as title in alfred) and use a list of strings as match. Both didn't work, but title came the closest. Alternative would be to add multiple entries with the same arg, but I personally think a list as title or match or something is a cleaner solution. Just don't know whether it's possible.

What is the best way to support aliases for commands? 

Edited by vidavidorra
Link to comment
20 minutes ago, vidavidorra said:

What is the best way to support aliases for commands?

 

Add a second command with a different name that does the same thing. If you have Alfred filtering the results, you could also use the match field to specify the different aliases.

 

Alfred's JSON format clearly defines title, subtitle, arg etc. as single strings. You can't return a completely different type (an array) and expect that to work in a reasonable way.

Edited by deanishe
Link to comment

@deanishe Thanks for your reply. I ended up creating a python script which is used as script filter which returns a JSON string as described in the docs. This scripts runs the query on a command list (aliases). It might not be the cleanest solution, but works perfectly so far as I've tested it!

 

import sys
import json

commands = [
    {
        'title': 'On',
        'arg': 'on',
        'command_list': ['on', 'up', 'activate'],
        'autocomplete': 'Bluetooth On'
    },
    {
        'title': 'Off',
        'arg': 'off',
        'command_list': ['off', 'down', 'deactivate'],
        'autocomplete': 'Bluetooth Off'
    },
    {
        'title': 'Restart',
        'arg': 'restart',
        'command_list': ['toggle', 'change', 'switch'],
        'autocomplete': 'Bluetooth Toggle'
    },
    {
        'title': 'restart',
        'arg': 'restart',
        'command_list': ['reset', 'restart'],
        'autocomplete': 'Bluetooth Restart'
    }
]


def suggest_command(query):
    """
    Return a JSON object with the commands that start with query.

    Parameters
    ----------
    query : str
        Query where the returned commands must start with

    Returns
    -------
    dict
        JSON ojbect of commands that start with query
    None
        No

    """
    query = query.strip()

    data = {
      "items": []
    }

    for command in commands:
        if any(item.startswith(query) for item in command['command_list']):
            # print(command['arg'])
            item = {
              'title': command['title'],
              'autocomplete': command['autocomplete'],
              'arg': command['arg']
            }

            data['items'].append(item)

    if data['items']:
        return data
    else:
        return None


def main(arg):
    res = suggest_command(arg)
    if res:
        print(json.dumps(res))


if __name__ == u'__main__':
    main(sys.argv[1] if len(sys.argv) > 1 else '')

 

Link to comment

That works. But you could do the same more simply by putting the command keywords in match and letting Alfred do the filtering:

import json
import sys

commands = [
    {
        'title': 'On',
        'arg': 'on',
        'match': 'on up activate',
        'autocomplete': 'Bluetooth On',
    },
    {
        'title': 'Off',
        'arg': 'off',
        'match': 'off down deactivate',
        'autocomplete': 'Bluetooth Off',
    },
    {
        'title': 'Restart',
        'arg': 'restart',
        'match': 'toggle change switch',
        'autocomplete': 'Bluetooth Toggle',
    },
    {
        'title': 'restart',
        'arg': 'restart',
        'match': 'reset restart',
        'autocomplete': 'Bluetooth Restart',
    },
]

json.dump(dict(items=commands), sys.stdout)

In fact, if that's the entirety of the code, you don't even need Python. Just save a JSON file and cat it to Alfred. Much faster.

Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...