Jump to content

Find & Replace Text from Clipboard using the Replace Utility (or any other)?

Recommended Posts

Is there an easy way to use Alfred's Replace utility dynamically, so that it identifies the text that it's finding and replacing from arguments after a keyword?


Let me explain using an example. For research purposes, I often copy and paste large amounts of text into Google Translate from very old public domain books. Because of their scan quality and OCR-related problems, it's not uncommon for the text to exhibit patterns that need to be corrected. These are patterns that are specific to the text (i.e., not ones that justify creating a permanent workflow for them). For example, in one text I copied this morning, the OCR'd text believes that the letter "p" is actually the letters "jj."


To correct this problem using text on the clipboard, it'd be great if I could use a workflow (1) that was triggered by a keyword, (2) that searched for whatever string I specified inside a set of quotation marks as the first argument, and (3) that replaced all instances of the string with another string specified inside a second set of quotation marks as the second argument. As a result, the next time I paste the text from the clipboard, it should paste the new cleaned up version. 


Using the text example from above - which errantly includes the letters jj where the letter p should be - the workflow might operate like this in Alfred's search bar:




In layman's terms (and, as you can tell from this post - I'm a layman), the workflow would effectively place the strings in Alfred's Replace utility as follows (making sure to ignore the quotation marks and to only include text/punctuation/spaces/etc. that are included within the quotation marks):




To be clear, I do not intend to place the text in the Replace utility permanently. This is just how I envision the workflow operating, using the example text from above. The important point is that the workflow responds - dynamically - to whatever's in the two arguments that follow the keyword (and, in this case, it's important that it actually ignore the quotation marks).


I'm not married to this method either. So, if you know a better way to accomplish this task or if you know of a workflow that can already accomplish it, I'd love to hear them! Thanks for any assistance you can lend! I really appreciate it.

Edited by Jasondm007
Link to comment

You can configure most (all?) workflow elements dynamically with a JSON Config utility immediately before the element you want to configure.


Right--click on the element you want to dynamically configure and select Copy Configuration. Add a JSON Config element immediately before it, paste the configuration into the box, and edit it to your heart's content. You can use {var:VARNAME} in the JSON, which is how you get your dynamic values in there:

  "alfredworkflow" : {
    "config" : {
      "matchmode" : 0,
      "matchstring" : "{var:old}",
      "replacestring" : "{var:new}"


Link to comment

Thanks a ton @deanishe


I'm sure this is a stupid question, but how do I get the two arguments after the keyword to be assigned as variables? So, using the original example (Alfred Search: fr "jj" "p"), how do I get jj assigned as variable 1 and p assigned as variable 2 (before passing them to the JSON Config element)?


I noticed another thread that uses a Run Script element to do something somewhat similar. Is this the route you'd suggest? Thanks again!!

Link to comment

You have two choices. Either you write a script that parses the input into two arguments and sets the variables, or you chain two separate Keyword inputs (one to get the text to replace and one to get the text to replace it with).


The former is probably going to be easier to use in most cases, but the latter is easier to implement and more correct: If you parse one input into two values using a script, how do you handle the situation when one of the values contains the input delimiter?

Link to comment
1 hour ago, Jasondm007 said:


Glad to hear the setup is correct



I don’t think it was: there’s no input for the text the replace should be performed on. But after I added that, it still didn’t work.


I’ve not used JSON Config much myself, so it’s possible I haven’t understood it. Perhaps @Vero could have a look to see if we’re doing it right.

Link to comment

@Jasondm007 There are a few things going on here. Firstly, the Replace util should work fine when passing in a JSON configuration (Just tested this, not a bug), but in your workflow, you are passing matchmode as 1 (REGEX) instead of 0 (String), so things won't be processed as you expect.


Secondly, @deanishe is correct that there is no actual input query being passed in to process the find and replace on, so you are essentially asking Alfred to do a find and replace of no string at all. If you want to act on whatever is in the clipboard, you could add {clipboard} as the argument in the Arg / Var utility before the Find and Replace.


It's also worth noting that when it comes to the find / replace, you don't actually need to configure it with JSON, you can configure it inline like this:


Screen Shot 2018-07-18 at 11.05.15.png


Here is your workflow (simplified) which does the find and replace based on your given arguments for the current contents of the clipboard:

https://www.dropbox.com/s/mxxkiggrqa8gy1o/Text - Find %26 Replace.alfredworkflow?dl=0




Link to comment
11 minutes ago, Andrew said:

Firstly, the Replace util should work fine when passing in a JSON configuration (Just tested this, not a bug)


Could you upload your workflow for me to have a look? I did change the matchmode and added {clipboard} as the input in my version, but it still didn't work :(


Edited by deanishe
Link to comment
  • 8 months later...
On 3/30/2019 at 1:15 PM, Bhishan said:

@Jasondm007   I was also trying to achieve the same.

I found your link useful but the link is broken, may be you can share it in github  or bitbucket so that it will remain infinitely!




@Bhishan I've implemented this in a much larger workflow, so I'd recommend downloading @Andrew's posted above (July 18, 2018) - which fixes the original workflow:




I just checked, and his Dropbox upload still works.

Edited by Jasondm007
Fixed URL from Quoted Text
Link to comment
  • 3 years later...

Ok, now how would i go about inputting multiple variables "find"


ie, I'd like to change "c2", "c3", "c4".... to all "c1"


{{c2::optic nerve }}
{{c5::Third nerve}}
{{c6::Ciliary ganglion}}
{{c7::sphincter light reflex}}

Link to comment
  • 2 weeks later...

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