使用Automator和Applescript根据文件名将文件移动到文件夹中

使用Automator和Applescript根据文件名将文件移动到文件夹中,applescript,automator,Applescript,Automator,我有一个包含以下文件的文件夹: Elephant.19864.archive.other.pdf Elephant.17334.other.something.pdf Turnip.19864.something.knight.pdf Camera.22378.nothing.elf.pdf 我希望这些文件移动到以下结构中 Archive Elephant Elephant.19864.pdf Elephant.17334.pdf Turnip

我有一个包含以下文件的文件夹:

Elephant.19864.archive.other.pdf
Elephant.17334.other.something.pdf
Turnip.19864.something.knight.pdf
Camera.22378.nothing.elf.pdf
我希望这些文件移动到以下结构中

Archive
    Elephant
        Elephant.19864.pdf
        Elephant.17334.pdf
    Turnip
        Turnip.19864.pdf
    Camera.HighRes
        Camera.HighRes.22378.pdf
生成的文件由一个单词或多个单词组成,后跟一个数字序列,后跟其他单词,然后是扩展名。我想将它们移动到一个名为单词或数字前面的单词的文件夹中,并删除数字和扩展名(.pdf)之间的所有单词

如果文件夹不存在,那么我必须创建它

我原以为使用Automator或AppleScript会很简单,但我似乎无法理解它


使用Automator/AppleScript容易吗?如果是这样,我应该看些什么?很容易,一开始并不明显。一些事情让你开始

要解析文件名以获取文件夹名,需要将名称分隔为一个列表

set AppleScript's text item delimiters to {"."}
set fileNameComponents to (every text item in fileName) as list
set AppleScript's text item delimiters to oldDelims
--> returns: {"Elephant", "19864", "archive", "other", "pdf"}
该列表有一个基于1的索引,因此第1项是“大象”,第5项是“pdf”。要将文件名混合在一起,您只需要

set theFileName to (item 1 of fileNameComponents & item 2 of fileNameComponents & item 5 of fileNameComponents) as string
要创建文件夹,只需使用以下命令

tell application "Finder"
    set theNewFolder to make new folder at (theTargetFolder as alias) with properties {name:newFolderName, owner privileges:read write, group privileges:read write, everyones privileges:read write}
end tell
set theFileToRename to theTargetFilePath as alias -- alias is important here
set name of theFileToRename to theFileName
要移动文件,您只需要以下内容

tell application "Finder"
    set fileMoved to move theTargetFile to theTargetFolder
end tell
要重命名文件,请使用类似以下内容的内容

tell application "Finder"
    set theNewFolder to make new folder at (theTargetFolder as alias) with properties {name:newFolderName, owner privileges:read write, group privileges:read write, everyones privileges:read write}
end tell
set theFileToRename to theTargetFilePath as alias -- alias is important here
set name of theFileToRename to theFileName
我建议首先创建一个所有目标文件的列表,然后根据文件名为列表中的每个文件创建文件夹,移动文件,最后在文件到达最终位置后重命名文件


加盐调味。

这很简单,一开始并不明显。一些事情让你开始

要解析文件名以获取文件夹名,需要将名称分隔为一个列表

set AppleScript's text item delimiters to {"."}
set fileNameComponents to (every text item in fileName) as list
set AppleScript's text item delimiters to oldDelims
--> returns: {"Elephant", "19864", "archive", "other", "pdf"}
该列表有一个基于1的索引,因此第1项是“大象”,第5项是“pdf”。要将文件名混合在一起,您只需要

set theFileName to (item 1 of fileNameComponents & item 2 of fileNameComponents & item 5 of fileNameComponents) as string
要创建文件夹,只需使用以下命令

tell application "Finder"
    set theNewFolder to make new folder at (theTargetFolder as alias) with properties {name:newFolderName, owner privileges:read write, group privileges:read write, everyones privileges:read write}
end tell
set theFileToRename to theTargetFilePath as alias -- alias is important here
set name of theFileToRename to theFileName
要移动文件,您只需要以下内容

tell application "Finder"
    set fileMoved to move theTargetFile to theTargetFolder
end tell
要重命名文件,请使用类似以下内容的内容

tell application "Finder"
    set theNewFolder to make new folder at (theTargetFolder as alias) with properties {name:newFolderName, owner privileges:read write, group privileges:read write, everyones privileges:read write}
end tell
set theFileToRename to theTargetFilePath as alias -- alias is important here
set name of theFileToRename to theFileName
我建议首先创建一个所有目标文件的列表,然后根据文件名为列表中的每个文件创建文件夹,移动文件,最后在文件到达最终位置后重命名文件

加盐调味