Jump to content

Find & replace: error checking for failed find


Recommended Posts

Using @Andrew's workflow from this post I discovered that find and replace appears to be case sensitive (which is fine). However, in a workflow I'd need to warn a user if the find word was not found (because of a case error, for example). I've tried a Conditional to match {var:find} to ' ' or to false but nothing seems to pick up the error. How can I trap it, please? (RegEx seems a little bit of a challenge in that context.)

 

Stephen

Link to comment

Sorry Vitor, I am clearly being obtuse, but I can't get that to work anywhere in Andrew's workflow—either when added after storing {var:find} or (which seems less logical, because by then {query} is entirely different, after storing {var:replace}). It just seemed to me that in that workflow I had to match {var:find} to something. The only place I can match it to the initial input ("{query}") is after storing {var:find} and by passing {query} through to the conditional . Am I doing something stupid (quite possible!)?

 

Stephen

Link to comment
  • 1 month later...

I use the following to find and remove duplicate words in Microsoft Word:

 

1. Use wildwards in Microsoft Word advanced find and replace

2. Find

 

(<[A-Z a-z]@)[ ,.;:]@\1>

and Replace with \1

 

 

 

Question:

 

1. How can I use Alfred and AppleScript to do the same on active document or selected word files in finder? My current work around is using vba code below:

 

Sub remove_duplicate_words() Application.ScreenUpdating = False With ActiveDocument.Range   With .Find     .ClearFormatting     .Replacement.ClearFormatting     .Replacement.text = "\1"     .Forward = True     .Wrap = wdFindContinue     .MatchWildcards = True     .text = "([A-Za-z0-9'’]@)[, ]@\1"     .Execute     Do While .Found = True       .Execute Replace:=wdReplaceAll     Loop     .text = "([A-Za-z0-9'’]@[, ]@[A-Za-z0-9'’]@)[, ]@\1"     .Execute     Do While .Found = True       .Execute Replace:=wdReplaceAll     Loop   End With End With Application.ScreenUpdating = True End Sub

 

 2. Is it possible to do this with automation tasks or regex above? 

Link to comment

You could take a look at my workflow that resulted from the original question in this thread (Find and replace in clipboard text). However, be warned that was designed to work with simple text so I strongly suspect it's not going to work with Word files without significant modification. (As I—mercifully—stopped using Word many years ago I'm afraid I can't investigate that further.) The workflow will show the basic building blocks of what you wish to do.

 

Stephen

Link to comment
15 hours ago, TomBenz said:

How can I use Alfred and AppleScript to do the same on active document or selected word files in finder?

 

AppleScript support depends on the app which is automated. But even if Word supports AppleScript, that doesn’t apply to closed files in the Finder.

 

11 hours ago, Stephen_C said:

However, be warned that was designed to work with simple text so I strongly suspect it's not going to work with Word files without significant modification.

 

Quite likely. Word uses a proprietary file format, so you may need a specialised tool to alter it in this way.

 

@TomBenz You should be asking in the Microsoft/Word forums instead. The answer to your question depends entirely on Microsoft having given the ability to Word of being manipulated in that manner. Once there is an answer to that—and assuming it’s doable—we can help you integrate that into Alfred.

Link to comment
On 10/3/2022 at 10:21 PM, vitor said:

 

AppleScript support depends on the app which is automated. But even if Word supports AppleScript, that doesn’t apply to closed files in the Finder.

 

 

Quite likely. Word uses a proprietary file format, so you may need a specialised tool to alter it in this way.

 

@TomBenz You should be asking in the Microsoft/Word forums instead. The answer to your question depends entirely on Microsoft having given the ability to Word of being manipulated in that manner. Once there is an answer to that—and assuming it’s doable—we can help you integrate that into Alfred.

Thanks. I will write AppleScript for it.

 

Following is the find and replace AppleScript for MS word. Need to see how I can use regex to remove duplicates with it.

 

set xFind to "TEXT YOU WANT TO FIND"

set xReplace to "REPLACE TEXT"

 

tell application "Microsoft Word"

tell active document

set numberOfSections to number of sections

log ("••••• There are " & numberOfSections & " in this document.")

repeat with curSectionNum from 1 to numberOfSections

log ("••••• Section: " & curSectionNum & " - Begin search and replace")

tell section curSectionNum

execute find (find object of (text object)) ¬

find text xFind replace with xReplace replace replace all

repeat with whichHeaderFooter in {header footer primary, header footer first page, header footer even pages}

execute find (find object of (text object of (get header index whichHeaderFooter))) ¬

find text xFind replace with xReplace replace replace all

execute find (find object of (text object of (get footer index whichHeaderFooter))) ¬

find text xFind replace with xReplace replace replace all

end repeat

end tell

end repeat

end tell

end tell

Edited by TomBenz
Link to comment
7 minutes ago, TomBenz said:

Thanks. I will write AppleScript for it.

 

 

Wow. This AppleScript does the job of finding and eliminating duplicates. I can launch via Run AppleScript workflow. Let me know if there is a better way to do it via Alfred. Few things:

1. If number of duplicates can be found and prompted in Alfred notifications:

2. Duplicates word such as That that (i.e. case insensitive) can also be removed. I have tried "without match case" but it doesn't work.

 

execute find (find object of (text object)) ¬

find text xFind replace with xReplace replace replace all with match wildcards without match case

 

set xFind to "(<[A-Z a-z]@)[ ,.;:]@\\1>"

set xReplace to "\\1"

set i to 0

 

tell application "Microsoft Word"

tell active document

set numberOfSections to number of sections

log ("••••• There are " & numberOfSections & " in this document.")

repeat with curSectionNum from 1 to numberOfSections

log ("••••• Section: " & curSectionNum & " - Begin search and replace")

tell section curSectionNum

execute find (find object of (text object)) ¬

find text xFind replace with xReplace replace replace all with match wildcards

repeat with whichHeaderFooter in {header footer primary, header footer first page, header footer even pages}

execute find (find object of (text object of (get header index whichHeaderFooter))) ¬

find text xFind replace with xReplace replace replace all

execute find (find object of (text object of (get footer index whichHeaderFooter))) ¬

find text xFind replace with xReplace replace replace all

end repeat

end tell

end repeat

end tell

end tell

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