------------------------------------------------------------------------------------------------
# Created: 2012-09-11 : 12:00
# Modified: 2012-09-17 : 15:25
# Application: Finder & mdfind commandline (Spotlight)
# Purpose: Find similar file to Finder-selection in software archive and move selection to it.
# Dependencies: Only OSX components used.
------------------------------------------------------------------------------------------------
try
set softwareArchiveFolder to (POSIX path of ("" & (path to home folder) & "Software_Archive:"))
tell application "SystemUIServer"
activate
end tell
tell application "Finder"
activate
set sel to selection as alias list
if sel ≠ {} then
set _file to first item of sel
set _name to name of _file
set _ext to name extension of _file
end if
end tell
if _ext ≠ "" then
set AppleScript's text item delimiters to "." & _ext
set _name to text item 1 of _name
end if
set _name to do shell script "sed -E 's/ *[0-9\\._-]+ */ /g;s/ +$//;s/^ +//;s/ +/*/g' <<< " & quoted form of _name
set searchString to "*" & _name & "*"
set searchFolder to quoted form of 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
move _file to _folder
reveal _file
activate
end tell
else
set sel to (item 1 of sel) as «class furl»
sel
set the clipboard to sel
set fldr to alias POSIX file softwareArchiveFolder
tell application "Finder" to open fldr
end if
on error e number n
set e to e & return & return & "Error Number: " & n
beep
tell me to set dDlg to display dialog e buttons {"Cancel", "Copy", "OK"} default button "OK" with icon 0
if button returned of dDlg = "Copy" then set the clipboard to e
end try
------------------------------------------------------------------------------------------------