PB-Panda Posted January 4 Share Posted January 4 Hi Everyone, I am trying to make an Alfred workflow that does the following 1. Is triggered via a keyboard shortcut 2. Takes the currently selected text as input 3. Feeds that text into a Python script to do some basic processing (removing spaces, changing capitalization, etc) 4. Pastes that modified text in place of the selected text Based on the above requirements, I do not think the script filter tool will work well because that requires calling up Alfred's dialog box, but I am having trouble outputting the result of the Python script into something Alfred can work with. I have tried different Python outputs, including the `sys.stdout.write` recommended on other forums. The current Python script (no processing, just testing) in which I tried to use a JSON object is below. I believe JSON objects are only for script filters though, and the return line also appears to do nothing helpful. import sys import json items = { "type": "text", "title": "Result", "arg": "Test_Text" } sys.stdout.write(json.dumps(items)) return json.dumps(items) Is there an easy way to get Alfred to read the Python output, or a different way to achieve what I want. I would prefer Python to do the heavy lifting even though the script is simple because it's just more portable, but a working script is better than nothing obviously. Thanks for your help. Workflow and clipboard modules attached. Link to comment
giovanni Posted January 4 Share Posted January 4 Hi @PB-Panda welcome to the forum! You are correct, a script filter is not needed, and neither is a JSON output. You just need to print (or sys.stdout.write) your output so that it becomes available to the next item in the workflow. This is a similar workflow to which I have added a space removal universal action using Python, which should be helpful to you as a template. I hope this helps! vitor 1 Link to comment
PB-Panda Posted January 5 Author Share Posted January 5 Hi @giovanni, Thank you for the example! The issue turned out to be with my Python installation, and I will leave a breadcrumb trail here in case it helps anyone else. 1. After realizing giovanni's Python script did not work but the rest of them in their workflow (zsh scripts) did, I chose to research why Alfred may not be able to run Python in general. I also created a Python script that created a file on the desktop so I could be sure it wasn't a data extraction to Alfred problem. (Also, my Python scripts run fine from the command line or an IDE) 2. Based on this forum post and memory that I may have installed Python on my system with an alternative path variable (before I really knew what I was doing probably), I decided a reinstall of Python might be in order. 3. Followed the steps laid out by this forum post to reinstall Python3. (I already had HomeBrew installed). 4. Alfred now runs Python scripts that are created within the workflow itself. I still cannot run an external script (it appears to not do anything), but at least I have one way to make what I want work. Thanks again! My final Python script: import sys # Constants TEXT_HEADING = "\\text{" TEXT_TRAILING = "}" output = TEXT_HEADING + sys.argv[1] + TEXT_TRAILING sys.stdout.write(output) Link to comment
vitor Posted January 5 Share Posted January 5 17 hours ago, PB-Panda said: I still cannot run an external script You may be missing the shebang. 17 hours ago, PB-Panda said: My final Python script Note you don’t need Python for that, you could use a Replace Utility: Replace [regex] (.+) with \\\\text{$1} Link to comment
PB-Panda Posted January 5 Author Share Posted January 5 That was it @vitor—not sure how I missed that detail before. Thanks! I've been dipping my toes in to regex lately, and it looks interesting. I'm trying to keep things in Python (or another programming language) for portability outside of Alfred in case something catastrophic happened to the company, but I appreciate the suggestion! Link to comment
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now