GibHenry Councilperson

Joined: Jun 20, 2003 Posts: 104 Location: Birmingham, Alabama; and Amelia (Terni), Italy
|
Posted: Tue Mar 15, 2005 8:30 am Post subject: This AppleScript searches content of Writer and Calc Files! |
|
(*
Courtesy of an unnamed angel on the Apple forums ("biovizier" of Ontario), here is an AppleScript which will search a specified folder and its subfolders for any occurrence of a specified string inside any .sxw or .sxc file.
When I asked how to give him credit, he modestly replied, "The script itself still has a lot of limitations, like that fact that it will choke on various forms of punctuation in the search string, or the way it scans the entire contents of the zip archive, not just the actual 'text', both of which could lead to unexpected results. It's much more important that that info gets passed on..."
Five lines of code have been split for posting. If the script doesn't compile, try deleting the "¬" characters and joining the two lines that each "¬" separates. For best results, set your list view options (cmd-J) to display comments. Save the script in either "/Library/Scripts" or your user "~/Library/Scripts" folder (make one if it doesn't exist), then you can access it from your "Scripts" menu. The "Scripts" menu can be enabled by launching "/Application" > "AppleScript" > "Install Script Menu".
*)
---
property qt : ASCII character 34
set typext to " -regex " & qt & ".*/*sx[cw]" & qt
set searchinfo to (display dialog "Enter search string and choose file type" default answer ¬
"" buttons {"Writer", "Calc", "Writer & Calc"} default button 3)
set thestring to text returned of searchinfo
set thetype to button returned of searchinfo
if thetype is "Writer" then
set typext to " -name " & qt & "*.sxw" & qt
else if thetype is "Calc" then
set typext to " -name " & qt & "*.sxc" & qt
end if
set theFolder to quoted form of POSIX path of (choose folder)
set part1 to "for i in `find " & theFolder & typext & " | sed 's/ /:/g'`;"
set part2 to "do unzip -p " & qt & "`echo " & qt & "$i" & qt & " | tr ':' ' '`" & qt ¬
& " | grep -i " & qt & thestring & qt & " 1>/dev/null;"
set part3 to "if [ $? = 0 ]; then founds=" & qt & ¬
"$founds`echo $i | tr ':' " & qt & " " & qt & "`:" & qt & ";"
set part4 to "fi; done; echo " & qt & "$founds" & qt & " | sed 's@//@/@g'"
set founds to (do shell script part1 & part2 & part3 & part4)
if founds is "" then
display dialog "The string " & qt & thestring & qt & ¬
" was not found" buttons {"Cancel"} default button 1
end if
set my text item delimiters to ":"
set thelist to text items of founds
if (count of thelist) is 2 then
try
tell application "Finder"
activate
reveal (POSIX file (item 1 of thelist) as alias)
end tell
end try
return
end if
set thelistPlus to thelist
set the last item of thelistPlus to "Alias Selected (Default 'All')"
set chosen to (choose from list thelistPlus with prompt ¬
"Choose files to 'reveal'" with multiple selections allowed)
if chosen contains "Alias Selected (Default 'All')" then
if (count of chosen) is 1 then
set chosen to thelist
end if
set cd to (current date)
set resultsf to "Search " & (word 1 of thestring) & "..." & day of cd & time of cd
tell application "Finder"
set resultsfl to (make new folder at desktop with properties {name:resultsf})
try
repeat with afound in chosen
set thisalias to (make alias file to POSIX file afound at resultsfl)
set comment of thisalias to (do shell script "dirname " & qt & afound & qt)
end repeat
end try
open resultsfl
set properties of window 1 to {toolbar visible:(false), current view:(list view)}
set zoomed of window 1 to true
activate
end tell
else
set revealist to {}
try
repeat with eachosen in chosen
set chosenpath to POSIX file eachosen
set revealist to revealist & chosenpath
end repeat
tell application "Finder"
activate
set selecton to items in revealist
reveal revealist
end tell
end try
end if
--- _________________ Gib Henry |
|