jFish jFish - 1 year ago 194
AppleScript Question

Applescript: How to create new folder from file name, create subfolders, move file into subfolder

I would like to create an applescript application that I can drag a file (such as an Illustrator file in my case) onto in order to create a new folder with the name of the original (Illustrator) file. I want that new folder to be created in the same location as the Illustrator file.

Next, I want to create 3 subfolders within my new folder. (Ex. One with the name of ff, one named mr, and one name NX.)

Within one of the created subfolders, I want another subfolder created. (Ex. named Hires, inside the ff folder.)

Finally, I want the illustrator file to be moved into the (mr) subfolder that was created.

Below is a screen shot of the file structure that I would like to create.

Desired file structure

I have been messing around with some different scripts but haven't gotten anything to work as desired.

Thanks so much in advance.

Answer Source

This is a simple solution taking advantage of the mkdir shell command which is able to create complex folder structures in one line.

Save the script as application

on open theFiles
    set folderStructure to "/{ff/Hires,mr,NX}"
    repeat with aFile in theFiles
        tell application "System Events"
            set {name:Nm, name extension:Ex} to aFile
            set baseFileName to text 1 thru ((get offset of "." & Ex in Nm) - 1) of Nm
            set baseFolder to POSIX path of container of aFile & "/" & baseFileName
        end tell
        do shell script "/bin/mkdir -p " & quoted form of baseFolder & folderStructure
        do shell script "/bin/mv " & quoted form of POSIX path of aFile & space & quoted form of (baseFolder & "/mr")
    end repeat
end open
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download