Jump to content

First Workflow: Clipboard RegExp Replace


Recommended Posts

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

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
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
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 by vitor
Link to comment

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

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

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?

 

image.png.8b0106e6f65ed7631f4f30fc089df3ec.png

 

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 by deanishe
Link to comment

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 by mihalski
Link to comment
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

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