Hello!
This script is really made for finding some stuff, you know you did within some days based on some criteria, like some text in it or not.
Try it, I think you’ll like it!
-- Debugged and enhanced 14/08/12
-- After idea by Adam Bell
--http://macscripter.net/viewtopic.php?id=24765
property ScriptTitle : "FindScriptsByDate"
property infoIcon : a reference to file ((path to library folder from system domain as text) & "CoreServices:CoreTypes.bundle:Contents:Resources:AlertNoteIcon.icns")
property searchIcon : a reference to file ((path to library folder from system domain as text) & "CoreServices:CoreTypes.bundle:Contents:Resources:ScreenSharingIcon.icns")
property timeKinds : {"Created", "Modified", "Used", "None"}
set idOfFrontApp to getfrontAppId()
set thetimeKind to chosenItemsFromAList({aTxtMessage:"What kind of date are you searching by?", aTxtTitle:my ScriptTitle, timeoutInSeconds:300, anItemsList:timeKinds, defaultItemsList:item 4 of timeKinds, btnsAsList:{"Cancel", "OK"}, cancelIdxAsInt:1, defaultIdxAsInt:2, blnIfMultiple:false, bundleIdToFrontapp:idOfFrontApp})
if thetimeKind is null then error number -128
if thetimeKind is not in "None" then
set daysback to dataEntryDialog({aTxtMessage:"How many days do you want to search?", aTxtTitle:my ScriptTitle, timeInSecondsAsInt:300, defaultAnsAsText:7 as text, btnsAsList:{"Cancel", "Ok"}, cancelIdxAsInt:1, defaultIdxAsInt:2, iconFileRefOrNull:searchIcon, bundleIdFrontApp:idOfFrontApp})
if daysback is "" then error number -128
end if
set soughtText to dataEntryDialog({aTxtMessage:"Enter some text you want to search for, or nothing!", aTxtTitle:my ScriptTitle, timeInSecondsAsInt:300, defaultAnsAsText:"", btnsAsList:{"Cancel", "Ok"}, cancelIdxAsInt:1, defaultIdxAsInt:2, iconFileRefOrNull:searchIcon, bundleIdFrontApp:idOfFrontApp})
if soughtText is null then error number -128 -- my way is not consisten with the user interface guidelines
-- if soughtText is null then set soughtText to "" -- my way
if thetimeKind is in "None" and soughtText is "" then error number -128 -- you don't want to do that, trust me!
if thetimeKind is in "Created" then
if soughtText is not "" then
set foundFiles to paragraphs of (do shell script "mdfind -onlyin / ' kMDItemContentType == com.apple.applescript.script && kMDItemContentCreationDate < $time.today && kMDItemContentCreationDate >= $time.today(-" & daysback & ") && kMDItemTextContent == \"*" & soughtText & "*\"[c]'")
else
set foundFiles to paragraphs of (do shell script "mdfind -onlyin / ' kMDItemContentType == com.apple.applescript.script && kMDItemContentCreationDate < $time.today && kMDItemContentCreationDate >= $time.today(-" & daysback & ")'")
end if
else if thetimeKind is in "Modified" then
if soughtText is not "" then
set foundFiles to paragraphs of (do shell script "mdfind -onlyin / ' kMDItemContentType == com.apple.applescript.script && kMDItemFSContentChangeDate < $time.today && kMDItemFSContentChangeDate >= $time.today(-" & daysback & ") && kMDItemTextContent == \"*" & soughtText & "*\"[c]'")
else
set foundFiles to paragraphs of (do shell script "mdfind -onlyin / ' kMDItemContentType == com.apple.applescript.script && kMDItemFSContentChangeDate < $time.today && kMDItemFSContentChangeDate >= $time.today(-" & daysback & ")'")
end if
else if thetimeKind is in "Used" then
if soughtText is not "" then
-- used
set foundFiles to paragraphs of (do shell script "mdfind -onlyin / ' kMDItemContentType == com.apple.applescript.script && kMDItemLastUsedDate < $time.today && kMDItemLastUsedDate >= $time.today(-" & daysback & ") && kMDItemTextContent == \"*" & soughtText & "*\"[c]'")
else
set foundFiles to paragraphs of (do shell script "mdfind -onlyin / ' kMDItemContentType == com.apple.applescript.script && kMDItemLastUsedDate < $time.today && kMDItemLastUsedDate >= $time.today(-" & daysback & ")'")
end if
else -- "None"
set foundFiles to paragraphs of (do shell script "mdfind -onlyin / ' kMDItemContentType == com.apple.applescript.script && kMDItemTextContent == \"*" & soughtText & "*\"[c]'")
end if
if foundFiles ≠{} then
if soughtText is not "" then
if thetimeKind is not in "None" then
set theMsg to "Choose some files, " & thetimeKind & " within the last " & daysback & " days with the text: " & soughtText & " that you want to open!"
else
set theMsg to "Choose some files, with the text: " & soughtText & " that you want to open!"
end if
else
set theMsg to "Choose some files, " & thetimeKind & " within the last " & daysback & " days that you want to open!"
end if
set theFilesToOpen to chosenItemsFromAList({aTxtMessage:theMsg, aTxtTitle:my ScriptTitle, timeoutInSeconds:300, anItemsList:foundFiles, defaultItemsList:(item 1 of foundFiles), blnIfMultiple:true, bundleIdToFrontapp:idOfFrontApp}) -- Returns List of Chosen Items or false.
if theFilesToOpen ≠null then
repeat with ftoOpen in theFilesToOpen
try
do shell script "open " & (quoted form of contents of ftoOpen)
end try
end repeat
else
abortNicely({bundleIdFrontApp:idOfFrontApp})
end if
else
alertDialog({aTextMessage:"There were no files found", aTextTitle:my ScriptTitle, timeInSecs:300, btnAsList:{"Ok"}, iconAsFileRef:infoIcon, bundleIdOfFrontApp:idOfFrontApp})
abortNicely({bundleIdFrontApp:idOfFrontApp})
end if
on alertDialog(R) -- Returns Nothing
-- R : {aTextMessage:theMessage,aTextTitle:thetitle,timeInSecs:lenToTimeout,btnAsList:theButton,iconAsFileRef:theIcon,bundleIdOfFrontApp:frontappId}
local res, failed, e, N
set failed to false
tell application "SystemUIServer"
activate
try
if (iconAsFileRef of R) is null then
set res to button returned of (display dialog (aTextMessage of R) with title (aTextTitle of R) giving up after (timeInSecs of R) buttons (btnAsList of R) default button 1)
else
set res to button returned of (display dialog (aTextMessage of R) with title (aTextTitle of R) giving up after (timeInSecs of R) buttons (btnAsList of R) default button 1 with icon (iconAsFileRef of R))
end if
if res = "" then set failed to true
on error e number N
set failed to true
end try
end tell
if failed is true then
abortNicely({bundleIdFrontApp:(bundleIdOfFrontApp of R)}) -- Returns Nothing
end if
return
end alertDialog
on dataEntryDialog(R) -- Returns TheReplyOrNull
-- R : {aTxtMessage:theMessage,aTxtTitle:thetitle,timeInSecondsAsInt:lenToTimeout,defaultAnsAsText:theDefaultAns,btnsAsList:theButtons,cancelIdxAsInt:btnIdxCancel,defaultIdxAsInt:btnIdxDefault,iconFileRefOrNull:rscFile,bundleIdFrontApp:idOfFrontApp}
local res, failed, outcome
set failed to false
tell application "SystemUIServer"
activate
try
if (iconFileRefOrNull of R) is null then
set res to (display dialog (aTxtMessage of R) default answer (defaultAnsAsText of R) with title (aTxtTitle of R) giving up after (timeInSecondsAsInt of R) buttons (btnsAsList of R) cancel button (cancelIdxAsInt of R) default button (defaultIdxAsInt of R))
else
set res to (display dialog (aTxtMessage of R) default answer (defaultAnsAsText of R) with title (aTxtTitle of R) giving up after (timeInSecondsAsInt of R) buttons (btnsAsList of R) cancel button (cancelIdxAsInt of R) default button (defaultIdxAsInt of R) with icon (iconFileRefOrNull of R))
end if
if gave up of res is true then error number 3000
set outcome to trimwh of me from (text returned of res)
on error e number N
-- log e & " : " & n
if N is -128 then
set outcome to null
else if N is 3000 then
set failed to true
-- log "Timed out"
end if
end try
end tell
if failed is true then
abortNicely({bundleIdFrontApp:(bundleIdFrontApp of R)}) -- Returns Nothing
else
return outcome
end if
end dataEntryDialog
on chosenItemsFromAList(R) -- Returns List of Chosen Items or null.
-- R : {aTxtMessage:theMessage,aTxtTitle:thetitle,timeoutInSeconds:lenToTimeout,anItemsList:theList,defaultItemsList:defItems,blnIfMultiple:multipleAllowed,bundleIdToFrontapp:frontappId}
-- returns indicies, dies upon timeout
local myres, idxList
try
with timeout of (timeoutInSeconds of R) seconds
tell application "SystemUIServer"
activate
if (blnIfMultiple of R) is true then
set myres to choose from list (anItemsList of R) default items (defaultItemsList of R) with title (aTxtTitle of R) with prompt (aTxtMessage of R) with multiple selections allowed
else
set myres to choose from list (anItemsList of R) default items (defaultItemsList of R) with title (aTxtTitle of R) with prompt (aTxtMessage of R)
end if
end tell
end timeout
if myres is false then return null
return myres
on error e number N
abortNicely({bundleIdFrontApp:(bundleIdToFrontapp of R)}) -- Returns Nothing
end try
end chosenItemsFromAList
on getfrontAppId() -- Returns bundleid of active app
local frontappId
set frontappId to ""
tell application "System Events"
set frontappId to bundle identifier of first application process whose frontmost is true
end tell
return frontappId
end getfrontAppId
on abortNicely(R) -- Returns Nothing
tell application "System Events" to tell application process id (bundleIdFrontApp of R)
key down control
key code 118
key up control
end tell
error number -128
end abortNicely
to trimwh from astring
-- 09/08/12 Tested!
-- http://macscripter.net/viewtopic.php?pid=154062#p154062
set oldDelims to AppleScript's text item delimiters
set AppleScript's text item delimiters to {" ", " "} -- space, tab
set aList to astring's text items
repeat with i from 1 to (count aList)
if item i of aList is "" then set item i of aList to missing value
end repeat
set astring to aList's text as text
set AppleScript's text item delimiters to oldDelims
return astring
end trimwh