将文件放置在基于单独列表的空文件结构中?
Placing Files in an Empty File Structure Based on a Separate List?
是否有一种相当简单的方法可以将文件放入基于列表或电子表格的文件夹结构中?
假设,例如,我有 100 张不同的动物图像;一个空的分类文件夹结构——一个 Kingdom 文件夹,包含 5 个不同的 Family 文件夹,每个文件夹包含 5 个 Species 文件夹;以及每个文件名及其对应的王国、家族和物种的电子表格。我如何编写一些 applescript 或告诉 automator 来解释电子表格中的信息以将每个图像移动或复制到电子表格中指定的目录?
我发现了很多将东西放在特定目录中的好方法,但在使用单独的列表来指定文件的目的地方面却知之甚少。
谢谢,
阿尔
最简单的方法是将 Excel 列表保存到文本 /tab 分隔符文件中。然后脚本可以读取该文件。
在下面的脚本中,我假设文本文件总是有 3 列文件名、家族文件夹名称和物种文件夹名称。
脚本首先询问源文件夹(所有图像所在的位置),然后是 'kingdom' 文件夹(子文件夹和图像所在的位置),最后是文本文件(来自 Excel)。
然后脚本循环到文本文件的每一行,并将文件移动到相关文件夹。如果子文件夹 family 或 species 不存在,则脚本会在移动图像之前创建它们。
如果文本文件的行不包含 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
是否有一种相当简单的方法可以将文件放入基于列表或电子表格的文件夹结构中?
假设,例如,我有 100 张不同的动物图像;一个空的分类文件夹结构——一个 Kingdom 文件夹,包含 5 个不同的 Family 文件夹,每个文件夹包含 5 个 Species 文件夹;以及每个文件名及其对应的王国、家族和物种的电子表格。我如何编写一些 applescript 或告诉 automator 来解释电子表格中的信息以将每个图像移动或复制到电子表格中指定的目录?
我发现了很多将东西放在特定目录中的好方法,但在使用单独的列表来指定文件的目的地方面却知之甚少。
谢谢, 阿尔
最简单的方法是将 Excel 列表保存到文本 /tab 分隔符文件中。然后脚本可以读取该文件。
在下面的脚本中,我假设文本文件总是有 3 列文件名、家族文件夹名称和物种文件夹名称。
脚本首先询问源文件夹(所有图像所在的位置),然后是 'kingdom' 文件夹(子文件夹和图像所在的位置),最后是文本文件(来自 Excel)。
然后脚本循环到文本文件的每一行,并将文件移动到相关文件夹。如果子文件夹 family 或 species 不存在,则脚本会在移动图像之前创建它们。 如果文本文件的行不包含 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