Links
Comment on page

Developing Alfred5 workflow using alfred5 python package

programming, alfred5, alfred workflow, python, macOS
DALLE_2023-11-01_01.15.53_-_Hand-drawn_illustration_of_an_incredibly_sweet_and_friendly-looking_eagle_sitting_at_a_computer_using_Visual_Studio_wearing_an_Alfred5_hat._The_eagle.png

🔰 How to Create Workflow

2oDMChr.png
IMVWNDm.png
WicJKBN.png
AwPNT8Y.png
DALLE_2023-11-01_01.17.40_-_Hand-drawn_illustration_of_an_engineer_eagle_creating_the_architecture_of_a_software_project.The_eagle_looks_focused_and_determined_wearing_glasses.png

Preparing to Code Workflow using alfred5 package and python

Init your project directory

~ MindEase Theme
  • Create src folder
  • Put your codes and requirements.txt to src folders
Install [`alfred5`](https://github.com/yemreak/alfred5)
pip3 install alfred5 --target=src/libs
Append the top of the `main.py`
import sys
sys.path.insert(0, "src/libs")
  • If u want to use different -target for ex . use WorkflowClient.run(packagedir=".")
If you use `vscode`, add the code that below to `.vscode/settings.json` to debug your file
~ MindEase Theme
{
"python.analysis.extraPaths": [
"./src/libs"
],
"python.analysis.exclude": [
"./src/libs"
]
}
⚠️ If u installed all of requirements, dont need to create requirements.txt file in src
DALLE_2023-11-01_01.17.41_-_Hand-drawn_illustration_of_an_intellectual_eagle_trying_to_understand_a_complex_software_project._This_eagle_wears_reading_glasses_and_is_deep_in_thou.png

Understanding alfred5 use-cases

  • Via SnippetsClient API create custom snippets programmaically
  • Via WorkflowClient API create custom alfred workflow
    • Craete requirements.txt file for your python project to let alfred5 installs them if needed 🙃
    • To install from requirements.txt do all import packages inside main
    • Use global keyword to access imported packages globally
      • client.query is the query string
      • client.page_count is the page count for pagination results
    • Dont need to add alfred5 to requirements.txt
  • Use WorkflowClient.log to log your message to alfred debugger
  • Use WorkflowClient(main, cache=True) method to use caching system
    • Just do it for static (not timebased nor any dynamic stuff) response
    • Db path is db/results.yml also you can see it from workflow debug panel
DALLE_2023-11-01_01.20.02_-_Hand-drawn_illustration_of_a_contented_eagle_sitting_beside_its_computer_which_has_a_prominent_Python_logo_on_the_screen.The_eagle_having_solved_a.png

⭐️ Example Project

tUJjVUJ.png
from re import sub
from urllib.parse import quote_plus
import sys
sys.path.insert(0, "src/libs")
from alfred5 import WorkflowClient
async def main(client: WorkflowClient):
# To auto install requirements all import operation must be in here
global get
from requests import get
query = client.query
client.log(f"my query: {query}") # use it to see your log in workflow debug panel
# (use cache=True) Use cache system to quick response instead of old style that below
# if client.load_cached_response():
# return
char_count = str(len( query))
word_count = str(len(query.split(" ")))
line_count = str(len(query.split("\\n")))
encoded_string = quote_plus(query)
remove_dublication = " ".join(dict.fromkeys(query.split(" ")))
upper_case = query.upper()
lower_case = query.lower()
capitalized = query.capitalize()
template = sub(r"[a-zA-Z0-9]", "X", query)
client.add_result(encoded_string, "Encoded", arg=encoded_string)
client.add_result(remove_dublication, "Remove dublication", arg=remove_dublication)
client.add_result(upper_case, "Upper Case", arg=upper_case)
client.add_result(lower_case, "Lower Case", arg=lower_case)
client.add_result(capitalized, "Capitalized", arg=capitalized)
client.add_result(template, "Template", arg=template)
client.add_result(char_count, "Characters", arg=char_count)
client.add_result(word_count, "Words", arg=word_count)
client.add_result(line_count, "Lines", arg=line_count)
# (use cache=True) to cache result for query instead of old style that below
# if u work with static results (not dynamic; coin price etc.)
# client.cache_response()
if __name__ == "__main__":
WorkflowClient.run(main) # WorkflowClient.run(main, cache=True)
DALLE_2023-11-01_01.20.00_-_Illustration_of_a_triumphant_eagle_reclining_in_its_chair_laptop_beside_it_showcasing_the_Python_logo_signifying_the_completion_of_a_software_projec.png

🪪 License

Copyright 2023 Yunus Emre Ak ~ YEmreAk.com
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
<http://www.apache.org/licenses/LICENSE-2.0>
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
2023 © Yunus Emre AK