Folder Action: Add sequential # and the date to filenames

Could someone help me make this work? It’s my first real attempt at AS. I’m going to be adding onto this but lets start with just renaming the files. I want to number and date pictures that I save to the folder. If I could make the number 4 digits with 0’s in front that would be great, and I want the date to be mm/dd/yy.


on adding folder items to this_folder after receiving saved_items
	repeat with i from 1 to (count files in saved_items)
		set the_saved_file_path to item i of saved_items
		tell application "Finder"
			set fileName of the_saved_file_path to fileName of the_saved_file_path & "-" & i & "-" & short date string of (current date) & ".jpg"
		end tell
	end repeat
end adding folder items to

Creating a filename unique to each run would be great too. For instance, picture-a-0001-03/13/06.jpg would be the result of one time. Then the next time I add files the name could be picture-b-0001-03/14/06.jpg. I suspect including a text prompt might be the best solution to unique naming, but I’d like this to be as autonomous as possible.

Thanks for any help!

Hi,

This line:

count files in saved_items

saved_items is a list of references. To count items in a list, just use the list:

count saved_items

This line:

set fileName of the_saved_file_path to fileName …

fileName should be ‘name’. There is no file property as fileName, but there is a ‘name’ property.

Lastly but not leastly, when you change the name of a file in a adding folder items to folder action, the script will think that you are adding new files just by changing the name. What I always say is to move the files to a different folder and then change the name.

Editted: and btw if you do this, you might just as well use a droplet and duplicate the items instead of moving.

gl,

Thanks for the tips! I’m working out of Rosenthal’s massive book, without reading enough of it first. As far as using droplets instead, I’m trying to set this up to happen anytime a picture hits my download file from firefox. Ultimately I want the files renamed and imported into iPhoto. This just seemed like the first step. Automator showed some slight potential to accomplish this, except that I can’t seem to make single automator workflow that works without getting an error. So, I figured I’d just suck it up and dive into the AS.

Again, thanks! I’ll toss those corrections in and see what I get. I’ll be back with more questions and receptive to any other suggestions. I may switch it to a droplet yet, as it shouldn’t be a difficult change.

Edit: Just as you predicted… The script worked and kept looping. I’ll try copying the file to another folder and deleting the original, then worry about importing to iphoto from there.

Hi,

You’re welcome.

One more thing. You probably want to get rid of the extension from the original name. Here’s an example of shifting the extension to the end of the new name

– just to get a reference to a file
set f to choose file – choose a jpeg
set the_name to name of (info for f)
if the_name ends with “.jpg” then
set the_name to (text 1 thru -5 of the_name)
end if
set new_name to (the_name & “whatever” & “.jpg”)
display dialog new_name

gl,

Ok, I’ve tried integrating your name idea, but first I got the script to work copying the file to another folder at the end to prevent the loop saving problem. Tell me what you think of this:

on adding folder items to this_folder after receiving saved_items
	repeat with i from 1 to (count saved_items)
		set the_saved_file_path to item i of saved_items
		if the_saved_file_path ends with ".jpg" then
			tell application "Finder"
				set name of the_saved_file_path to (text 1 through -5 of the_saved_file_path) & "-" & i & "-" & short date string of (current date) & ".jpg"
				try
					move the_saved_file_path to container "Macintosh HD:Users:my_name:Desktop:Downloads:Pictures:" replacing no
				end try
			end tell
		end if
	end repeat
end adding folder items to

edit: just read up on character referencing and understand the -5 now.

Hi iMactel,

It won’t work like that. The variable saved_items is a list of references to the added items, so the_saved_file_path is an alias reference. It’s not a string so you can’t check if it ends with “.jpg”. You either need to coerce the alias reference to a string (path) or get the name of the item. Here, you need the name later for creating the new name, so just get the name like in my example in the previous post. You can get the name with Finder or the ‘info for’ command.

What I like to do in naming conventions is to use the date and time where the time is seconds after midnight. This way, the file name should be unique and you won’t need to deal with a file with the same name.

I’ll show you an example.

gl,

The time convention sounds like a great way to deal with same names. I appreciate the help understanding the file referencing.

Ok, starting over and trying again:

on adding folder items to this_folder after receiving saved_items
	set DL_folder to "Macintosh HD:Users:myname:Desktop:Downloads:"
	set pic_folder to "Macintosh HD:Users:myname:Desktop:Downloads:Pictures:"
	repeat until (count files in DL_folder) is 0
		set f to file 1 of DL_folder
		set the_name to name of (info for f)
		if the_name ends with ".jpg" then
			set the_name to (text 1 thru -5 of the_name)
		end if
		set new_name to (the_name & "-" & time of (current date) & "-" & short date string of (current date) & ".jpg")
		set name of (info for f) to new_name
		tell application "Finder"
			move f to pic_folder
		end tell
	end repeat
end adding folder items to

How close am I? This is turning out to be a better learning project than I expected…

edit: As this is a folder action, I should be able to replace DL_folder with this_folder, shouldn’t I?

You’re doing pretty good!

You can’t use the info for command to change the name though. You need the Finder. I adjusted it a little and think this should work:

on adding folder items to this_folder after receiving saved_items
set pic_folder to “Macintosh HD:Users:myname:Desktop:Downloads:Pictures:” as alias
repeat with f in saved_items
set the_name to name of (info for f)
if the_name ends with “.jpg” then
set the_name to (text 1 thru -5 of the_name)
end if
set new_name to (the_name & “-” & time of (current date) & “-” & short date string of (current date) & “.jpg”)
tell application “Finder”
set name of f to new_name
move f to pic_folder
end tell
end repeat
end adding folder items to

One problem you might have is if you download big files.

gl,

I feel like Columbo here, but one more thing. It might be better to store the current date in a variable. This way you’re sure to get the exact same date for the time and date.

on adding folder items to this_folder after receiving saved_items
set pic_folder to “Macintosh HD:Users:myname:Desktop:Downloads:Pictures:” as alias
repeat with f in saved_items
set the_name to name of (info for f)
if the_name ends with “.jpg” then
set the_name to (text 1 thru -5 of the_name)
end if
set cur_date to (current date)
set t to time of cur_date
set d to date string of cur_date – change this to short date
set new_name to (the_name & “-” & t & “-” & d & “.jpg”)
tell application “Finder”
set name of f to new_name
move f to pic_folder
end tell
end repeat
end adding folder items to

I don’t have the ‘short date string’, so you need to change that in the above script.

gl,

GREAT!!! It seems to be doing well. A few files get double labeled… That would be the problem with larg files. Now I need to integrate the ability to handle other files that wont get named. I want it to ignore every file thats not a .jpg.

I think this is it:

on adding folder items to this_folder after receiving saved_items
	set pic_folder to "Macintosh HD:Users:myname:Desktop:Downloads:Pictures:" as alias
	set other_folder to "Macintosh HD:Users:myname:Desktop:Downloads:Other:" as alias
	repeat with f in saved_items
		set the_name to name of (info for f)
		if the_name ends with ".jpg" then
			set cur_date to (current date)
			set t to time of cur_date
			set d to short date string of cur_date
			set the_name to (text 1 thru -5 of the_name)
			set new_name to (the_name & "-" & t & "-" & d & ".jpg")
			set size_count to size of (info for f) as integer
			if size_count > 20000 then
				tell application "Finder"
					set name of f to new_name
					move f to pic_folder
				end tell
			else
				tell application "Finder"
					delete f
				end tell
			end if
		else
			tell application "Finder"
				move f to other_folder
			end tell
		end if
	end repeat
end adding folder items to

edit: I added your time and date variables in… works great! Ok, i updated this with a filter for size, too.

Now to figure out how to import files into iPhoto and make that a folder action for my pictures file. :lol: