Jump to content
mihalski

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

Share this post


Link to post

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.

Share this post


Link to post
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.

Share this post


Link to post
Posted (edited)

The Run Script uses /usr/bin/python though when I want /usr/local/bin/python3.

 

Or do I misunderstand how that works?

Edited by mihalski

Share this post


Link to post

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

Share this post


Link to post

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?

Share this post


Link to post
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”?

Share this post


Link to post

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.

Share this post


Link to post
Posted (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 by vitor

Share this post


Link to post

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.

Share this post


Link to post
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.

Share this post


Link to post

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.

Share this post


Link to post
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.

Share this post


Link to post
Posted (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?

 

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

Share this post


Link to post
Posted (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 by mihalski

Share this post


Link to post
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.

Share this post


Link to post

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...