Alfredで自作プラグイン(workflow) 最初の一歩

Alfredでプラグイン作るぞ!!! と思った人が最初に見るべき記事が足りない気がしたので書きます。

なお、プラグインの利用および作成は課金民だけに許されています。世の中カネっすわ

用語

作りたいもの

みなさん大体こういうものを作りたいはずです

  • Alfredを呼び出す
  • おもむろに文字を叩く
  • それに何らかの形で呼応したアイテムがシュッと出て来る

なので本記事では、それに即したHello Worldをやってみます。

Getting Started

Alfredの設定 > Workflows > [+] > Script Filter

Continue を押したら、雛形ができました。 XMLのほうは要らないので消します。

やったー完成です。お疲れ様でした(ぇ

結局、JSONを吐けばいいのさ

このままでは自作した感がありません。一旦、jsonformatの中身を見ましょう。 Script Filterとして、標準出力に適切な出力(つまりJSON)を流すことが求められるようです。

#
# Alfred Script Filter JSON format
#
# This example demonstrates all fields available for populating results.
#
# For an in-depth explanation, use the (?) help button to the bottom left.
#

cat << EOB
{"items": [

    {
        "uid": "desktop",
        "type": "file",
        "title": "Desktop",
        "subtitle": "~/Desktop",
        "arg": "~/Desktop",
        "autocomplete": "Desktop",
        "icon": {
            "type": "fileicon",
            "path": "~/Desktop"
        }
    },

    {
        "valid": false,
        "uid": "flickr",
        "title": "Flickr",
        "icon": {
            "path": "flickr.png"
        }
    },

    {
        "uid": "image",
        "type": "file",
        "title": "My holiday photo",
        "subtitle": "~/Pictures/My holiday photo.jpg",
        "autocomplete": "My holiday photo",
        "icon": {
            "type": "filetype",
            "path": "public.jpeg"
        }
    },

    {
        "valid": false,
        "uid": "alfredapp",
        "title": "Alfred Website",
        "subtitle": "https://www.alfredapp.com/",
        "arg": "alfredapp.com",
        "autocomplete": "Alfred Website",
        "quicklookurl": "https://www.alfredapp.com/",
        "mods": {
            "alt": {
                "valid": true,
                "arg": "alfredapp.com/powerpack",
                "subtitle": "https://www.alfredapp.com/powerpack/"
            },
            "cmd": {
                "valid": true,
                "arg": "alfredapp.com/powerpack/buy/",
                "subtitle": "https://www.alfredapp.com/powerpack/buy/"
            },
        },
        "text": {
            "copy": "https://www.alfredapp.com/ (text here to copy)",
            "largetype": "https://www.alfredapp.com/ (text here for large type)"
        }
    }

]}
EOB

詳細についてはここで https://www.alfredapp.com/help/workflows/inputs/script-filter/json/

既存の実装はどうなっているの?

次に、Chromeのブックマークを検索するWorkflowを参考にしてみます。 https://github.com/blainesch/alfred-chrome-bookmarks

Script Filter の部分をみると、こんなんでした。

PROFILE="~/Library/Application Support/Google/Chrome/**/Bookmarks" php bookmarks.php {query}

出力を見せてもらいましょう。

export PROFILE="~/Library/Application Support/Google/Chrome/**/Bookmarks"
php bookmarks.php {query} > ~/Desktop/result.json
php bookmarks.php {query}
<?xml version="1.0"?>
<items>
    <item arg="https://accounts.google.com/AccountChooser?service=mail" uid="61">
        <title>Gmail</title>
        <subtitle>https://accounts.google.com/AccountChooser?service=mail</subtitle>
    </item>
    <item arg="https://ipsj.ixsq.nii.ac.jp/ej/index.php?action=pages_view_main&amp;active_action=repository_view_main_item_snippet&amp;index_id=5305&amp;pn=1&amp;count=20&amp;order=16&amp;lang=japanese&amp;page_id=13&amp;block_id=8" uid="2210.92">
        <title>情報学広場:情報処理学会電子図書館</title>
        <subtitle>https://ipsj.ixsq.nii.ac.jp/ej/index.php?action=pages_view_main&amp;active_action=repository_view_main_item_snippet&amp;index_id=5305&amp;pn=1&amp;count=20&amp;order=16&amp;lang=japanese&amp;page_id=13&amp;block_id=8</subtitle>
    </item>

標準出力の行き先である Open URL の Object を眺めてみるに、 arg属性が、次の{query}になることが推測できます。

ところで、あなたはXMLを使うフレンズなんだね・・・。

出力を次の処理の入力にする

いよいよ、Script Filterをいじってみましょう。

まず、画面をポチポチして、Object を増やします。 そして、2つの要素を繋ぎます。

jsonformatのスクリプトを改造します (なお、望むならkeywordの変更も、ここでやれます)

これで、記事冒頭の動画になりました。

まとめ

みなさんもAlfredでどしどしプラグインを作って便利になっていきましょう。