Applescript - создание папок на основе первого слова имени файла - PullRequest
0 голосов
/ 29 июня 2018

По сути, я ищу яблочный скрипт, который позволил бы мне упорядочить огромные 50 000 файлов, создавая папки, содержащие только первое слово файлов, игнорируя остальную часть имени файла после первого пробела.

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

Единственная проблема заключается в том, что мне нужно выбрать в «тексте от 1 до» числа букв, которые я хочу сохранить. И, к сожалению, первое слово в именах файлов имеет разную длину ...

Можно ли изменить этот скрипт на мой? или у вас есть другие предложения?

Заранее спасибо за любой ответ !!

1 Ответ

0 голосов
/ 29 июня 2018

Я рекомендую использовать 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
...