View Full Version : file to folder applescript

Dec 30, 2011, 12:22 AM
I have a movies folder containing movie files (... pretty special eh!) and I would like an applescript script to be able to create new subfolders within that movies parent folder with names corresponding to the names of the movies and then move the movie file into the corresponding sub folder. That probably seems pretty confusing so here's the scenario:
I have the movies 300 and Avatar in my movies folder. I want to create new folders within the movies folder called 300 and Avatar. I want to move the two movie files into their respective newly created folders. However, I have much more than two movie files.

Could someone suggest a script for this? I assume it would be pretty short and simple...

Mark FX
Dec 30, 2011, 03:57 PM
Sorry to awnser your question with more questions.

Do you know any Applescript ?

How many Movie files are you talking about ?

Do you know, that if you have more than a couple of dozen files, that the
process would be very slow with Applescript ?

Awnser these questions, and I'll post some kind of Applescript solution for you.

Regards Mark

Dec 30, 2011, 04:23 PM
Hi Mark,
I know a very tiny little bit... but have forgotten most of it! However (like most people) I can look at a script and work out what it is doing...

There is about 45 items in the folder.

I didn't realise it would be slow but surely it will still be faster than doing them all by hand...?


Mark FX
Dec 30, 2011, 04:40 PM
Yes it will be faster than by hand.

I am writing and testing some Applescript, that might do the job for you.

But it's not as short and simple as you might think.
bare with me and I'll see what I can come up with.

Are your movies in the stabdard Movies folder of the user ?

Regards Mark

Dec 30, 2011, 05:06 PM
I really appreciate it mate.

They are in a folder called "movies" within the "movie folder in my home directory... /Users/home/Movies/Movies

Mark FX
Dec 30, 2011, 05:29 PM
The standard OSX foder is just ~/Movies not ~/Movies/Movies.

Have you created that sub folder yourself ?


Dec 30, 2011, 05:35 PM
Yes I created it. Within the ~/Movies folder I have tv shows and other videos so I have created a separate sub folder for my actual movies.

Mark FX
Dec 30, 2011, 06:07 PM
OK I'm still working on it for you.

It's gone midnight here, so I will post something for you tommorrow night.

Regards Mark

Mark FX
Dec 31, 2011, 03:04 PM
OK Sammy, I've come up with some Applescript code for you,
Its not too refined, but it does work.
Copy and Paste this code into the Applescript Editor, then you can
either run it from there, or you can save it as a compiled Applescript,
that can be double clicked like an app to run.

on run {}
set movies_folder to (path to movies folder) & "Movies" as text
set movies_folder to movies_folder as alias
on error error_message number error_number
display alert error_message & space & error_number
message "An Error occured while trying to find the folder " & (movies_folder as text) & "." & return &
"Or an Error occured, while trying to convert the folder " & (movies_folder as text) & return &
"into type, Applescript Alias." as warning giving up after 30
end try
tell application "Finder"
set all_movie_files to every file in movies_folder as list
set movie_files to name of every file in movies_folder as list
set default_text_item_delimiters to AppleScript's text item delimiters
set AppleScript's text item delimiters to "."
set movie_file_names to {}
repeat with movie_name from 1 to length of movie_files
set movie_file_name to text item 1 of (item movie_name of movie_files) as text
set end of movie_file_names to movie_file_name
set new_movie_folder to make new folder in movies_folder with properties {name:movie_file_name}
delay (0.25)
if new_movie_folder exists then
move (item movie_name of all_movie_files) to new_movie_folder replacing yes
on error error_message number error_number
display alert error_message & space & error_number
message "An Error occured while trying to move the movie file." & return &
movie_file_name & ", to the newly created movie folder." as warning giving up after 30
end try
end if
on error
if (movie_name + 1) is less than (length of movie_files) then
set movie_name to (movie_name + 1)
else if (movie_name + 1) is equal to (length of movie_files) then
exit repeat
end if
end try
end repeat
set AppleScript's text item delimiters to default_text_item_delimiters
end tell
end run

hope this is what your looking for, as you can run it time and again, as
it wont redo folders that already exist.

Regards Mark

Dec 31, 2011, 11:20 PM
Thanks sooooooo much for doing that. However, I have a small problem... I am probably doing something very stupidly wrong.

In the third line, I assume I was supposed to replace
(path to movies folder)
with the actual path...

So my third line reads
set movies_folder to /Users/sam/Movies/Movies & "Movies" as text
but when I go to compile it, I get an error
Syntax Error
Expected expression but found /.

What am I doing wrong??

Sorry, but I never properly learnt any applescript, I used to just mess around and copy and paste things that I got to work...

Red Menace
Dec 31, 2011, 11:31 PM
The Finder does not like POSIX (slash delimited) paths, but in your example the main problem is that you forgot to quote the text string.

There are several built-in paths that a script can get - path to movies folder is a Standard Additions (a scripting addition) command to return the path to the current user's movies folder, so in this particular case you don't need to change it.

Jan 1, 2012, 12:05 AM
That works perfect!!!

Ummm... say I wanted to reverse this script, ie take the files out of their individual folders, would it be hard to make a script for that?

Red Menace
Jan 1, 2012, 01:01 AM
Not really too difficult, although it depends a bit on how many levels deep you want to go, or if there are folders you want to leave alone. If there are just the new folders that the previous script created, you can get the documents of the entire contents of the original folder and move the files there, for example:

set sourceFolder to (choose folder with prompt "Files of the chosen folder will be moved out of their containing folders.")
tell application "Finder" to repeat with aFile in (get files of entire contents of sourceFolder)
move aFile to sourceFolder
on error errmess -- duplicate name, etc
log errmess -- just skip it and log the error
end try
end repeat

Although I usually make some test file copies to play with so that I don't wind up having to find them or put them back if things don't work out right...

Mark FX
Jan 1, 2012, 12:39 PM
As Red Menace has advised, you could alter the script I posted, to do the reverse job.

This line in the script, could be changed to list all of the folders in the Movies
folder, instead of the files.

set all_movie_files to every file in movies_folder as list

changed to this

set all_movie_folders to every folder in movies_folder as list

this is not the only line of code you would need to change, but it gives you
the ideas involved to adapt my script.

I would advise you to buy a good book on the basics of Applescript, and
this sort of script will suddenly become quit simple for you to write.
And I think you wont regret learning Applescript, as its great fun, and a useful
tool for little utility programs.

Regards Mark

Jan 1, 2012, 04:36 PM
This works perfectly guys!!! Thank you soooo much for your help :)

And yes, I will try and learn some Applescript, I imagine it would be quite handy.