Jump to content

How to run a python script from alfred?


Recommended Posts

Hey all!

 

This is a newbie question, thanks in advanced for you patience. I have written a script that restructures a specific URL on python. It works well in Pycharm. 

 

I am wondering how I can use alfred to run this script. Happy to do the work of reading and learning, but after hours of googling I have come up mostly empty handed.  Code below incase it's relevant.

 

import pyperclip
text = pyperclip.paste()
link = text
pt_1, Pt_2 = link.split("#")
notion, erase, delete = ([pt_1[i:i+22] for i in range(0, len(pt_1), 22)])
confirm = ('{}{}'.format(notion, Pt_2))
pyperclip.copy(confirm)

 

Link to comment

New user here, so I can't be the most help.... but you definitely want to create a workflow plugin for Alfred. 

If you go to Alfred's settings > Workflows and then click the + sign you can create a new workflow. I suggest you start from the blank template. After you give it a name and the like like, if you right click your new plugin itself in the left most listings where all the plugins are shown, you can select "open in finder" to reveal the folder where you plugin is. This will let you embed your script files as needed (like your *.py file in this case. 

 

Now the juicy part. Think of workflows as something like an automator action or iOS shortcut where you can create a flow of things visually to accomplish your task. The big blank space to your right is your canvas. Simply right click it and you'll be able to added the comments you think link up visually to accomplish the task you want.

 

Based on your simple needs, I think you may just need the "run script" workflow option which you'll find under actions when you right click the blank space. It runs whatever shell command you type in there as if from the root of the folder of the workflow, so basically: 

 

/usr/bin/python *.py should be all you need. If you want to be able to supply arguments based on what you type in Alfred, there is an option for that as well.... but I think you'll want to use a script filter first based on the plugins I see. It seems like this allows you to separate the keyword/arguments as otherwise EVERYTHING types in the search bar would be supplied as arguments to the script. Look at the reddit plugin as a simple example to get an idea as it's super easy to figure out/reverse engineer how to from that one.

 

Good luck!

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