Applescript:创建文件夹/子文件夹并移动多个文件

时间:2013-01-25 17:50:56

标签: macos applescript automator

我有一个比我能构建的复杂得多的Applescript问题。我一直在寻找这几天,我找不到这样的剧本,也找不到足够的信息,可以用我有限的知识拼凑出来。

我有多个带结构化名称的文件。每个文件都具有以下名称结构:

  

ttu_collectionname_000001.pdf
  ttu_collectionname_000002.mp3
  ttu_collectionname_000003.pdf ...等(每个文件都有不同的文件类型。)

还有一个与每个原始文件关联的csv元数据文件。

  

ttu_collectionname_000001.csv
  ttu_collectionname_000002.csv
  ttu_collectionname_000003.csv ...等(这些文件中的每一个都是csv文件。)

我需要根据包含子文件夹和子子文件夹的文件名创建一个文件夹。每个顶级文件夹名称在数字序列中都是唯一的。每个顶级文件夹的每个子和子子文件夹名称都相同。

文件夹结构应如下所示:

  
      
  • ttu_collectionname_000001
      
        
    • 内容
        
          
      • 存档
      •   
      • 显示
      •   
    •   
    • 元数据
        
          
      • 存档
      •   
      • 显示
      •   
    •   
  •   
  • ttu_collectionname_000002
      
        
    • 内容
        
          
      • 存档
      •   
      • 显示
      •   
    •   
    • 元数据
        
          
      • 存档
      •   
      • 显示
      •   
    •   
  •   

然后我需要将每个文件移动到特定的子子文件夹。

文件 ttu_collectionname_000001.pdf 将移至 ttu_collectionname_000001 / content / display 文件夹。

文件 ttu_collectionname_000001.csv 将移至 ttu_collectionname_000001 / metadata / display 文件夹。

2 个答案:

答案 0 :(得分:2)

尝试:

set myFolder to "Mac OS X:Users:stark:Main Folder"
tell application "Finder" to set myFiles to folder myFolder's files as alias list
repeat with aFile in myFiles
    tell application "System Events" to set {fileName, fileExt} to {name, name extension} of aFile
    set baseName to text 1 thru ((get offset of "." & fileExt in fileName) - 1) of fileName
    do shell script "mkdir -p " & (quoted form of (POSIX path of myFolder)) & "/" & baseName & "/{\"content\",\"metadata\"}/{\"display\",\"archive\"}"
    tell application "System Events"
        if fileExt is "pdf" then move aFile to (myFolder & ":" & baseName & ":content:display" as text)
        if fileExt is "csv" then move aFile to (myFolder & ":" & baseName & ":metadata:display" as text)
    end tell
end repeat

答案 1 :(得分:0)

假设您的文件位于同一个文件夹中, before

这个AppleScript:

set pathToFolderOfTTUFiles to (path to the desktop as text) & "TTU:"
tell application "Finder"
    set theFiles to every item of folder pathToFolderOfTTUFiles whose name extension is not "csv" and kind is not "Folder"
    repeat with theFile in theFiles
        set lengthOfExtension to (length of (theFile's name extension as text)) + 1
        set fileNameWithoutExtension to text 1 through -(lengthOfExtension + 1) of (theFile's name as text)
        set theFolder to make new folder at folder pathToFolderOfTTUFiles with properties {name:fileNameWithoutExtension}

        set theContentFolder to make new folder at theFolder with properties {name:"content"}
        make new folder at theContentFolder with properties {name:"archive"}
        set theContentDisplayFolder to make new folder at theContentFolder with properties {name:"display"}
        set theMetadataFolder to make new folder at theFolder with properties {name:"metadata"}
        make new folder at theMetadataFolder with properties {name:"archive"}
        set theMetadataDisplayFolder to make new folder at theMetadataFolder with properties {name:"display"}

        move theFile to theContentDisplayFolder
        set pathToCSV to pathToFolderOfTTUFiles & fileNameWithoutExtension & ".csv"
        if exists pathToCSV then move pathToCSV to theMetadataDisplayFolder
    end repeat
end tell

创建: after