Apple Event: May 7th at 7 am PT

Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

Creating list of folders from filenames with files organized in subfolders within

Hi all. This is my first post in the Apple Support Community and I really hope that someone can help me as I'm using apple script for the first time.


I have a list of hundreds of items that are each .jpg, .cr2, or .mov. Certain filenames have multiple extensions, ex: Q95A7170.CR2 & Q95A7170.jpg.


I'm looking for an applescript or automator action that will allow me to create a folder out of each filename with 5 subfolders in it


I need the subfolders to be called:

AUDIO

COLOR

METADATA

PICTURE

PROXY


In this instance I need all the .mov, .cr2, and .jpgs to be routed to the PICTURE folder. In later instances I'll need to route .mxf to PICTURE AND .xml .sif & .thm to METADATA.


So for example I have a list of that looks like this


User uploaded file


and I want a script that will make it look like this (i did all of this manually: creating the folders, renaming them, and putting the specific files into the picture folder)


User uploaded file

(not nearly a fraction of the list -- there are hundreds and hundreds of files)


I've been researching applescripts for hours but the only script that i could make work and was very useful was this one to create the folders out of the names of the files:

tell application "Finder"

set selected to selection

set current_folder to item 1 of selected

set mlist to every file of current_folder

repeat with this_file in mlist

set cur_ext to name extension of this_file

set new_name to text 1 thru -((length of cur_ext) + 2) of (name of this_file as text)

set new_folder to makenewfolderwith properties {name:new_name} atcurrent_folder


movethis_filetonew_folder

end repeat

end tell


Even with this script I had to manually copy all of the duplicate file named .jpgs out of the folder before it would properly run.



Also I've found these scripts but I'm not sure what information to change. Like I said, I don't know much about code at all and I'm finding more research about it is just digging me deeper in a hole that I'm not entirely ready for.


Here are the other two scripts that CLAIM they can do what I'm looking for, but I haven't gotten them to work.


1.

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


2.

set myFolder to "FOLDERPATH"

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


(the .pdf, .csv extensions are examples from the person who posted that discussion.)

I tried using both of these codes but to no avail.


Overall, this process is incredibly time-consuming and this is the first time I am using apple script so I know almost nothing about code.


Also I don't know if this changes anything, but all of these files are on an external and I'm trying to save them back on this external.


PLEASE HELP! I am a night assistant editor doing very tedious work at 3:00 AM or later and anything that would expedite this process would be so unbelievably appreciated. Please, someone who's more experience with code and who's smarter than I am on this topic, help me! I know there must be a way to automate this process!


Please and so many thank yous! Really, this code would save me 40+ hours of eye-tiring work.


Shelby

Mac Pro, Mac OS X (10.6.8), 2 x 2.26 GHz Quad-Core Intel Xeon

Posted on Mar 22, 2013 12:11 AM

Reply
28 replies

Apr 16, 2013 7:55 PM in response to belbyb

curious. the code you posted works fine on my box. The only thing I see that's odd is that you've doubled up the 'set targetFolder...' line, but that shouldn't do anything. The lines you added look well-formed.


do the drag and drop path like I suggested before, and if still fails click on the Events button near the bottom of the Applescript editor wind and copy the diagnostics up here so that I can take a look at them.

Apr 16, 2013 8:02 PM in response to twtwtw

Hey -- sorry I missed your reply before.


So I dragged the folder over and clicked alias to define the path and clicked "run". Again, nothing.


Here are the events:

tell application "AppleScript Editor"

get every file of «class cfol» "/Volumes/Data/Users/shelbybryant/Desktop/20120818-XF300-01/CONTENTS/CLIPS001" whose visible = true


--> {file "Data:Users:shelbybryant:Desktop:20120818-XF300-01:CONTENTS:CLIPS001:INDEX.MIF" }

get name of file "Data:Users:shelbybryant:Desktop:20120818-XF300-01:CONTENTS:CLIPS001:INDEX.MIF"


--> "INDEX.MIF"

get «class extn» of file "Data:Users:shelbybryant:Desktop:20120818-XF300-01:CONTENTS:CLIPS001:INDEX.MIF"


--> "MIF"

exists «class cfol» "INDEX" of «class cfol» "/Volumes/Data/Users/shelbybryant/Desktop/20120818-XF300-01/CONTENTS/CLIPS001"


--> true

get «class posx» of «class cfol» "INDEX" of «class cfol» "/Volumes/Data/Users/shelbybryant/Desktop/20120818-XF300-01/CONTENTS/CLIPS001"


--> "/Volumes/Data/Users/shelbybryant/Desktop/20120818-XF300-01/CONTENTS/CLIPS001/IN DEX"

exists «class cfol» "INDEX" of «class cfol» "/Volumes/Data/Users/shelbybryant/Desktop/20120818-XF300-01/CONTENTS/CLIPS001"


--> true

get «class posx» of «class cfol» "INDEX" of «class cfol» "/Volumes/Data/Users/shelbybryant/Desktop/20120818-XF300-01/CONTENTS/CLIPS001"


--> "/Volumes/Data/Users/shelbybryant/Desktop/20120818-XF300-01/CONTENTS/CLIPS001/IN DEX"

exists «class cfol» "AUDIO" of «class cfol» "/Volumes/Data/Users/shelbybryant/Desktop/20120818-XF300-01/CONTENTS/CLIPS001/I NDEX"


--> true

get «class posx» of «class cfol» "AUDIO" of «class cfol» "/Volumes/Data/Users/shelbybryant/Desktop/20120818-XF300-01/CONTENTS/CLIPS001/I NDEX"


--> "/Volumes/Data/Users/shelbybryant/Desktop/20120818-XF300-01/CONTENTS/CLIPS001/IN DEX/AUDIO"

exists «class cfol» "COLOR" of «class cfol» "/Volumes/Data/Users/shelbybryant/Desktop/20120818-XF300-01/CONTENTS/CLIPS001/I NDEX"


--> true

get «class posx» of «class cfol» "COLOR" of «class cfol» "/Volumes/Data/Users/shelbybryant/Desktop/20120818-XF300-01/CONTENTS/CLIPS001/I NDEX"


--> "/Volumes/Data/Users/shelbybryant/Desktop/20120818-XF300-01/CONTENTS/CLIPS001/IN DEX/COLOR"

exists «class cfol» "METADATA" of «class cfol» "/Volumes/Data/Users/shelbybryant/Desktop/20120818-XF300-01/CONTENTS/CLIPS001/I NDEX"


--> true

get «class posx» of «class cfol» "METADATA" of «class cfol» "/Volumes/Data/Users/shelbybryant/Desktop/20120818-XF300-01/CONTENTS/CLIPS001/I NDEX"


--> "/Volumes/Data/Users/shelbybryant/Desktop/20120818-XF300-01/CONTENTS/CLIPS001/IN DEX/METADATA"

exists «class cfol» "PICTURE" of «class cfol» "/Volumes/Data/Users/shelbybryant/Desktop/20120818-XF300-01/CONTENTS/CLIPS001/I NDEX"


--> true

get «class posx» of «class cfol» "PICTURE" of «class cfol» "/Volumes/Data/Users/shelbybryant/Desktop/20120818-XF300-01/CONTENTS/CLIPS001/I NDEX"


--> "/Volumes/Data/Users/shelbybryant/Desktop/20120818-XF300-01/CONTENTS/CLIPS001/IN DEX/PICTURE"

exists «class cfol» "PROXY" of «class cfol» "/Volumes/Data/Users/shelbybryant/Desktop/20120818-XF300-01/CONTENTS/CLIPS001/I NDEX"


--> true

get «class posx» of «class cfol» "PROXY" of «class cfol» "/Volumes/Data/Users/shelbybryant/Desktop/20120818-XF300-01/CONTENTS/CLIPS001/I NDEX"


--> "/Volumes/Data/Users/shelbybryant/Desktop/20120818-XF300-01/CONTENTS/CLIPS001/IN DEX/PROXY"

end tell


Also, don't know if this helps but here's a screen shot. You can see no subfolders were created and nothing, consequently, was sorted.


User uploaded file

Apr 16, 2013 8:25 PM in response to twtwtw

I just realized something... is this code not working because it was originally generated to turn files into folders? So with this particular batch, for example, there are no files because all of the folders are already made. I think just the files existed in a list for the 5D footage.


Hmm.. so if I'm right and this is the problem, which section of code should i delete? Now instead of creating these folders, I just need it to create the subfolders and to sort the extensions within.

Apr 16, 2013 8:27 PM in response to twtwtw

Just tried this:

set workFolder to "/Volumes/Data/Users/shelbybryant/Desktop/20120818-XF300-01/CONTENTS/CLIPS001"

tell application "System Events"


-- create subfolders, if needed

repeat with thisSubfolderName in {"AUDIO", "COLOR", "METADATA", "PICTURE", "PROXY"}

my checkForFolder({parentFolder:targetFolder, folderName:thisSubfolderName})

end repeat



-- sort files into subFolders

if fileExt is "jpg" or fileExt is "cr2" or fileExt is "mov" then


movethisFiletotargetFolder & "/PICTURE"

else if fileExt is "xml" or fileExt is "sif" or fileExt is "thm" then

move thisFile to targetFolder & "/METADATA"

else if fileExt is "mxf" then


movethisFiletotargetFolder & "/PICTURE"

else if fileExt is "cif" or fileExt is "cpf" then

move thisFile to targetFolder & "/METADATA"

else


-- skip unknown file extensions

end if

end repeat

end tell


to checkForFolder({parentFolder:fParent, folderName:fName})

-- find or create a folder

tell application "System Events"

if not (exists folder fName of folder fParent) then

set output to POSIX path of (makenewfolderat end of folderfParentwith properties {name:fName})

else

set output to (POSIX path of (folder fName of folder fParent))

end if

end tell


-- returns a POSIX path

return output

end checkForFolder


But I'm getting a "syntax error"

Apr 16, 2013 8:42 PM in response to belbyb

belbyb wrote:


I just realized something... is this code not working because it was originally generated to turn files into folders? So with this particular batch, for example, there are no files because all of the folders are already made. I think just the files existed in a list for the 5D footage.


Hmm.. so if I'm right and this is the problem, which section of code should i delete? Now instead of creating these folders, I just need it to create the subfolders and to sort the extensions within.


I'm not sure I'm following this entirely, but yes, the original script was designed to turn files (only files) into folders. If what you're saying is that the main folders exist but the subfolders don't, then you need to create the subfolders first and then use the same sort routine. So, right after the tell application "System Events" line, add a routine like this:


tell application "System Events"


-- do folders first

set foldersToProcess to folders of folderworkFolder whose visible is true

repeat with thisFolder in foldersToProcess


-- create subfolders, if needed

repeat with thisSubfolderName in {"AUDIO", "COLOR", "METADATA", "PICTURE", "PROXY"}

my checkForFolder({parentFolder:POSIX path of thisFolder, folderName:thisSubfolderName})

end repeat

end repeat



-- get files to work on

set filesToProcess to files of folder workFolder whose visible is true

...

...

...

Apr 16, 2013 9:05 PM in response to twtwtw

oh, I just looked at the screenshot and noticed the files were partly sorted already (in folders of the correct name, but not sorted into subfolders). That needs another tweak, since the script currently doesn't look inside of folders. Note that I've moved the sorting bit into a handler because files have to be sorted on two different levels.


set workFolder to "/Volumes/Data/Users/shelbybryant/Desktop/20120818-XF300-01/CONTENTS/CLIPS001"

tell application "System Events"


-- do folders first

set foldersToProcess to folders of folderworkFolder whose visible is true

repeat with thisFolder in foldersToProcess


-- create subfolders, if needed

repeat with thisSubfolderName in {"AUDIO", "COLOR", "METADATA", "PICTURE", "PROXY"}

my checkForFolder({parentFolder:POSIX path of thisFolder, folderName:thisSubfolderName})

end repeat



-- sort files already in folders but not yet subfoldered

set partiallySortedFiles to files of thisFolder

repeat with thisFile in partiallySortedFiles

my sortThingsOut(POSIX path of thisFolder, thisFile)

end repeat

end repeat



-- work on files in main folder

set filesToProcess to files of folder workFolder whose visible is true

repeat with thisFile in filesToProcess

set {fileName, fileExt} to {name, name extension} of thisFile



-- get name of file without extension

set rootName to text 1 thru -((length of fileExt) + 2) of fileName



-- make sure a correctly named folder exists

set targetFolder to my checkForFolder({parentFolder:workFolder, folderName:rootName})



-- create subfolders, if needed

repeat with thisSubfolderName in {"AUDIO", "COLOR", "METADATA", "PICTURE", "PROXY"}

my checkForFolder({parentFolder:targetFolder, folderName:thisSubfolderName})

end repeat



--sort files in main folder

my sortThingsOut(targetFolder, thisFile)

end repeat

end tell


to sortThingsOut(targetFolder, aFile)

tell application "System Events"

set {fileName, fileExt} to {name, name extension} of aFile


--sort

if fileExt is "jpg" or fileExt is "cr2" or fileExt is "mov" then

move aFile to targetFolder & "/PICTURE"

else if fileExt is "xml" or fileExt is "sif" or fileExt is "thm" then

move aFile to targetFolder & "/METADATA"

else if fileExt is "mxf" then


movethisFiletotargetFolder & "/PICTURE"

else if aFile is "cif" or fileExt is "cpf" then

move thisFile to targetFolder & "/METADATA"

else


-- skip unknown file extensions

end if

end tell

end sortThingsOut


to checkForFolder({parentFolder:fParent, folderName:fName})


-- find or create a folder

tell application "System Events"

if not (exists folder fName of folder fParent) then

set output to POSIX path of (makenewfolderat end of folderfParentwith properties {name:fName})

else

set output to (POSIX path of (folder fName of folder fParent))

end if

end tell



-- returns a POSIX path

return output

end checkForFolder


Sorry about the undefined variable flub. shouldn't get that here.

Apr 16, 2013 9:11 PM in response to twtwtw

ok so I just tried that code and got this error message:

User uploaded file


And that time the code DID do something. It created subfolders in the FIRST folder: screen shot below. Did not sort the items into any of the subfolders and did not create subfolders in any of the following clip folders (BC0072, BC0073, etc.)

User uploaded file

Apr 16, 2013 9:15 PM in response to belbyb

Hey, I'm working off the cuff here; you need to use your eyes too. If you look at that handler you'll see I started renaming thisFile to aFile and didn't get them all. Make those last two or three changes and you should be good.


And of course it only created one round of folders; it crapped out the first time it tried to sort.

Apr 16, 2013 9:54 PM in response to twtwtw

little to no experience *** wow don't know how "know" slipped out before. The later into the night that it gets at work, the crazier I feel. Code is working perfectly. I truly appreciate you taking your time to help me. Having this code makes my life so much easier and takes a huge burden off of me. I am so grateful that you took the time to help me. Thank you again.

Creating list of folders from filenames with files organized in subfolders within

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple ID.