Jump to content

How do I Pass Users Configurable Variables into a Python 3 Script

Recommended Posts

Hello! I am a newbie to writing my own scripts/Alfred workflows, even though I have used Alfred for a long while.


I have been searching everywhere for how to pass a variable in Alfred to a variable in a Python script, but I couldn't find anything. I figured this was the best place to ask.


This is the script I wrote to add {query} to a specific line in a file, I personally use it for logging in my daily note in Obsidian.



import sys
import os.path
import time

# Convert Alfred variables to Python
query = sys.argv[1]

# Create "save_path" variable for Daily Notes folder
save_path = "/Users/jackverstraate/Obsidian/Daily Notes/"

# Create "file_name" variable with YYYY-MM-DD format
file_name = str((time.strftime("%Y-%m-%d")))

# Join "save_path" and "file_name" to create completeName
completeName = os.path.join(save_path, file_name) + ".md"

# Count the number of lines in a file to create variable "line_count"
with open(completeName, 'r') as fp:
    for count, line in enumerate(fp):
line_count = (count + 1)

# Read the contents of a file to create variable "contents"
with open(completeName, "r") as f:
    contents = f.readlines()

# Insert "query" at "line_count" minus 22 with current time
contents.insert((line_count) - 22, "- **" + (time.strftime("%I:%M %p")) + "** – " + query + "\n")

with open(completeName, "w") as f:
    contents = "".join(contents)


I want the variable "save_path" to be passed from Alfred instead of manually typing a string for the user path. Can you help me out?

Link to comment

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...