Jump to content

Find and replace in clipboard text


Recommended Posts

With acknowledgments to @Andrew (because I used his find and replace workflow) and to @vitor (without whom there would have been no working error trapping) this is a workflow that will allow you to find and replace a word or phrase in text already on the clipboard and, optionally, copy the revised text to a text file for spellchecking.

 

I'm unsure how useful it will be but It was created to while away a few idle hours and as a partial response to this post.

 

Read Me

 

Download

 

Stephen

Link to comment

You can remove the 2fr Keyword (the text, not the object), since it shouldn’t be called on its own.


As for the user-configurable Keyword, a few changes are needed for it to work (right now it won’t do anything):

  • Add a Variable name (e.g. startKeyword) under the User Configuration, as well as a Default Value (fr).
  • In the fr Keyword object, replace the Keyword text with {var:startKeyword}.

Finally—and I suggest this as an exercise if you’re interested in exploring the solution—with a Universal Action Trigger you can make this work on any text (not just the current one in the clipboard).

Link to comment

Thanks: so annoyed with myself for not making the keyword configuration work properly. However, it now does and this and a couple of other workflows also containing the same aberration have all been updated on Github. Other comment (about 2fr keyword) taken on board and changed.

 

Will look at adding a Universal Action trigger tomorrow after some sleep.

 

In passing, how the dickens do you get in-line code highlighting in this board's editor? I have grappled and failed miserably.

 

Stephen

Link to comment
2 hours ago, Stephen_C said:

so annoyed with myself for not making the keyword configuration work properly.

 

Don’t be! It’s part of the process and you’re doing great! I like seeing what you come up with and how each Workflow gives you something else to tackle. I hope the progress is fun for you, even if sometimes it can feel frustrating. As the joke goes:

 

“I hate programming

I hate programming

I hate programming

It works!

I love programming”

 

2 hours ago, Stephen_C said:

how the dickens do you get in-line code highlighting in this board's editor?


It’s my own configuration with MarkdownTransform.

 

Edited by vitor
Link to comment
  • 1 month later...

hey @Stephen_C this workflow is great. I ran into one thing that might be considered an edge case. I had a ton of text where I wanted to remove a bunch of en dashes but i couldn't initially replace them with nothing because the "replace" keyword required an argument. I changed it to optional and it did the trick. I don't know if that is something you might want to update as most find and replace functions allow for the replace field to be blank.

 

 

Edited by sepulchra
Link to comment
18 hours ago, TomBenz said:

Two questions related to it:

Sorry, neither of those are possible currently and I can't at the moment envisage having the time to experiment and try to add them (even if that were a simple exercise—which I suspect it is not).

 

Edit: do bear in mind we are dealing with plain text here so I suspect the second request is impractical. The first is probably feasible but requires more work than I currently have time to devote: sorry.

 

Stephen

Edited by Stephen_C
Clarification + typo
Link to comment
4 hours ago, Stephen_C said:

Sorry, neither of those are possible currently and I can't at the moment enviage having the time to experiment and try to add them (even if that were a simple exercise—which I suspect it is not).

 

Edit: do bear in mind we are dealing with plain text here so I suspect the second request is impractical. The first is probably feasible but requires more work than I currently have time to devote: sorry.

 

Stephen

Ok Stephen. Thanks for your input. See the code below on item 1 and 2 and see if these make any sense.

 

Item 1 -- this regex works but I still need to understand and test it more. I don't understand scripting additions syntax yet.

 

use framework "Foundation"

use scripting additions

--------------------------------------------------------------------------------

 

tell application "TextEdit"

set workingWin to document 1

set this_text to the text of the workingWin

set input to this_text -- "The crow flies over the house."

set regex to ".*-.*-.*-.*-.*"

set uptext to re_match of me from the input against regex given replacement:""

 

set the text of the workingWin to uptext

end tell

--------------------------------------------------------------------------------

on re_match against pattern from str given replacement:fmt

try

set regex to current application's NSRegularExpression's ¬

regularExpressionWithPattern:pattern ¬

options:(current application's ¬

NSRegularExpressionCaseInsensitive) ¬

|error|:(missing value)

log pattern

set updatedtext to (regex's stringByReplacingMatchesInString:str ¬

options:0 range:{0, length of str} ¬

withTemplate:fmt) ¬

as text

on error

set updatedtext to str

end try

return updatedtext

end re_match

 

Item 2 -- this find specific text and mark it in red colour.

 

tell application "TextEdit"

set xword to "yep"

set aRef to a reference to (every word of document 1 where it is xword)

set aRef's color to {65535, 0, 0}

--set aRef's contents to ""

end tell

 

 

Link to comment

Thanks for the ideas. However, my workflow is a fairly basic, simple workflow and I am rather inclined to keep it that way. I don't really wish to include in it some rather impenetrable AppleScript 😀 (even if I'm relatively happy with AppleScript). I also suspect there is a rather easier method of incorporating RegEx (see, for example, my Double space to single space workflow). However, that would involve creating an option at the start of this workflow (e.g., Do you wish to use RegEx?) which, to my mind, would be an irritating distraction from the simplicity of the existing workflow.

 

Nevertheless you are, of course, most welcome to take my workflow and develop it to suit your own needs.

 

Stephen

Link to comment
3 hours ago, TomBenz said:

I see if I can modify the double-space workflow and adapt it to my use case.

I'd be inclined to do it the other way around: copy the Find and replace in clipboard text workflow and then pretty much all you will have to do is to the change the Replace action (annotated as Run find & replace on clipboard text in the workflow) to use RegEx replace rather than string. That, at least, is the basic alteration I think is needed.

 

Stephen

Link to comment
  • 1 month later...
  • 8 months 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...