------------------------------------------------------------------------------------------------
# Created: 2012-09-11 : 12:00
# Modified: 2012-09-11 : 12:32
# Application: Finder & mdfind commandline (Spotlight)
# Purpose: Find similar file to Finder-selection in software archive and move selection to it.
# Dependencies: None: only OSX components used.
------------------------------------------------------------------------------------------------
try
# Several of these are unnecessary for *this* script but here for convenience.
set applicationFolder to (POSIX path of (path to applications folder))
set userAppSupportFolder to quoted form of (POSIX path of (path to application support from user domain))
set userLibraryFolder to quoted form of (POSIX path of (path to library folder from user domain))
set userPrefsFolder to quoted form of (POSIX path of (path to preferences folder from user domain))
set softwareArchiveFolder to quoted form of (POSIX path of ("" & (path to home folder) & "Software_Archive:"))
tell application "Finder"
set sel to selection as alias list
if sel ≠ {} then
set _file to first item of sel
set _name to name of _file
end if
end tell
set _word to word 1 of _name
set searchString to "*" & _word & "*"
set searchFolder to softwareArchiveFolder
set cmd to "mdfind -onlyin " & searchFolder & " \"kMDItemFSName == '" & searchString & "'c\""
set foundItems to do shell script cmd
set _folder to false
if foundItems ≠ "" then
set foundItems to paragraphs of foundItems
set AppleScript's text item delimiters to "/"
repeat with i in foundItems
if i contains "." then
set _folder to contents of i
set _folder to (text items 1 thru -2 of _folder) as text
set _folder to alias POSIX file _folder
exit repeat
end if
end repeat
end if
if _folder ≠ false then
tell application "Finder"
open _folder
# set bounds of front window to {0, 44, 870, 520}
move _file to _folder
reveal _file
activate
end tell
end if
on error e number n
set e to "Error: " & e & return & return & "Error Number: " & n
beep
tell me to set dDlg to display dialog e buttons {"Cancel", "Copy", "OK"} default button "OK"
if button returned of dDlg = "Copy" then set the clipboard to e
end try
------------------------------------------------------------------------------------------------