mihalski Posted June 14, 2019 Share Posted June 14, 2019 Hi, First post, first workflow I just purchased Alfred 4 on a whim because I had read up on it and a repetitive task happened to be annoying me. I wished to replace the occurrence of a colon in text from the clipboard when pasting. I loaded up the "Clipboard - Getting Started" workflow and stuck a Replace between the Clipboard Contents and Copy to Clipboard objects with Replace: regex (^\\w+)(:\\s)([^\\0]+$) with \1 - \3. This did not work so I also tried replacing with $1 - $3. No dice. Is there something fundamental I am misunderstanding here? I know the regex works and tested it on the suggested site and with other applications. Any help grateful appreciated! Regards, Michal Link to comment
mihalski Posted June 14, 2019 Author Share Posted June 14, 2019 I managed to get a half asses solution going. Using a hotkey I trigger a terminal command that consists of the following python script: #!/usr/bin/env python3 import re, pyperclip text = pyperclip.paste() result = re.sub("(^\\w+)(:\\s)([^\\0]+$)", r"\1 - \3", text) pyperclip.copy(result) This modifies the clipboard and readies it for pasting. Unfortunately it also opens up a terminal window. I wish I knew a way that this could all be done in the background without all the extra work, including the final paste. Link to comment
deanishe Posted June 14, 2019 Share Posted June 14, 2019 1 hour ago, mihalski said: Unfortunately it also opens up a terminal window. The script doesn’t open a terminal window. That’s happening because you’re using Terminal Command. Use a Run Script instead. Link to comment
mihalski Posted June 14, 2019 Author Share Posted June 14, 2019 (edited) The Run Script uses /usr/bin/python though when I want /usr/local/bin/python3. Or do I misunderstand how that works? Edited June 14, 2019 by mihalski Link to comment
deanishe Posted June 14, 2019 Share Posted June 14, 2019 Save your script in the workflow, set the Run Script language to bash, and put this in the script box: /usr/local/bin/python3 script.py Link to comment
mihalski Posted June 14, 2019 Author Share Posted June 14, 2019 Thank you! That makes complete sense and works perfectly. Now is there any way to do an inverse hotkey? ie MY hotkey ctrl-command-v triggers the script and then a command-v to paste? Link to comment
deanishe Posted June 14, 2019 Share Posted June 14, 2019 28 minutes ago, mihalski said: Now is there any way to do an inverse hotkey? ie MY hotkey ctrl-command-v triggers the script and then a command-v to paste? I don’t understand what you mean. What is “doing a Hotkey”? Link to comment
mihalski Posted June 14, 2019 Author Share Posted June 14, 2019 Literally pressing a keyboard combination of keys (ie a hotkey). ie. My desire is for me to press my hotkey, the script is run and then command-v gets triggered. One step process to paste the modified clipboard string. Link to comment
deanishe Posted June 14, 2019 Share Posted June 14, 2019 Right-click > Outputs > Dispatch Key Combo Link to comment
vitor Posted June 14, 2019 Share Posted June 14, 2019 (edited) 9 hours ago, mihalski said: the script is run and then command-v gets triggered. One step process to paste the modified clipboard string. So what you’re looking for isn’t to send a hotkey, but to paste text. You should always describe the desired outcome, not the solution you imagined. Use a Copy to Clipboard Output — it has a checkbox to paste the text. Edited June 14, 2019 by vitor Link to comment
mihalski Posted June 14, 2019 Author Share Posted June 14, 2019 Thanks. You may notice that Copy to Clipboard is where I originally started. Does this mean I have to change the script so that it prints the desired string an output rather than putting it directly into the clipboard? That's my understanding. Link to comment
deanishe Posted June 15, 2019 Share Posted June 15, 2019 8 hours ago, mihalski said: Does this mean I have to change the script so that it prints the desired string an output rather than putting it directly into the clipboard? Yes. You don't need pyperclip at all, in fact (you can use pbpaste to get the clipboard contents), and it would be sensible to use the system Python instead, so your workflow Just Works out-of-the-box without needing developer tools installed. A non-standard runtime is a very heavy dependency for something as tiny as this workflow appears to be. Link to comment
mihalski Posted June 15, 2019 Author Share Posted June 15, 2019 Oh I would absolutely streamline this (and try and figure out why the built in Replace didn't work) if I intended to use it consistently. As is it was only needed 128 times. Having said that, does anyone know what the problem with my original attempt to use regex with Replace was? I will certainly find uses for that if I can get it working. Link to comment
deanishe Posted June 15, 2019 Share Posted June 15, 2019 6 minutes ago, mihalski said: Having said that, does anyone know what the problem with my original attempt to use regex with Replace was? Is this regex pasted verbatim? (^\\w+)(:\\s)([^\\0]+$) If so, then the problem was possibly the double backslashes. Beyond that, we can't really answer questions about a workflow we've never seen. We're just guessing. Whenever you have a question about a workflow that isn't doing what you expect, always post a link to the workflow, so we can see for ourselves. Link to comment
mihalski Posted June 15, 2019 Author Share Posted June 15, 2019 I'm not sure I am doing this correctly (ie sharing my attempt at a workflow) but here goes: https://www.dropbox.com/s/6ke4abwe98rl15v/Clipboard Regex Substitute.alfredworkflow Alfred 4.0.1 macOS 10.14.5 I suspect I simply don't understand how information flows through the workflows. Link to comment
deanishe Posted June 15, 2019 Share Posted June 15, 2019 (edited) Got it. Is this an accurate description of what the workflow is supposed to do? Quote When CTRL-SHIFT-V is pressed takes a string from the clipboard (such as "BASICS: VSCode User Interface KnowHow") and replaces the colon with a dash (resulting in "BASICS - VSCode User Interface KnowHow) and pastes it. If so, can you not just use this? As a rule, you should generally avoid regular expressions unless you really need them. More of a last resort. Quote Some people, when confronted with a problem, think “I know, I'll use regular expressions.” Now they have two problems. Edited June 15, 2019 by deanishe Link to comment
mihalski Posted June 15, 2019 Author Share Posted June 15, 2019 (edited) Wow. Talk about overcomplicating things for myself! If I had understood that Replace can work on SUBstrings as you've demonstrated then I would have not gone the regex way. Thank you! However, there are occasions where I would want to rearrange matched groups and then regex would be the solution? EDIT: Also, this would not handle variable whitespace on either side of the colon would it? Edited June 15, 2019 by mihalski Link to comment
deanishe Posted June 15, 2019 Share Posted June 15, 2019 3 hours ago, mihalski said: However, there are occasions where I would want to rearrange matched groups and then regex would be the solution? EDIT: Also, this would not handle variable whitespace on either side of the colon would it? Both correct. 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