Jasondm007 Posted July 16, 2018 Share Posted July 16, 2018 (edited) 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 July 16, 2018 by Jasondm007 Link to comment
deanishe Posted July 16, 2018 Share Posted July 16, 2018 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
Jasondm007 Posted July 16, 2018 Author Share Posted July 16, 2018 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
deanishe Posted July 16, 2018 Share Posted July 16, 2018 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
Jasondm007 Posted July 17, 2018 Author Share Posted July 17, 2018 Thanks @deanishe ! I opted for the easier/safer route, and have built the following workflow: Unfortunately, it's still not working just quite. I know I've already bothered you enough, but if you have a second, could you take a look and let me know what I'm missing? I've uploaded it here: Alfred workflow. Thanks again! OSchrock 1 Link to comment
deanishe Posted July 17, 2018 Share Posted July 17, 2018 The Replace utility doesn't appear to work correctly with JSON Config. Looks like a bug with Alfred Link to comment
Jasondm007 Posted July 17, 2018 Author Share Posted July 17, 2018 Glad to hear the setup is correct. I'll report the bug. Thanks again for all of your help, @deanishe! Link to comment
deanishe Posted July 17, 2018 Share Posted July 17, 2018 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
Andrew Posted July 18, 2018 Share Posted July 18, 2018 @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: 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 Cheers, Andrew zlc1952 and Ethan Hansen 1 1 Link to comment
deanishe Posted July 18, 2018 Share Posted July 18, 2018 (edited) 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 July 18, 2018 by deanishe Link to comment
Andrew Posted July 18, 2018 Share Posted July 18, 2018 @deanishe with the original download, just change the JSON config to: { "alfredworkflow" : { "arg" : "{clipboard}", "config" : { "matchmode" : 0, "matchstring" : "{var:find}", "replacestring" : "{var:replace}" } } } ... and it should work as expected. deanishe 1 Link to comment
deanishe Posted July 18, 2018 Share Posted July 18, 2018 Ah. I didn't set arg via JSON, but in the Args & Vars where I set {var:replace}. Thanks. Link to comment
Andrew Posted July 18, 2018 Share Posted July 18, 2018 @deanishe for that to work, you'd need to make the JSON utility pass through {query} as the arg. deanishe 1 Link to comment
Jasondm007 Posted July 18, 2018 Author Share Posted July 18, 2018 @Andrew & @deanishe - This is why Alfred is the best thing about using a Mac. You guys are the best! Thanks a ton! Andrew 1 Link to comment
Bhishan Posted March 30, 2019 Share Posted March 30, 2019 @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! Thanks. Link to comment
Jasondm007 Posted April 2, 2019 Author Share Posted April 2, 2019 (edited) 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! Thanks. @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: Quote https://www.dropbox.com/s/mxxkiggrqa8gy1o/Text - Find %26 Replace.alfredworkflow?dl=0 I just checked, and his Dropbox upload still works. Edited April 2, 2019 by Jasondm007 Fixed URL from Quoted Text Bhishan 1 Link to comment
Bhishan Posted April 5, 2019 Share Posted April 5, 2019 Nevermind, I can confirm it works, I was just confused by dropbox log in request. Link to comment
zlc1952 Posted July 14, 2022 Share Posted July 14, 2022 Ok, now how would i go about inputting multiple variables "find" ie, I'd like to change "c2", "c3", "c4".... to all "c1" {{c1::retina}}{{c2::optic nerve }}{{c3::pretectal}}{{c4::Edinger-Westphal}}{{c5::Third nerve}}{{c6::Ciliary ganglion}}{{c7::sphincter light reflex}} Link to comment
zlc1952 Posted July 14, 2022 Share Posted July 14, 2022 I tried using "c*" but i think it took it very literally. (also, I am using the dropbox workflow) Link to comment
vitor Posted July 14, 2022 Share Posted July 14, 2022 @zlc1952 Seems like you’re looking for regular expressions. With the Replace Utility, set it to Replace regex c\d+ with c1. The regular expression c\d+ means “the letter c, followed by one or more numbers”. zlc1952 1 Link to comment
zlc1952 Posted July 27, 2022 Share Posted July 27, 2022 Thank you @vitor, you literally are so helpful! Thanks for all you do! Also, I'm learning more about regex now! Really interesting stuff, I think learning it will really benefit me in all areas! vitor 1 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