Automator / Applescript Сортировка и перемещение файлов - PullRequest
1 голос
/ 28 июля 2010

Мне было интересно, был ли способ в Automator использовать код Applescript, чтобы получить расширение файла, и равняется ли оно определенному расширению (например, .pdf или .rtf), перемещению в определенную папку для этого расширения(например if (extension == pdf) { move to folder "~/PDF Files" } else if (extension == rtf) { move to folder "~/Rich Text Files" })

Ответы [ 2 ]

3 голосов
/ 29 июля 2010

Вот яблочный скрипт.Поскольку ваш запрос был прост, я просто написал его для вас.Обратите внимание, как я получаю расширение файла с помощью подпрограммы «getNameAndExtension (F)».Обычно вы можете получить расширение файла из Finder (так называемое расширение имени), но я обнаружил, что Finder не всегда надежен, поэтому я всегда использую эту подпрограмму.Эта подпрограмма всегда была надежной.

set homeFolder to path to home folder as text
set rtfFolderName to "Rich Text Files"
set pdfFolderName to "PDF Files"

-- choose the files
set theFiles to choose file with prompt "Choose RTF or PDF files to move into your home folder" with multiple selections allowed

-- make sure the folders exist
tell application "Finder"
    if not (exists folder (homeFolder & rtfFolderName)) then
        make new folder at folder homeFolder with properties {name:rtfFolderName}
    end if
    if not (exists folder (homeFolder & pdfFolderName)) then
        make new folder at folder homeFolder with properties {name:pdfFolderName}
    end if
end tell

-- move the files
repeat with aFile in theFiles
    set fileExtension to item 2 of getNameAndExtension(aFile)
    if fileExtension is "rtf" then
        tell application "Finder"
            move aFile to folder (homeFolder & rtfFolderName)
        end tell
    else if fileExtension is "pdf" then
        tell application "Finder"
            move aFile to folder (homeFolder & pdfFolderName)
        end tell
    end if
end repeat



(*=============== SUBROUTINES ===============*)
on getNameAndExtension(F)
    set F to F as Unicode text
    set {name:Nm, name extension:Ex} to info for file F without size
    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
    end if
    return {Nm, Ex}
end getNameAndExtension
0 голосов
/ 28 июля 2010

Я сейчас не в своем Apple, поэтому я не могу поиграть с Automator и разобраться. Но если вы могли бы сделать это, переключив видоискатель в представление списка, отсортируйте по типу, а затем выберите блоки файлов того же типа и перетащите их в нужную папку.

...