Re: Need help with script
Re: Need help with script
- Subject: Re: Need help with script
- From: Kai Edwards <email@hidden>
- Date: Sat, 28 Dec 2002 03:12:07 +0000
on Thu, 19 Dec 2002 13:19:43 -0500, Paul Ricci <email@hidden>
wrote:
>
I have never used applescript but feel I have a need for it at the moment so
>
if someone is able to do this for me I would greatly appreciate it.
>
>
I have a folder with some text files in it. I need a script that would open
>
each file one at a time, look for a certain word (it could change so if need
>
I can just change the code) and if it appears, leave it and if it does not,
>
put it in the trash.
>
>
If this is easy and someone could post something for me I would be very
>
thankful. If it is not and payment is required to do this, I will see what
>
I can do.
I don't know if you're still looking for something, Paul, but I just noticed
your message while catching up on some outstanding, pre-holiday digests.
Anyway, I've cobbled together a script which seems to do roughly what you
describe. Sorry if it's a bit on the lengthy side. I sacrificed some brevity
for (hopefully) greater clarity, more descriptive variable names - and a few
extra subroutines for good measure. I hope they give you some ideas to help
you get started on writing your own stuff.
Since the script is pretty basic 'vanilla', it should work on various system
versions - or require minimal tweaking at most. Try this:
=======================================
property folderPath : ""
property searchString : "Enter text"
property folderPref : "Selected Folder"
property searchPref : "Any Occurrence"
to getFolderPath()
if folderPath is not "" then
display dialog "Search for files in folder:" & return & [NO BREAK]
return & "\"" & folderPath & "\"" buttons {"Cancel", [NO BREAK]
"Change Folder", "Selected Folder"} default button folderPref with icon 1
set folderPref to result's button returned
if folderPref is "Selected Folder" then return folderPath
end if
changeFolderPath()
end getFolderPath
to changeFolderPath()
choose folder with prompt "Choose a folder to search"
if result is not "" then return result as string
end changeFolderPath
to getFileList()
tell application "Finder"
try
set fileList to folder folderPath's files as alias list
on error number -1700 -- (can't make single item list into alias list)
set fileList to folder folderPath's files's item 1 as alias as list
end try
end tell
if fileList is not {} then return fileList
beep
display dialog "There are no files there." buttons [NO BREAK]
"Cancel" default button 1 with icon 0
end getFileList
to getSearchString()
set {enteredString, searchPref} to (display dialog [NO BREAK]
"Search for files that don't contain:" default answer [NO BREAK]
searchString buttons {"Cancel", "Whole Words", [NO BREAK]
"Any Occurrence"} default button [NO BREAK]
searchPref with icon 1)'s {text returned, button returned}
if enteredString is not "" then return enteredString
beep
getSearchString("Please enter some text - or click 'Cancel'.")
end getSearchString
to getTrashList from fileList
set {chunkMax, trashList} to {32766, {}}
set chunkStep to chunkMax + 1 - (count searchString)
repeat with aFile in fileList
set {openFile, lo} to {open for access aFile, 1}
set {hi, max} to {lo + chunkMax, get eof openFile}
if max = 0 then
set textFound to false
else
repeat
if hi > max then set hi to max
set fileText to read openFile from lo to hi
set textFound to findTextIn(fileText)
if textFound or hi = max then exit repeat
set {lo, hi} to {lo + chunkStep, hi + chunkStep}
end repeat
end if
close access aFile
if not textFound then set trashList's end to aFile
end repeat
if trashList is not {} then return trashList
beep
display dialog "No files were found." buttons [NO BREAK]
"Cancel" default button 1 with icon 0
end getTrashList
to findTextIn(fileText)
if searchPref is "Any Occurrence" then [NO BREAK]
return searchString is in fileText
if fileText is searchString then return true
set {aStr, bStr} to [NO BREAK]
{{space, tab, return, "/"}, {".", "?", "!", ":", ";"}}
repeat with startString in aStr
if fileText ends with startString & searchString then return true
repeat with endString in aStr & bStr
tell searchString & endString to if fileText [NO BREAK]
starts with it or startString & it is in fileText then return true
end repeat
end repeat
false
end findTextIn
to trashItems from trashList
set {dlog, finderStatus} to [NO BREAK]
{getDialog for trashList, getFinderStatus()}
tell application "Finder"
select trashList
beep
set trashPref to (display dialog dlog's [NO BREAK]
msg buttons dlog's btn default button [NO BREAK]
3 with icon 2)'s button returned
if trashPref contains "Trash" then delete trashList
if trashPref contains "Empty" then empty
end tell
restore(finderStatus)
end trashItems
to getFinderStatus()
tell application "Finder"
set targetWindow to folder folderPath's container window
set notOpen to not (exists targetWindow) or targetWindow's popup
activate
if notOpen then open targetWindow
set {windowIndex, windowZoomable, windowZoomed} to [NO BREAK]
targetWindow's {index, zoomable, zoomed}
if windowIndex is not 1 then set targetWindow's index to 1
set notZoomed to windowZoomable and not windowZoomed
if notZoomed then set targetWindow's zoomed to true
set finderSelection to selection
end tell
{targetWindow, notOpen, notZoomed, windowIndex, finderSelection}
end getFinderStatus
to restore(finderStatus)
set {targetWindow, notOpen, notZoomed, [NO BREAK]
windowIndex, finderSelection} to finderStatus
tell application "Finder"
try
select finderSelection
end try
if notZoomed then set targetWindow's zoomed to false
if notOpen then
close targetWindow
else if windowIndex is not 1 then
set targetWindow's index to windowIndex
end if
end tell
end restore
to getDialog for trashList
set trashCount to count trashList
{msg:"Trash the " & (trashCount & " file" & getS(trashCount) & [NO BREAK]
" not containing " & getT() & " \"" & searchString & "\"?")} & [NO BREAK]
{btn:{"Trash & Empty", "Trash Only", " Cancel "}}
end getDialog
to getT()
if searchPref is "Any Occurrence" then return "any occurrence of"
"the word" & getS(count searchString's words)
end getT
to getS(num)
if num = 1 then return ""
"s"
end getS
set folderPath to getFolderPath()
set fileList to getFileList()
set searchString to getSearchString()
set trashList to getTrashList from fileList
trashItems from trashList
=======================================
Let me know if you have any problems or queries.
--
Kai
_______________________________________________
applescript-users mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/applescript-users
Do not post admin requests to the list. They will be ignored.