Jump to content

Help: Workflow to Extract and Display Links from Markdown Files


Recommended Posts

Posted

Overview:
I'm creating a workflow to extract all links (including x-devonthink-item:// deep links) from a Markdown file
and display them in Alfred for selection. Despite extensive testing, the links do not appear in Alfred,
although the script works perfectly in the terminal.

 

Goal:

• Input: Select a Markdown file via a File Filter in Alfred.

• Output: Display all links in the file as individual Alfred items with actionable links.

 

Setup:

1. File Filter:

• Scope: Targets Markdown files in a specific folder.

• Output: Passes the selected file path to the next step.

2. Run Script (Python 3):

import sys
import re
import json
from pathlib import Path

def extract_links(file_path):
    try:
        content = Path(file_path).read_text()
        links = re.findall(r"(https?://[^\s]+|x-devonthink-item://[^\s]+)", content)
        return {
            "items": [
                {
                    "title": link,
                    "subtitle": "Press Enter to open this link",
                    "arg": link,
                    "valid": True
                }
                for link in links
            ]
        }
    except Exception as e:
        return {"items": [{"title": "Error", "subtitle": str(e), "valid": False}]}

if __name__ == "__main__":
    if len(sys.argv) > 1:
        file_path = sys.argv[1]
        print(json.dumps(extract_links(file_path)))
    else:
        print(json.dumps({
            "items": [{"title": "No file specified", "subtitle": "Please select a valid file", "valid": False}]
        }))

3. Open URL or Copy to Clipboard:

• Input: {query} from the script.

• Expected to open the selected link or copy it to the clipboard.

 

Issues Faced:

• The workflow passes the file path from the File Filter to the Run Script.

• The Python script correctly processes the file and outputs valid JSON when run in the terminal.

• However, Alfred does not display the results as actionable items. Not at all, just disappears. 

 

What I've Tried:

1. Debugging the workflow in Alfred:

• The Debugger confirms the file path is passed to the script.

• No errors are displayed, but Alfred doesn't show any results.

2. Simplified the script to output hardcoded JSON:

• Even hardcoded JSON fails to display in Alfred.

3. Confirmed the JSON structure meets Alfred's requirements:

2024_12.06---AlfredPreferences.png.17b9d0da02a0229ce857f9128eb7965a.png2024_12.06---AlfredPreferences.png.8cecc19d4565d63807afdc93d080a8da.png

2024_12.06---AlfredPreferences.png.1cc2de9002a9aadbe9524ba61f02a173.png2024_12.06---CleanShot.png.c1fd0b2a472c68f1ebc40adc8e0385fc.png

2024.12.06 -  -  - [Alfred Preferences].png

Posted

I feel dumb now hahaha. That was so easy. It is my first workflow tho. 🤷
Thank you so much, appreciate the help. 

Posted

It seems to otherwise be going well already, the other objects make sense where they are, looks like a good and useful first workflow. Keep it up, we’re here to help if you have more questions.

Posted

Actually I do have another question since we are here. 

Is there any way to have my index results show numerically? Have tried scripting but not quite successfully. 
Thank you 
2024_12.06---LogiOptions.thumb.png.90f9b33f5a459b538b6df11f7e4980d0.png

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...