Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save Cdaprod/f68edd9f1d1f29e056bbdcc609fadf49 to your computer and use it in GitHub Desktop.
Save Cdaprod/f68edd9f1d1f29e056bbdcc609fadf49 to your computer and use it in GitHub Desktop.
By following these steps, you can create a powerful iOS Shortcut to automate the creation of GitHub Gists from Markdown content copied to your clipboard.

Creating an iOS Shortcut to create a GitHub Gist from the Markdown content in your clipboard involves several steps. Here’s a guide to accomplish this:

  1. Generate a GitHub Personal Access Token:

    • Ensure you have a GitHub Personal Access Token with the gist scope.
  2. Prepare the Script:

    • Use a Python script that takes input from the clipboard and creates a Gist. This script can be run using a web service like PythonAnywhere or a similar service that can execute scripts via a URL.
  3. Create the iOS Shortcut:

    • Use the Shortcuts app on iOS to create a new shortcut that copies the clipboard content and sends it to your script via a web request.

Step-by-Step Instructions

Step 1: Create a Python Script on PythonAnywhere

  1. Sign up or log in to PythonAnywhere.

  2. Create a new Python file with the following content:

    from flask import Flask, request, jsonify
    import requests
    import json
    
    app = Flask(__name__)
    
    # Replace with your GitHub Personal Access Token
    GITHUB_TOKEN = 'your_personal_access_token'
    
    @app.route('/create_gist', methods=['POST'])
    def create_gist():
        content = request.json.get('content')
        description = request.json.get('description', 'Gist created via iOS Shortcut')
        gist_data = {
            "description": description,
            "public": True,
            "files": {
                "file.md": {
                    "content": content
                }
            }
        }
    
        headers = {
            'Authorization': f'token {GITHUB_TOKEN}',
            'Accept': 'application/vnd.github.v3+json'
        }
    
        response = requests.post('https://api.github.com/gists', headers=headers, data=json.dumps(gist_data))
    
        if response.status_code == 201:
            return jsonify({"success": True, "url": response.json()['html_url']})
        else:
            return jsonify({"success": False, "error": response.content}), response.status_code
    
    if __name__ == '__main__':
        app.run(host='0.0.0.0', port=5000)
  3. Save the file and configure the web app to run this script on PythonAnywhere.

Step 2: Create the iOS Shortcut

  1. Open the Shortcuts app on your iOS device.

  2. Create a new shortcut by tapping the "+" icon.

  3. Add the "Get Clipboard" action:

    • Search for "Get Clipboard" and add it to your shortcut.
  4. Add the "Text" action:

    • Add the "Text" action and set it to a JSON template. Use the following template:

      {
          "content": "clipboard text"
      }
  5. Add the "Replace Text" action:

    • Replace "clipboard text" with the variable from the "Get Clipboard" action.
  6. Add the "URL" action:

    • Set the URL to the endpoint of your PythonAnywhere web app, e.g., https://yourusername.pythonanywhere.com/create_gist.
  7. Add the "Get Contents of URL" action:

    • Configure it to use the POST method and set the request body type to JSON.
    • Set the JSON body to the variable from the "Replace Text" action.
  8. Add the "Show Result" action:

    • Show the result of the "Get Contents of URL" action.
  9. Name your shortcut and save it.

Example Shortcut Steps

  1. Get Clipboard
  2. Text:
    {
        "content": "clipboard text"
    }
  3. Replace Text:
    • Replace "clipboard text" with the clipboard variable.
  4. URL:
    • https://yourusername.pythonanywhere.com/create_gist
  5. Get Contents of URL:
    • Method: POST
    • Headers: Content-Type: application/json
    • Request Body: JSON
    • Body: Variable from "Replace Text"
  6. Show Result

Running the Shortcut

Now, when you copy Markdown text to your clipboard and run this shortcut, it will create a Gist on GitHub with the content of your clipboard.

By following these steps, you can create a powerful iOS Shortcut to automate the creation of GitHub Gists from Markdown content copied to your clipboard.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment