基于单独的列表将文件放在空文件结构中?

时间:2016-03-09 01:47:37

标签: macos automation applescript

是否有一种相当简单的方法可以根据列表或电子表格将文件放入文件夹结构中?

例如,假设我有100个不同的动物图像;分类的空文件夹结构 - 包含5个不同族文件夹的Kingdom文件夹,每个文件夹包含5个Species文件夹;以及每个文件名及其相应的王国,家庭和物种的电子表格。我如何写一些AppleScript或告诉automator来解释电子表格中的信息,以便将每个图像移动或复制到电子表格中指定的目录中?

我发现有很多好方法可以将内容放在特定的目录中,但使用单独的列表来指定文件的目的地并不多。

谢谢, 人

1 个答案:

答案 0 :(得分:0)

最简单的方法是将Excel列表保存到文本/制表符分隔符文件中。然后脚本可以读取该文件。

在下面的脚本中,我假设文本文件总是有3列文件名,系列文件夹名和种类文件夹名。

该脚本首先询问源文件夹(所有图像都在哪里),然后是'kingdom'文件夹(子文件夹和图像将在哪里),最后是文本文件(来自Excel)。

然后脚本循环到文本文件的每一行,并将文件移动到相关文件夹。如果子文件夹系列或物种不存在,则脚本在移动图像之前创建它们。 如果文本文件的行不包含3个值,则跳过行。

我提出了很多评论,让你可以根据需要进行调整:

set Source to (choose folder with prompt "Select images folder") as text
set Kingdom to (choose folder with prompt "Select destination folder") as text

set Ftext to choose file with prompt "Select text file containing naes & folder paths"
-- file format must be : FileName <tab> FamilyFolder <tab> SpeciesFolder <return>

set FContent to read Ftext
set allLines to every paragraph of FContent
repeat with aline in allLines
set AppleScript's text item delimiters to {ASCII character 9} -- use tab to split columns
set T to every text item of aline
if (count of text items of T) = 3 then -- valid line
    set FName to text item 1 of T
    set Family to text item 2 of T
    set Species to text item 3 of T
    tell application "Finder"
        try
            -- check if file Fname in Source folder exists
            if not (exists file (Source & FName)) then
                display alert "the file " & FName & " does not exist in source folder."
                return
            end if
            -- check if folder Family in Kingdom folder exists
            if not (exists folder Family in folder Kingdom) then
                make new folder in folder Kingdom with properties {name:Family}
            end if
            -- check if folder Species in Family folder exists
            if not (exists folder Family in folder Family of folder Kingdom) then
                make new folder in folder Family of folder Kingdom with properties {name:Species}
            end if
            -- then move FName from Source to Kingdom / Family / Species
            move file (Source & FName) to folder Species of folder Family of folder Kingdom
        end try
    end tell
end if
end repeat