Hi, I need some help to finish the routine below, which intent is to collect documents with similar names in a main folder. (like: Ubuntu auf Mac - Anleitung_files, Ubuntu auf Mac - Anleitung_htm, Ubuntu 10.04 virtualbox shared folder_files, Ubuntu 10.04 virtualbox shared folder_htm, ect)
feel free to suggest another approach too. thanks.
-on sort_names(subfolder)
tell application "Finder"
set subfolder to (folder of front window) --for debugging purposes
set container_of to (subfolder as alias)
set subfiles to document files of container_of
end tell
--find similar file names and create their folders
set folder_ls to {}
repeat with i in subfiles --get contents of subfolder x
set the_nm to (get name of (info for i as alias))
set find_nm to my gt_nm(the_nm)
if (count words in find_nm) > 1 then
set find_tag to {first word of find_nm, middle word of find_nm, last word of find_nm}
else
set find_tag to first word of find_nm
end if
--choose the longest word of a name (its a bit arbitrary, I know- but the results aren't bad at all)
--another possibility could be to search 3 times (first, middle, last word) but the processing it becomes too long.
set gr_of to {}
repeat with i in find_tag
if gr_of is {} then
set gr_of to (i as text)
else
if (length of i as text) > length of gr_of then set gr_of to i
end if
end repeat
set the_sc to {}
if length of gr_of > 3 then
copy paragraphs of (do shell script "find " & (quoted form of POSIX path of container_of) & space & "-iname " & gr_of & "*") to end of the_sc
end if
display dialog paragraph 1 of (the_sc as list) -->why 0?
if (count paragraphs of the_sc) ≠0 and (count paragraphs of the_sc) > 3 then
set find_tag to gr_of
try
tell application "Finder" to make new folder at container_of with properties {name:find_tag, label index:3}
end try
copy find_tag to end of folder_ls
end if
end repeat
repeat with i in folder_ls
set tag_folder to (i as alias)
copy paragraphs of (do shell script "find " & (quoted form of POSIX path of (container_of as alias)) & space & "-iname " & find_tag & "*") to the_sc
set all_similar to my correct_pt(the_sc)
tell application "Finder" to repeat with a in all_similar
try
move (a as alias) to tag_folder
end try
end repeat
end repeat
--end sort_names
on gt_nm(the_nm)
set res to the_nm
set the_c to 0
--cut the extension, remove hyphens
set del to {".", ".", "_", "|"}
repeat with i in del
set the_c to the_c + 1
if i is in res then
set tid to AppleScript's text item delimiters
set AppleScript's text item delimiters to i
if the_c is 1 then
copy first text item of (get text items of the_nm) to res
else
set AppleScript's text item delimiters to " "
set res to "" & res
set AppleScript's text item delimiters to {}
end if
set tid to AppleScript's text item delimiters
end if
end repeat
return (res as text)
end gt_nm
on correct_pt(the_sc)
repeat with i in the_sc
set thisText to i
set AppleScript's text item delimiters to "//"
set thisText to thisText's text items
set AppleScript's text item delimiters to "/"
set thisText to "" & thisText
set AppleScript's text item delimiters to {}
end repeat
return thisText
end correct_pt