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
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!
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.
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
try
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
try
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:
030721_001.tif
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!
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
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: propertyparent_folder : (path todesktop) asstring – modify this to the location where you want the new date folder created
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?
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.
-marc
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.
-marc
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
hmm, not sure what i’m doing wrong here. before i posted i did try taking the iviewm.pro 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?
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.)
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
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.