# Alfred5 icin python ve alfred5 paketi ile Alfred Workflowu Gelistirmek

![DALLE\_2023-11-01\_01.17.10\_-\_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](https://i.imgur.com/5NQSDNF.png)

## 🔰 Alfred5 Workflowu Olusturalim

![2oDMChr.png](https://i.imgur.com/2oDMChr.png)

![IMVWNDm.png](https://i.imgur.com/IMVWNDm.png)

![WicJKBN.png](https://i.imgur.com/WicJKBN.png)

![AwPNT8Y.png](https://i.imgur.com/AwPNT8Y.png)

***

![rR0nFpI.png](https://i.imgur.com/rR0nFpI.png)

## Workflow Gelistirmek icin Proje Dizinini Ayarlayalim

![O6rYLQA.png](https://i.imgur.com/O6rYLQA.png)

* `src` klasörü oluşturun
* Kodlarınızı ve `requirements.txt`'yi `src` klasörüne yerleştirin
* `[alfred5](<https://github.com/yemreak/alfred5>)`'i kurun

  ```shell
  pip3 install alfred5 --target=src/libs

  ```

  <https://github.com/yemreak/alfred5>
* `main.py` dosyasının başına aşağıdaki kodu ekleyin

  ```python
  import sys
  sys.path.insert(0, "src/libs")

  ```

  * Eğer farklı bir `target` kullanmak isterseniz, örneğin `.`, `WorkflowClient.run(packagedir=".")` kullanın
* `vscode` kullanıyorsanız, dosyanızı hata ayıklamak için `.vscode/settings.json`'a aşağıdaki kodu ekleyin

![QeseMTr.png](https://i.imgur.com/QeseMTr.png)

```json
{
    "python.analysis.extraPaths": [
        "./src/libs"
    ],
    "python.analysis.exclude": [
        "./src/libs"
    ]
}

```

⚠️ Eğer tüm gereksinimleri kurduysanız, \`src\` içinde \`requirements.txt\` dosyası oluşturmanıza gerek yoktur.

***

![jYYTykf.png](https://i.imgur.com/jYYTykf.png)

## `alfred5` kullanım senaryolarını anlama

* `SnippetsClient` API ile programlama yoluyla özel kod parçacıkları oluşturun
* `WorkflowClient` API ile özel alfred akışı oluşturun
  * Eğer gerekiyorsa python projeniz için `alfred5`'in onları kurmasını sağlamak için `requirements.txt` dosyası oluşturun 🙃
  * `from requirements.txt`'ten tüm paketleri içe aktarmak için `main` içinde yapın
  * İçe aktarılan paketlere global olarak erişmek için `global` anahtar kelimesini kullanın
    * `client.query` sorgu dizgisidir
    * `client.page_count` sayfalama sonuçları için sayfa sayısıdır
  * `alfred5`'i `requirements.txt`'ye eklemenize gerek yok
* Alfred hata ayıklayıcısına mesajınızı kaydetmek için `WorkflowClient.log` kullanın
  * [alfred akışını hata ayıklama](https://www.alfredapp.com/help/workflows/utilities/debug/)
  * \[bu projenin neden tüm günlük işlemler için stderr kullanıyor]\(<https://www.alfredforum.com/topic/14721/get-the-python-output-back-to-alfred>...

***

![T2FVqD2.png](https://i.imgur.com/T2FVqD2.png)

## ⭐️ Ornek Proje Yapalim

![tUJjVUJ.png](https://i.imgur.com/tUJjVUJ.png)

```python
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)



```

![McjaSni.png](https://i.imgur.com/McjaSni.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.


```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.yemreak.com/arsiv/programming/alfred5-icin-python-ve-alfred5-paketi-ile-alfred-workflowu-gelistirmek.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
