Applescript - 根据文件名的第一个单词创建文件夹

Applescript - Creating folders based on the first word of the filename

本质上,我正在寻找一个 applescript,它允许我通过创建只有文件的第一个单词的文件夹来订购大量 50.000 个文件,而忽略第一个 space 之后的文件名的其余部分。

For eaxmple the 50.000 files are named like this:
 - amazingfrog -shootingbase.jpg 
 - frog 2sHDn1_9fFs12s.jpg
 - frog 29adjjdd39939.mov 
 - Horse IUS39aosdja.mov
 - horse 282131888.jpg 
 - HORSE.jpg
 And so on.....

- I would like to be like this:
    - amazingfrog
       -amazingfrog -shootingbase.jpg
    - frog    
       -frog 2sHDn1_9fFs12s.jpg 
       -frog 29adjjdd39939.mov
    - horse
       -horse IUS39aosdja.mov
       -horse 282131888.jpg 
       -horse.gif
And so on....

在互联网上我遇到了以下脚本:

set chosenFolder to (choose folder)
tell application "Finder" to set fileList to files of chosenFolder

repeat with aFile in fileList
    set {name:Nm, name extension:Ex} to info for (aFile as alias)
    if Ex is missing value then set Ex to ""
    if Ex is not "" then set Nm to text 1 thru ((count Nm) - (count Ex) - 1) of Nm
    set dateFolder to text 1 thru 15 of Nm
    set sourceFile to quoted form of POSIX path of (aFile as text)
    set destinationFile to quoted form of (POSIX path of chosenFolder & dateFolder & "/" & name of aFile)
    do shell script "ditto " & sourceFile & space & destinationFile
    do shell script "rm " & sourceFile
end repeat

唯一的问题是我必须在 "text 1 thru" 中选择我想要保留的字母的编号。不幸的是,文件名的第一个单词长度不同...

是否可以根据我的需要修改此脚本?或者您还有其他建议吗?

提前感谢您的回复!!

我推荐使用text item delimiters提取文件名的第一部分

set chosenFolder to (choose folder)
tell application "Finder" to set fileList to files of chosenFolder

set TID to text item delimiters
set text item delimiters to space
repeat with aFile in fileList
    set fileName to name of aFile
    set textItems to text items of fileName
    if (count textItems) = 1 then
        set fileExtension to name extension of aFile
        set folderName to text 1 thru ((get offset of "." & fileExtension in fileName) - 1) of fileName
    else 
        set folderName to first item of textItems
    end if
    set sourceFile to quoted form of POSIX path of (aFile as text)
    set destinationFile to quoted form of (POSIX path of chosenFolder & folderName & "/" & fileName)
    do shell script "ditto " & sourceFile & space & destinationFile
    do shell script "rm " & sourceFile
end repeat
set text item delimiters to TID