0

I have an Applescript question that is much more complex than I can construct. I have been searching for the past couple of days, and I cannot find any script like this, nor can I find enough information to piece one together with my limited knowledge.

I have multiple files with structured names. Each file has the following name structure:

ttu_collectionname_000001.pdf
ttu_collectionname_000002.mp3
ttu_collectionname_000003.pdf ... etc. (Each of these files are of varying file types.)

There is also a csv metadata file associated with each of the original files.

ttu_collectionname_000001.csv
ttu_collectionname_000002.csv
ttu_collectionname_000003.csv ... etc. (Each of these files are csv files.)

I need to create a folder based on the name of the file with sub and sub-subfolders. Each top-level folder name will be unique in the number sequence. Each sub and sub-subfolder name will be the same for each top-level folder.

The folder structure should look like this:

  • ttu_collectionname_000001
    • content
      • archive
      • display
    • metadata
      • archive
      • display
  • ttu_collectionname_000002
    • content
      • archive
      • display
    • metadata
      • archive
      • display

I then need to move the each file to a particular sub-subfolder.

The file ttu_collectionname_000001.pdf would be moved to the ttu_collectionname_000001/content/display folder.

The file ttu_collectionname_000001.csv would be moved to the ttu_collectionname_000001/metadata/display folder.

2 Answers 2

2

Try:

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
Sign up to request clarification or add additional context in comments.

4 Comments

Impressive. I'd forgotten about mkdir, and I never knew that one command could create multiple directories.
Thanks ... it is useful now and then.
Brilliant! Thank you so much! I have got to learn more Applescript. This was so helpful!
No, this has worked great. I must have inadvertently un-checked the answer. Sorry for the confusion.
0

Assuming your files are in the same folder, before

this 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

creates this: after

3 Comments

Thanks, John! I ran this script, too, and it did everything but move the csv files. Did I miss something?
Where are your CSV files? In my example, they're in the same folder as the PDFs and MP3s (see my first screenshot).
John, I am not sure what I was doing wrong earlier, but I got it to work. Thanks so much for your help!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.