Is there any way to find a string in all open TextEdit documents (rather than just one document?) I have hundred plus such open documents, unsaved.
-
I find sublime text more powerful for that. If you have Xcode/ vscode, add all of them to one project and it can search everywhere. If none of them, use grep (can't say about unsaved files there .)– ankiCommented Jun 10, 2020 at 17:08
-
Why wouldn’t you save a document and then use spotlight to index it? I really appreciate you putting this out, but it seems like unless TextEdit is saving the work without asking and you can search in spotlight anyhow this is really something you might change how you work. Great technical question - just not sure it’s practical or would help many. +1 for novelty in my book– bmike ♦Commented Jun 10, 2020 at 17:36
-
1@bmike, It's been my experience with unsaved TextEdit documents that Spotlight doesn't always index them. It seems to be hit or miss. I don't have as many unsaved TextEdit documents as the OP but often have a dozen or so. So the answer I wrote, I saved it as an AppleScript application and will use it if Spotlight does not find something I think is there.– user3439894Commented Jun 10, 2020 at 19:55
1 Answer
The following example AppleScript code when run from Script Editor, or saved as an AppleScript application, will present a search string dialog box for you to type in the search string.
If a match is found, it then creates a new TextEdit document with the name(s) of the document(s) containing the search string. If no match is found, it displays a dialog box with a message.
if running of application "TextEdit" then
tell application "TextEdit"
set docCount to count documents
if docCount is greater than 0 then
set searchString to ""
repeat while searchString is ""
set searchString to my getSearchString()
end repeat
set documentNamesList to {}
repeat with i from 1 to docCount
if text of document i contains the searchString then
copy name of document i to end of documentNamesList
end if
end repeat
if documentNamesList is not {} then
set AppleScript's text item delimiters to linefeed
set documentNamesList to documentNamesList as string
set AppleScript's text item delimiters to ""
set docText to "The following TextEdit documents contain the search string: " & ¬
searchString & linefeed & linefeed & documentNamesList
make new document with properties {text:docText}
activate
else
display dialog "No documents found containing the search string: " & ¬
searchString buttons {"OK"} default button 1 with title "No Match Found"
end if
else
display dialog "There are no open documents to search..." buttons {"OK"} ¬
default button 1 with title "No Open Documents"
end if
end tell
else
display dialog "TextEdit is not open..." buttons {"OK"} default button 1
end if
on getSearchString()
return text returned of (display dialog ¬
"Enter the search string:" default answer ¬
"" buttons {"Cancel", "OK"} default button 2 ¬
with title "Search Open TextEdit Documents")
end getSearchString
Note: The example AppleScript code is just that and does not contain any additional error handling as may be appropriate. The onus is upon the user to add any error handling as may be appropriate, needed or wanted. Have a look at the try statement and error statement in the AppleScript Language Guide. See also, Working with Errors. Additionally, the use of the delay command may be necessary between events where appropriate, e.g. delay 0.5
, with the value of the delay set appropriately.
-
1Brilliant logic, I’m hoping TextEdit always saves the text and spotlight searches it, but I could see spotlight avoiding the temp storage space where these all are saved until OP gives a document a name other than the one that’s generated when a new app opens by the system...– bmike ♦Commented Jun 10, 2020 at 17:37