{"id":17166757,"url":"https://github.com/willf/alfred-scripts","last_synced_at":"2025-03-24T18:15:53.158Z","repository":{"id":66569781,"uuid":"421087472","full_name":"willf/alfred-scripts","owner":"willf","description":"How to create Alfred Scripts in Python","archived":false,"fork":false,"pushed_at":"2021-10-26T17:51:29.000Z","size":18,"stargazers_count":3,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-03-24T12:52:23.102Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/willf.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2021-10-25T15:48:01.000Z","updated_at":"2024-09-10T04:17:55.000Z","dependencies_parsed_at":null,"dependency_job_id":"d921029a-c23a-497b-8b56-a8552c782fd5","html_url":"https://github.com/willf/alfred-scripts","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/willf%2Falfred-scripts","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/willf%2Falfred-scripts/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/willf%2Falfred-scripts/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/willf%2Falfred-scripts/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/willf","download_url":"https://codeload.github.com/willf/alfred-scripts/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":245325221,"owners_count":20596818,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-10-14T23:06:33.788Z","updated_at":"2025-03-24T18:15:53.151Z","avatar_url":"https://github.com/willf.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Writing Alfred copy/paste scripts in Python\n\nThis repository shows how to create [Alfred](https://www.alfredapp.com) scripts in Python. The examples below assume that you have Python 3 installed via Homebrew.\n\nOur working example will be a script that will convert text in the Mac clipboard to small caps, that is, for example, it will take \"Hello World!\" and replace it with \"ʜᴇʟʟᴏ ᴡᴏʀʟᴅ!\"\n\n## Set up\n\n1. Create a directory in which you want to place your Python-based Alfred scripts. I created mine in `$HOME/projects/alfred-scripts`\n2. Install the Python `clipboard` package locally (and any other packages you need):\n\n```bash\ncd $HOME/projects/alfred-scripts\npython3 -m pip install --target dist clipboard\n```\n\n## Writing and testing a script locally\n\nFirst you need to decide what your text transformation will do. Let's start with something simple: converting text to upper case:\n\n```python\ndef convert(text):\n    return text.upper()\n```\n\nBy my own convention, I write this scripts to run in three ways: in test mode, from the command line, and in clipboard mode (which is what we will use for Alfred). So I have a driver that looks like this:\n\n```python\nif __name__ == \"__main__\":\n    if len(sys.argv) \u003e 1:\n        action = sys.argv.pop(1)\n        if action == \"--test\":\n            unittest.main()\n        elif action == \"--clipboard\":\n            clipboard_main()\n        else:\n            print(\"Unknown action: {}\".format(action))\n        sys.exit(1)\n    else:\n        main()\n```\n\nYou can use your own conventions, but this allow me to test the script out from the command line.\n\nThe main function is easy enough:\n\n```python\ndef main():\n    sys.stdout.write(convert(sys.stdin.read()))\n```\n\nYou can embed unit tests in the script and invoke them using the `--test` flag as given above:\n\n```python\nimport unittest\n\nclass TestConvert(unittest.TestCase):\n    def test_convert(self):\n        text = \"Hello World!\"\n        match = \"HELLO WORLD!\"\n        self.assertEqual(convert(text), match)\n```\n\nOnce you are satisfied that your conversion routine is working, you can write the simple clipboard main:\n\n```python\ndef clipboard_main():\n    to_convert = clipboard.paste()\n    clipboard.copy(convert(to_convert.lower()))\n```\n\n(Note that you are _pasting_ into your program, and _copying_ back into the clipboard).\n\n## Creating a Alfred Workflow\n\nCreating the Alfred workflow is relatively simple. Create a new worklfow and name it. Typically, it will have four steps:\n\n1. A trigger command\n2. Executing ⌘C to copy text into the clipboard\n3. Running your script\n4. Executing ⌘V to paste text\n\nThe script should look like this (I use [zsh](https://www.zsh.org), your script might vary). Note that\nwe install the `clipboard` package; if there are other packages you need, you'll want to do the same thing.\n\n```zsh\nexport PATH=/opt/homebrew/bin:/usr/local/bin:$PATH\nexport SCRIPTDIR=$HOME/projects/alfred-scripts\nexport PYTHONPATH=$SCRIPTDIR/dist\npython3 $SCRIPTDIR/upper.py --clipboard\n\n```\n\nThis is what the workflow looks like:\n\n\u003cimg width=\"839\" alt=\"Workflow\" src=\"https://user-images.githubusercontent.com/37049/138725865-26e7b952-7b64-45c1-a849-ac84d7326223.png\"\u003e\n\nI find myself often needing to debug the Alfred script, so it's useful to learn [how to use the debugger](https://www.alfredapp.com/help/workflows/advanced/debugger/).\n\nNote: many thanks to [@deanishe](https://github.com/deanishe) for improvements to what I'm doing. Any\nfoolishness remaining I claim for my own. See also his [https://github.com/deanishe/alfred-workflow](alfred-workflow) package.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fwillf%2Falfred-scripts","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fwillf%2Falfred-scripts","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fwillf%2Falfred-scripts/lists"}