Find scripts by modification, creation or used date, with text

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! :smiley:


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


Hello!

This script gives you usage reports over a file, that is the dates and times it was used. Which may be handy now and then.



property scripttitle : "File Usage Dates Before Now"
--http://macscripter.net/viewtopic.php?id=24765 Idea by Adam Bell
-- © McUsr 2012 you are not allowed to post this elsewhere! http://macscripter.net/post.php?tid=39278
property genericIcon : a reference to file ((path to library folder from system domain as Unicode text) & "CoreServices:CoreTypes.bundle:Contents:Resources:GenericEditionFileIcon.icns")
set infoIcon to a reference to file ((path to library folder from system domain as text) & "CoreServices:CoreTypes.bundle:Contents:Resources:AlertNoteIcon.icns")
set idOfFrontApp to getfrontAppId()
set ReportText to ""
set failed to false
try
	
	tell application "Finder"
		activate
		set selected_items_list to (get selection) as alias list
		
		set selCount to count selected_items_list
		
		if selCount ≠ 0 then set last_item to the last item of the selected_items_list as text
		
		try
			set startPath to target of its Finder window 1 as alias
		on error
			set starpath to path to desktop folder
		end try
		
	end tell
	
	
	if selCount = 0 then
		tell application "SystemUIServer"
			activate
			try
				set the selected_items_list to (choose file with prompt "Choose the files or folders you want to see info for for" default location startPath with multiple selections allowed)
			on error
				set failed to true
			end try
		end tell
		
		set selCount to count selected_items_list
		if ((count of the selected_items_list) = 0) or failed then
			alertDialog({aTextMessage:"No files or applications are selected.", aTextTitle:my scripttitle, timeInSecs:300, btnAsList:{"Ok"}, iconAsFileRef:infoIcon, bundleIdOfFrontApp:idOfFrontApp})
			abortNicely({bundleIdFrontApp:idOfFrontApp})
		end if
		set last_item to the last item of the selected_items_list as text
		
	end if
	
	set failed to false
	tell application "SystemUIServer"
		activate
		try
			set outputType to button returned of (display dialog "Please choose form of output" with title scripttitle buttons {"Cancel", "Report", "Dialogs"} cancel button 1 default button 3 with icon genericIcon)
		on error
			set failed to true
		end try
	end tell
	if failed then abortNicely({bundleIdFrontApp:idOfFrontApp})
	
	repeat with this_item in the selected_items_list
		if outputType is "Dialogs" then
			set ReportText to ""
		end if
		
		
		set usageReport to showUsage for (this_item as alias)
		
		if outputType is "Report" then
			set ReportText to ReportText & return & "==================================" & return & return
		else
			set ReportText to ReportText & return
		end if
		
		
		set ReportText to ReportText & usageReport & return & return
		
		if outputType is "Dialogs" then
			set the clipboard to ReportText
			if contents of this_item is the last_item or selCount is 1 then
				set the button_list to {"Done"}
			else
				set the button_list to {"Cancel", "Next"}
			end if
			-- display the information
			with timeout of 900 seconds
				tell application "SystemUIServer"
					activate
					
					display dialog ReportText with title scripttitle buttons the button_list default button (the last item of the button_list)
				end tell
			end timeout
		end if
	end repeat
	if outputType is "Report" then
		
		tell application "TextEdit"
			activate
			make new document at the front
			set text of front document to "F i l e   i n f o r m a t i o n " & return & ReportText
		end tell
	end if
on error e number N
	if not failed then
		alertDialog({aTextMessage:e & " " & N, aTextTitle:my scripttitle, timeInSecs:300, btnAsList:{"Ok"}, iconAsFileRef:infoIcon, bundleIdOfFrontApp:idOfFrontApp})
		abortNicely({bundleIdFrontApp:idOfFrontApp})
	end if
end try

on showUsage for tFile
	local tpapth, tName, MD, tDates, Usage, theText, ofs
	set tPath to quoted form of (POSIX path of tFile)
	set tName to name of (info for tFile)
	set MD to do shell script "mdls -raw -name kMDItemUsedDates " & tPath
	set tid to AppleScript's text item delimiters
	set AppleScript's text item delimiters to "("
	set tDates to text item -1 of MD
	set AppleScript's text item delimiters to ")"
	set tDates to (text item 1 of tDates) as text
	set AppleScript's text item delimiters to return
	set tDates to text items of tDates
	set AppleScript's text item delimiters to tid
	set Usage to {}
	--  on suggestion from  DJ Bazzie Wazzie
	if item 2 of tDates contains "null" then
		set theText to "The file \"" & tPath & "\" has never been used!" & return & return
		return theText
	end if
	
	repeat with k from 2 to ((count tDates) - 1)
		set ofs to offset of "\"" in (item k of tDates)
		set Usage's end to (text (ofs + 1) thru -2 of item k of tDates) & return
	end repeat
	set Usage to Usage as text
	set theText to "The file \"" & tPath & "\" was used as follows:" & return & return & Usage
	return theText
end showUsage

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

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


Hello!

The script above is reinforced with regards to ui.

The script in post #1 has had its ui reinforced! You can now actually cancel the script by hitting esc! :slight_smile:

I have also removed a bug in the query,placing the [c] clause for not considering case behind and not in front of the search string; but in all fairness, I believe I saw this in online documentation, and thought that looked nice! It just doesn’t work with SL at least!

I have also added the option of not searching by date at all. But just search for text which is then mandatory in order for the script to run.

The follwoing handler is a bit more secure because when kMDItemUsedDates doesn’t exists the script won’t fail:


on showUsage for tFile
	set x to do shell script "mdls -raw -name kMDItemUsedDates " & quoted form of (POSIX path of tFile) & " | tr '[)(\\15\\12]' '[}{\\40\\40]'"
	if x = "{null}" then return "The file was never used."
	set l to run script x
	set AppleScript's text item delimiters to linefeed
	set s to l as string
	set AppleScript's text item delimiters to ""
	return "The file \"" & tFile & "\" was used as follows:" & return & return & s
end showUsage

Hello!

Thank you very much, I’ll ponder that!

I have removed another problem, a bug! it now actually opens files when run from the script menu. I am sure I tested this yesterday and it worked! But is surely does work now. The script in post #1.

Hello, I have patched the handler that returns the usage dates to act intelligently when it appears to be no usage of the file in the script in post 2

Thanks, for the insight DJ Bazzie Wazzie

Hello.

Apple has changed an icon in CoreServices:CoreTypes.bundle:Contents:Resources, so I had to change it for the script in post #1 as well, and I have also, changed the properties that holds the icons into global variables.

-- Updated for Mavericks 2013.11.20
-- 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:FavouriteItems.icns")
property timeKinds : {"Created", "Modified", "Used", "None"}

global infoIcon, searchIcon

set infoIcon to a reference to file ((path to library folder from system domain as text) & "CoreServices:CoreTypes.bundle:Contents:Resources:AlertNoteIcon.icns")
set searchIcon to a reference to file ((path to library folder from system domain as text) & "CoreServices:CoreTypes.bundle:Contents:Resources:FavoriteItemsIcon.icns")
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 back 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 «constant epmdctlm»
		key code 118
		key up «constant epmdctlm»
	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


Hello.

The script for reporting usagedates for a file where also broken in sense that it used the genericIcon that is no longer included.

Here is a new version, with a new icon that works under Mavericks. The icon is copyright Apple.inc.



property scripttitle : "File Usage Dates Before Now"
--http://macscripter.net/viewtopic.php?id=24765 Idea by Adam Bell
-- © McUsr 2012 you are not allowed to post this elsewhere! http://macscripter.net/post.php?tid=39278
global infoIcon, genericIcon
set infoIcon to a reference to file ((path to library folder from system domain as text) & "CoreServices:CoreTypes.bundle:Contents:Resources:AlertNoteIcon.icns")
set genericIcon to a reference to file ((path to library folder from system domain as Unicode text) & "CoreServices:CoreTypes.bundle:Contents:Resources:ConnectToIcon.icns")
set idOfFrontApp to getfrontAppId()
set ReportText to ""
set failed to false
try
	
	tell application "Finder"
		activate
		set selected_items_list to (get selection) as alias list
		
		set selCount to count selected_items_list
		
		if selCount ≠ 0 then set last_item to the last item of the selected_items_list as text
		
		try
			set startPath to target of its Finder window 1 as alias
		on error
			set starpath to path to desktop folder
		end try
		
	end tell
	
	
	if selCount = 0 then
		tell application "SystemUIServer"
			activate
			try
				set the selected_items_list to (choose file with prompt "Choose the files or folders you want to see info for for" default location startPath with multiple selections allowed)
			on error
				set failed to true
			end try
		end tell
		
		set selCount to count selected_items_list
		if ((count of the selected_items_list) = 0) or failed then
			alertDialog({aTextMessage:"No files or applications are selected.", aTextTitle:my scripttitle, timeInSecs:300, btnAsList:{"Ok"}, iconAsFileRef:infoIcon, bundleIdOfFrontApp:idOfFrontApp})
			abortNicely({bundleIdFrontApp:idOfFrontApp})
		end if
		set last_item to the last item of the selected_items_list as text
		
	end if
	
	set failed to false
	tell application "SystemUIServer"
		activate
		try
			set outputType to button returned of (display dialog "Please choose form of output" with title scripttitle buttons {"Cancel", "Report", "Dialogs"} cancel button 1 default button 3 with icon genericIcon)
		on error
			set failed to true
		end try
	end tell
	if failed then abortNicely({bundleIdFrontApp:idOfFrontApp})
	
	repeat with this_item in the selected_items_list
		if outputType is "Dialogs" then
			set ReportText to ""
		end if
		
		
		set usageReport to showUsage for (this_item as alias)
		
		if outputType is "Report" then
			set ReportText to ReportText & return & "==================================" & return & return
		else
			set ReportText to ReportText & return
		end if
		
		
		set ReportText to ReportText & usageReport & return & return
		
		if outputType is "Dialogs" then
			set the clipboard to ReportText
			if contents of this_item is the last_item or selCount is 1 then
				set the button_list to {"Done"}
			else
				set the button_list to {"Cancel", "Next"}
			end if
			-- display the information
			with timeout of 900 seconds
				tell application "SystemUIServer"
					activate
					
					display dialog ReportText with title scripttitle buttons the button_list default button (the last item of the button_list)
				end tell
			end timeout
		end if
	end repeat
	if outputType is "Report" then
		
		tell application "TextEdit"
			activate
			make new document at the front
			set text of front document to "F i l e   i n f o r m a t i o n " & return & ReportText
		end tell
	end if
on error e number N
	if not failed then
		alertDialog({aTextMessage:e & " " & N, aTextTitle:my scripttitle, timeInSecs:300, btnAsList:{"Ok"}, iconAsFileRef:infoIcon, bundleIdOfFrontApp:idOfFrontApp})
		abortNicely({bundleIdFrontApp:idOfFrontApp})
	end if
end try

on showUsage for tFile
	local tpapth, tName, MD, tDates, Usage, theText, ofs
	set tPath to quoted form of (POSIX path of tFile)
	set tName to name of (info for tFile)
	set MD to do shell script "mdls -raw -name kMDItemUsedDates " & tPath
	set tid to AppleScript's text item delimiters
	set AppleScript's text item delimiters to "("
	set tDates to text item -1 of MD
	set AppleScript's text item delimiters to ")"
	set tDates to (text item 1 of tDates) as text
	set AppleScript's text item delimiters to return
	set tDates to text items of tDates
	set AppleScript's text item delimiters to tid
	set Usage to {}
	--  on suggestion from  DJ Bazzie Wazzie
	if item 2 of tDates contains "null" then
		set theText to "The file \"" & tPath & "\" has never been used!" & return & return
		return theText
	end if
	
	repeat with k from 2 to ((count tDates) - 1)
		set ofs to offset of "\"" in (item k of tDates)
		set Usage's end to (text (ofs + 1) thru -2 of item k of tDates) & return
	end repeat
	set Usage to Usage as text
	set theText to "The file \"" & tPath & "\" was used as follows:" & return & return & Usage
	return theText
end showUsage

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

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