View Full Version : Simple Applescript in the Finder

Oct 2, 2010, 03:29 AM
As part of research for a book on vintage port, I am taking many photos of menus and pages of cellar books. Many. The camera is a DSC-W180, which names files DSC0nnnn.JPG (eg, DSC01234.JPG). Alas, the camera can’t count past 9999, it then going back to 1. Whoops.

Please, is it easy to add a button/command/keystroke somewhere in or near the Finder that does something like the following pseudocode (where ‘•’s represent tabbed indentation):

• For All Selected Files
•• If FileName is of the format “DSC0nnnn.JPG” Then
••• If FileCreationDate is >= 21:59 on Sun 26 Sept 2010 Then
•••• Rename file to “DSC1nnnn.JPG”.
••• End If
•• End If
• End For All


OSX 10.6.4; iMac 5,2; 1.83Ghz. Xcode 3.2.4 is installed.

Thank you kindly.

Oct 2, 2010, 01:54 PM
One option would be to get the program A Better Finder Rename


I don't use it but I suspect it will do what you want and a lot more.

Automator might do this for you but I never use that either.

Applescript can definitely do this. If you are not happy with the first two solutions repost and we'll figure that out.

Oct 2, 2010, 02:40 PM
Thank you. I will give the former a test.

However, it does feel that AppleScript really ought to be able to do this easily. It has advantages:
• I can be confident that the if statements, checking that the files are really those that I want renamed, are actually executed.
• The result is light, and easily and precisely changed.

Oct 2, 2010, 03:34 PM
As a precaution against carelessness, I do want the date check, which should be trivial in AppleScript, but doesn’t seem to be in A Better Finder Rename 8 (http://www.publicspace.net/ABetterFinderRename/).

Oct 3, 2010, 01:41 AM
Have you written much Applescript?

You can look at Macscripter.net for code examples.

There is a shareware script that does a lot of renaming:

Oct 3, 2010, 07:58 PM
How many pictures are you talking about?

You want the new filename to be 5 digits and have the pics renamed based on their creation date?

Oct 4, 2010, 01:35 AM
Files, over multiple folders, run:

DSC09999.JPG (file created 21:58 on Sun 26 Sept 2010)
DSC00001.JPG (file created 21:59 on Sun 26 Sept 2010)

So there are two ‘DSC00001.JPG’s. It seems natural to remove the duplication by changing the lead zero to a one. But, to prevent careless error, this should happen only to files with names in this format, and only to those of the second lap (i.e., after the lapping date).

How many? A one-day visit to an archive can produce 500 pictures. The plan is that they are dragged to a folder, command-A, button/keypress/menu, and it’s done. The checks are in case a careless error has selected files in the wrong folder.

Oct 4, 2010, 09:04 AM
You really want A better finder rename. I've found it essential for organizing my photo files. There are many ways to organize the files, using data from the files themselves. For instance, you can name the files based on the date they were taken and then numerically ordered - 10-4-10-0001, 10-4-10-0002, etc. That sounds like what you'd need for your purposes, and you wouldn't run the risk of accidentally overwriting files like you might with writing your own code.

Oct 4, 2010, 02:36 PM
What about some sort of Terminal command -

for i in $( find . -name 'DSC0*.JPG' -newer images/2010.08.06/DSC09999.JPG -type f -print ); do mv $i $( echo $i | sed 's/DSC_0/DSC_1/' ); done

Of course, you'd have to fill in the pathname to the DSC09999.JPG file. And the "-type f" and "-print" aren't really necessary, but it plays safe, in case you hit a directory called 'DSC0xxxx.JPG'.

Practice in a sample directory to get confident with it before using for real. Then keep it for when you need to prefix the files with a '2' or '3', etc.

Oct 4, 2010, 05:51 PM
Not what I had envisaged, but cool nonetheless. Thank you.

Just to be safe, can “DSC0*.JPG” be replaced with the more precise DSC0nnnn.JPG form?

And can there be an explicit constant date for the ‘newer’ thing?

Oct 5, 2010, 01:49 AM
Just to be safe, yes. Replace the asterisk with [0-9][0-9][0-9][0-9]

For the -newer option, you're safer using the existing format, and no, I don't know of a way to substitute a specific time.

I have had similar issues in the past when working with multiple cameras. so I had maybe 2,500 images using DSC_00001.JPG through to DSC_00400.JPG. A nightmare in the past also when I changed CF cards and battery, the counter would reset to DSC_00001.JPG. At -30C I'd get about 6 shots off before the battery would get too cold to operate, so into my pocket, warm battery out and in, and another 6 shots if I was quick. 16GB CF cards are the best invention ever.

Oct 5, 2010, 02:25 AM
Fabulous. Thank you again.

I have long complicated pathnames, and don't want to guarantee that picture 9999 never moves. Can it be replaced with a UNIX time of 1285538340 (http://www.wolframalpha.com/input/?i=unix+1285538340+gregorian)?

Edit: this (http://stackoverflow.com/questions/1275701/korn-shell-script-to-get-files-between-two-dates) suggests something like “-newer -t 201009262159”. I’m regretting never having mastered UNIX.

Oct 5, 2010, 07:26 AM
Well you could always replace the absolute path of DSC09999.JPG with another subshell which does a find looking for DSC09999.JPG, ie.
$(find . -name 'DSC09999.JPG' -type f -print)
and I though of that first, but what if there are 2 files with that name ? And that will happen when you get to the 19,998th picture, ie. DSC09999.JPG before you rename it with this script.

Did also consider your idea about "touch"ing an empty file with the desired modification date, put somewhere in the higher levels of your photograph directory hierarchy. But then I thought that you have the first DSC09999.JPG anyway. If you'd feel happier, touch -t 201009262159 photo.marker1 and use that instead of the reference to DSC09999.JPG.

Remember to update it or create another, ie. photo.marker2, when you get to 19,998+ photos.

There is a danger in the script in that once you get above 19,998 photos, you may have some you wish to rename to DSC1nnnn.JPG and some DSC2nnnn.JPG.
If you create the 2nd marker with the appropriate date and time and run that command first it won't be an issue, all the DSC0nnnn.JPGs that need to be renamed to DSC2nnnn.JPG will have been done, and the run of the original command (to rename to DSC1nnnn.JPG) will only pick up those still named as DSC0nnnn.JPG.
I'm also worried about the time accuracy you use for creating the marker files. Could be that the marker file ends up with a modification time just fractions of a second before the DSC09999.JPG it is copied from and you end up renaming DSC09999.JPG to DSC19999.JPG when you didn't intend to.

Oct 5, 2010, 07:29 AM
Why not use automater?

Oct 14, 2010, 06:18 AM
My preference is to use a process that I understand. That allows me to vary it, if needed, and to recognise potential and possible failure modes. Applescript is easy to read—hence my originally suggesting that.

Terminal commands, though powerful, are cryptic. I don’t know what is happening.

So Automator: Filter by Date Created → Find “DSC0” Replace “DSC1”. It’s not quite perfect, but my having to drag files onto it acts as a pre-selection. Doing that: thank you for the suggestion.