automating file organization

hello all,
i am just learning applescript and would appreciate any help in my applescript endeveour.
i have some parts figured out and many not.
here’s what i am trying to do:
automate my image file organization. i would like to create an applescript that when i drop files (image files) on it it will

  1. create a folder in a defined location with part of the date as it’s name (i.e. 2003_07) – i got this part working!
  2. rename the files with the date and a sequential number (i.e. 030721_001, 030721_002, etc.). however if there are existing files in the folder then start the sequential number from the last existing file.
  3. move the files to the folder created in step 1

so i have created some code that will make a folder with the date in the form 2003_07 and move the files into it.
i can’t figure out how to specify where it does this though. right now it just creates it on my desktop. how does one specify the location path?

on open added_items
	tell application "Finder"
		set the_date to do shell script "date +%Y_%m" as string
		if (folder the_date exists) is not true then
				make new folder with properties {name:the_date}
			on error the_error
				display dialog the_error giving up after 30
			end try
		end if
	end tell
	repeat with i from 1 to the number of items in added_items
		set the_file to item i of added_items
		tell application "Finder"
			if (file the_file exists) then
					move file the_file to folder the_date
				on error the_error
					display dialog the_error giving up after 10
				end try
			end if
		end tell
	end repeat
end open

but i can’t figure out the code that deals with renaming the files.
it is also important that they keep there existing extension (i.e. .tif, .jpg)
in the end i woul like them to look something like this:
this is the closest i’ve come to tackling the file names:

set the_date to do shell script "date +%y%m%d_" as string
set the_plusnumber to 1 as string
set the_Count to text -3 thru -1 of ("000" & the_plusnumber as string)
set file_name to {the_date, the_Count} as string

ideally in the end i would like it to automatically import into iviewmedia pro, but that’s a whole nother can’o’worms and i can try to add that later.

i hope this is clear. please let me know if anything needs more clarification.
also i welcome any suggestions on making the existing code better as my applescripting sucks booty.
i would be grateful for any assistance or help. thank you

If you save the following script as an application, you can drag and drop your files on to it. It will check for a folder named for the current month in some default location (modify the property at the top of the script to your desired location – right now it is the desktop) and create a new folder if it doesn’t exist. It then checks the folder for files named for the date until it gets to a new file name. Then it renames the dropped file to the new name and moves the file to the target folder. The only potential error I see is if one of the files in the source folder already has the new name. If this happens, the Finder will tell you there is an error (a file with the name already exists). Anyway, this should get you going. Oh, one more thing, this script isn’t too intelligent about the naming convention when it comes to different file types (this may be a good thing, I don’t know your exact needs). By this I mean that if you drop an HTML file on to the script (with the proper “.html” extension) then it will rename the file “2003_07_001.html” and move it to the target folder. Subsequent HTML files with the “.html” extension will be “2003_07_002.html” etc. Files with a different extension, such as “.jpg” will start back at “2003_07_001.jpg” since this is a different file name considering the extension. If you want to ignore the extension and sequentially number all files, you’ll have to strip the extensions altogether or add a lot more code. Anyway, on to the script!

Hope this helps,

thank you Jon! fantastic.
i played with the code this morning and made a few changes:

  • i took out:
set old_name to name of the_file

because it doesn’t seem like old_name is used at all, right?

  • i took out the dialog box.
  • i changed the code so that now it creates a year folder first (ie.2003) then carries out the other events within that parent folder.
  • changed the folder and file formats a bit

ok. i have two questions:
-very basic, but what is the correct way to specify the location? for example, to set the location of the target_parent_folder to something other than the desktop.
would it be:

property target_parent_folder : (path to home & "specific folder") as string


property target_parent_folder : (computer:Users:Username:folder)

i can find this information in the dictionaries. where is this stuff documented?

  • second is the problem that you already mentioned with files having different extensions getting their own sequential numbering.

but i actually don’t understand why this is happening. i guess i don’t completely understand what the code is doing. i thought that it takes the name of the file as a variable and the name of the extension as another variable and at the end it renames the file with the date and then adds the extension again. no? so then why is the extension a problem. or am i just completely confused?!
any ideas on how i can fix this problem?

thank you again. this is a great help, and my little applescript knowledge is certainly growing. look forward to your response
- poultryfarm

here is the code:
property parent_folder : (path to desktop) as string modify this to the location where you want the new date folder created

on open added_items
set the_year to do shell script “date +%Y” as string
set the_folder_date to do shell script “date +%Y_%m” as string
set the_file_date to do shell script “date +%y%m%d” as string
set target_parent_folder to (parent_folder & the_year & “:”)
set target_folder to (target_parent_folder & the_folder_date & “:”)

      [color=red]tell[/color] [color=blue]application[/color] "Finder"
           [color=red]if[/color] ([color=red]my[/color] [color=green]file_exists[/color]([color=green]target_parent_folder[/color])) = [color=blue]false[/color] [color=red]then[/color] ¬
                [color=blue]make[/color] [color=blue]new[/color] [color=blue]folder[/color] [color=blue]at[/color] [color=blue]folder[/color] [color=green]parent_folder[/color] [color=blue]with properties[/color] {[color=blue]name[/color]:[color=green]the_year[/color]}
           [color=red]if[/color] ([color=red]my[/color] [color=green]file_exists[/color]([color=green]target_folder[/color])) = [color=blue]false[/color] [color=red]then[/color] ¬
                [color=blue]make[/color] [color=blue]new[/color] [color=blue]folder[/color] [color=blue]at[/color] [color=blue]folder[/color] [color=green]target_parent_folder[/color] [color=blue]with properties[/color] {[color=blue]name[/color]:[color=green]the_folder_date[/color]}
           [color=red]repeat[/color] [color=red]with[/color] [color=green]i[/color] [color=red]from[/color] 1 [color=red]to[/color] ([color=blue]count[/color] [color=red]of[/color] [color=green]added_items[/color])
                [color=red]set[/color] [color=green]the_file[/color] [color=red]to[/color] ([color=blue]item[/color] [color=green]i[/color] [color=red]of[/color] [color=green]added_items[/color])
                --[color=olive]set old_name to name of the_file[/color]
                [color=red]set[/color] [color=green]source_folder[/color] [color=red]to[/color] ([color=blue]container[/color] [color=red]of[/color] [color=green]the_file[/color]) [color=red]as[/color] [color=blue]string[/color]
                [color=red]set[/color] [color=green]the_extension[/color] [color=red]to[/color] [color=blue]name extension[/color] [color=red]of[/color] [color=green]the_file[/color]
                [color=red]if[/color] [color=green]the_extension[/color] [color=red]is[/color] [color=red]not[/color] "" [color=red]then[/color] [color=red]set[/color] [color=green]the_extension[/color] [color=red]to[/color] "." & [color=green]the_extension[/color]
                [color=red]copy[/color] {0, [color=blue]true[/color]} [color=red]to[/color] {[color=green]add_num[/color], [color=green]file_exists[/color]}
                [color=red]repeat[/color] [color=red]while[/color] [color=green]file_exists[/color] = [color=blue]true[/color]
                     [color=red]set[/color] [color=green]add_num[/color] [color=red]to[/color] ([color=green]add_num[/color] + 1)
                     [color=red]set[/color] [color=green]target_file[/color] [color=red]to[/color] [color=green]the_file_date[/color] & "_" & ([color=red]my[/color] [color=green]add_leading_zeros[/color]([color=green]add_num[/color], 999)) & [color=green]the_extension[/color]
                     [color=red]set[/color] [color=green]file_exists[/color] [color=red]to[/color] ([color=red]my[/color] [color=green]file_exists[/color]([color=green]target_folder[/color] & [color=green]target_file[/color]))
                [color=red]end[/color] [color=red]repeat[/color]
                [color=red]set[/color] [color=blue]name[/color] [color=red]of[/color] [color=green]the_file[/color] [color=red]to[/color] [color=green]target_file[/color]
                [color=blue]move[/color] [color=blue]file[/color] ([color=green]source_folder[/color] & [color=green]target_file[/color]) [color=blue]to[/color] [color=blue]folder[/color] [color=green]target_folder[/color]
           [color=red]end[/color] [color=red]repeat[/color]
      [color=red]end[/color] [color=red]tell[/color]
 [color=red]on[/color] [color=red]error[/color] [color=green]the_error[/color]
      [color=blue]display dialog[/color] [color=green]the_error[/color] [color=blue]buttons[/color] {"OK"} [color=blue]default button[/color] 1 [color=blue]with icon[/color] 0 [color=blue]giving up after[/color] 10
 [color=red]end[/color] [color=red]try[/color]

end open

on [color=green]file_exists/color
set the_path to (the_file as alias)
return true
on error
return false
end try
end file_exists

on add_leading_zeros(the_number, total_number) – assumes integers or strings
copy {(count of (characters of (total_number as string))), (count of (characters of (the_number as string))), “”} to {i, j, z}
repeat while j < i
copy {(z & “0”), (j + 1)} to {z, j}
end repeat
return z & (the_number as string)
end add_leading_zeros

You’re welcome.

See the top line of the following script.

No. It checks for an exact filename including the extension and always starts checking from number 1 so if “…001.jpg” doesn’t exist but “…001.pdf” does and the file it is checking for has a “.jpg” extension, it will be named “…001.jpg”

Yes, see the following script. In this script, instead of checking for a name blindly, it queries the target folder for a list of all filenames without their extensions then it sequentially builds a new filename until it is unique, then it adds the that extension-less name to the list of files (so it doesn’t have to re-query the target folder for each file in the batch) and then finally re-adds the extension. The result is a numbered file list with extensions but sequential regardless of extension. As you add more files to the target folder, it will take the script longer to generate the list of extension-less file names. Probably not so clear but by studying the code, it should make more sense.

Again, save this script as an application. It’s probably best just to drop files on the application but folders will be renamed appropriately but the contents of the folder will not be touched.


you’re a wizard jon! thank you
what a beautiful script… it works!
i added some code finally to import the files into iView Media Pro and save the catalog (and i took out the dialog window again).
wow. i think that is everything on my wish list. thank you very very much.

understand this now thanks. but, for future reference where are some of these applescript features documented? i have a hard time understanding the dictionaries which seem to be more general. is there a place that i should be looking that lists these sorts of standards?

here is the final code:

sorry. one more idea!
would love to be able to insure that the extensions are lowercase. found this change case code from apple. some way we could incorporate this in there?
or is there an even easier way? i’ll play around with it.

ok. so i tried to insert the lowercase code to change the case of just the extension but i keep getting an error message (“‘the file’ doesn’t understand the count message”). when i hide the lowercase code with ‘–’ the script runs fine. any ideas?

Why do you keep stripping out the run handler? You should really keep this in as good design. Many people don’t understand how a droplet works and will double-click it to start it. Without the run handler, double-clicking the script will launch it but without the run handler (and the descriptive dialog) the script will just quit. Even if you are the only one using it, as long as you drag and drop files on to the icon, you’ll never see the dialog. Again, it’s just good design to have it there.

Anyway, as to the lowercase change, no problem except you are making it much harder than it needs to be:


i see what you mean. you’re code is much simpler. thanks
convincing points about the dialog window… i’ll leave it in then.
thank you again for all of your help, i appreciate it.

I seem to have encountered a problem with the script. i can’t seem to figure out where the problem is.
when i run the script and the month folder already exists i get an error message that says
“can’t make alias “path:to:image:imagename.jpg” into a <>.”

if i delete the existing month folder the script runs fine. i thought that the script took existing folders into account, but can’t seem to figure this out.
any help would be grately appreciated.

my second question is when i try and import files into iview mediaPro. When i import a lot of files at one time it appears that the applescript is to fast for Iview to handle and it will give an error message that i am trying to import an image while the program is currently importing. Perhaps an answer would be to slow down the applescript?, but that’s not such a great solution. or is it possible to maybe make a list of the files and only at the end import them all at once… currently the applescript makes a loop with each file and imports each file seperately.

any ideas and help would be lovely.
thank you in adance.

I don’t have iview mediaPro to test but the code for sorting the files is sound on my machine. Taking your code for importing to iview mediaPro out and running the script several times always works for me regardless of whether the month folder already exists or not. The code you added for a importing the file into iview mediaPro, on the other hand, does not seem sound. You are passing a reference to a file path that no longer exists since you moved it to the new file.

Your code

open "path:to:iviewmediapro catalog" 
tell application "iView MediaPro"
    open the_file
    save window 1
end tell 

should probably be:

open "path:to:iviewmediapro catalog" as alias
tell application "iView MediaPro"
    open ((target_folder  & target_file) as alias)
    delay 5 --a value in seconds to allow the app to add the file before continuing
    save window 1
end tell 


thanks for your reply jonn8

hmm, not sure what i’m doing wrong here. before i posted i did try taking the script out, and have just tried that again and i still get an error message.
wondering if i don’t have my folder path defined correctly?

wait a minute… perhaps i need to check my permissions? i’ll try repairing them and see what happens after.

regarding your suggested iview media code. thanks for the improvement and i’ll try with the delay.
what does the “as alias” do exactly?

That’s possible.

This is doubtful.

It tells the app that you are referring to a file path and not just a string.


as you warned, repairing permissions didn’t do jack.
here is the complete script, including my path defiinitions, with the iviewmedia code dashed out.
still not sure what’s wrong.

Copying the code you just pasted into a new Script Editor window, saving as an application, and dragging files on to it works perfectly on my system. The first time it ran it created the folder “~/Pictures/2003/” and “~/Pictures/2003/2003_08/” and copied files to it. On subsequent drag and drops, the files are added to “~/Pictures/2003/2003_08/” in proper sequential order with lowercase extensions. I’ve urn in 10 times without an error. I then deleted the “~/Pictures/2003/2003_08/” folder and it recreated it without a problem and restarted the numbering. Perhaps you could try logging in and then out, opening the application, recompiling, doing a save as, and see if that helps. Other than that, I’ve got no clue. What version of Mac OS X are you running? What about AS? (My system is Mac OS X 10.2.6/AppleScript 1.9.1.)


thanks jonn i appreciate your time and help

i restarted… and no luck.
i am using 10.2.6, please excuse my ignorance but i can’t seem to figure out where i find out what applescipt version i have. i saved as in both script editor 1.9 and 2. i still get the same error message. strange

display dialog AppleScript's version

result: 1.9.1

Ah, I found the error. The error you mentioned was the raw code for “alias list” and I couldn’t figure out why a jpg would throw an error since the only place “alias list” was used was in the get_existing_files handler and that should have been a folder, not a file throwing the error. Then it dawned on me that you must have had only one file in the folder! (I always tested with multiple files.) With only one file in the folder (or none), you will get an error trying to coerce every file to a list since it is only one. Silly Finder. I trapped this error. I also changed one other part of the script since I was using a variable name that was the same as a handler name and while not a problem, per se, for clarity sake, I changed it. Anyway, here’s your script.


yet again jonn8 thank you… after studying your notes and the script for a while i actually understand your changes. my little applescript brain is expanding.

i like your clipboard error addition… that’s nice.