Rename x number of files based on x insertions

Hi

I am trying to find a way to rename certain files within a folder, to make room for incoming files. The reason is this:

I have a personal photo website that displays 9 images per gallery, and I intend to have quite a few galleries. The images and their thumbnails are stored inside a folder - 9 of each, so 18 images per gallery. The trouble is, I don’t always create new galleries for new images - I often need to push old images out to allow new images in. As I want to push the oldest images out, I am forced to rename 16 files per gallery everytime I want to add new images!

Here’s how a gallery’s images are named: (the Fgallery are the fullsize images, the gallery ones are thumbnails)
Fgallery1-1.jpg
Fgallery1-2.jpg
Fgallery1-3.jpg
etc etc
Fgallery1-8.jpg
Fgallery1-9.jpg

gallery1-1.jpg
gallery1-2.jpg
gallery1-3.jpg
etc etc
gallery1-8.jpg
gallery1-9.jpg

So if I want to insert 2 new pics, the following files get pushed out:

Fgallery1-8.jpg
Fgallery1-9.jpg
gallery1-8.jpg
gallery1-9.jpg

then I have to rename Fgallery1-1.jpg to Fgallery1-3.jpg, Fgallery1-2.jpg to Fgallery1-4.jpg…, aswell as gallery1-1.jpg to gallery1-3.jpg, gallery1-2.jpg to gallery1-4.jpg…quite laborious as you can see!!! Finally I’m left with 2 free spots in each size (thumb and full) (Fgallery1-1.jpg and Fgallery1-2.jpg, gallery1-1.jpg and gallery1-2.jpg) where I can put two new images.

Isn’t there an easier way to do this? What would be cool is if I was prompted for how many photos I wanted to insert, and then the script pushed this many out and renamed the rest, freeing up x number of spots at the start. The reason I need to add photos at the start of the sequence is it’s these pics that get displayed first in the gallery, so it makes sense to have the newest pics here.

If this can work, how can it also be made to work with other galleries…? Other galleries take this form : Fgallery2-1.jpg and its thumbnail image, gallery2-1.jpg.

Any help would be greatly appreciated!

Thanks,

Matthew

PS - I have searched the forums for similar threads but haven’t turned anything up that helps me. :frowning:

Model: iMac G5 2Ghz
Browser: Safari 312.6
Operating System: Mac OS X (10.4)

Hi

Sounds like you want to arrange the gallery by modification date. In that way the most recent images would be displayed first. Applescript could then list by mod date, delete the number of files you request and add new ones in. You could get image events to re format the large image to the thumbnail size. There would be no need to rename.

But I don’t know how your gallery organises your images…

Firstly, thanks to both Jacques and Weedinner for your responses.

Jacques - you’ve helped me out before and I know your scripts work, but I haven’t yet had time to test your suggestion for this problem. I will do soon though, and will let you know how it works out.

Weedinner - do you mean how the actual gallery pics are laid out? If so, there are 9 pics per gallery, laid out like this:

1 2 3
4 5 6
7 8 9

1 is called Fgallery1-1.jpg, 2 is called Fgallery1-2 etc etc. The thumbnails are also arranged in the same way. So I always want to have the latest images going in at the start of the gallery - ie if I add 2 new images, I want them to sit in positions 1 & 2…meaning that 8 & 9 get pushed out.

Is this what you mean? If not, let me know. You can also take a look at the site : www.miamatcha.com

Thanks!

Matthew

Hi Jacques

I tried your script, and it seems very close, however there were a few problems.

Here’s what I did:

  1. I dropped three files on the script (saved as an aplication bundle) and it told me that it’s an odd number, and didn’t seem to do anything after that. There will be occasions when I need to move an odd number of files.

  2. I dropped two files (Fgallery1-8.jpg and Fgallery1-9.jpg) - and it did something. It seemed to remove the last one (Fgallery1-9.jpg) but not Fgallery1-8.jpg. It then renamed as follows:

Fgallery1-1.jpg → Fgallery1-2.jpg
Fgallery1-2.jpg → Fgallery1-3.jpg
Fgallery1-3.jpg → Fgallery1-4.jpg
Fgallery1-4.jpg → Fgallery1-5.jpg
Fgallery1-5.jpg → Fgallery1-6.jpg
Fgallery1-6.jpg → Fgallery1-7.jpg
Fgallery1-7.jpg → Fgallery1-8.jpg
Fgallery1-8.jpg → Fgallery1-9.jpg

This is perfect, however why didn’t it make two file number ‘jumps’ (ie Fgallery1-1.jpg → Fgallery1-3.jpg) seeing as I dropped 2 files? The desired result would have been:

Fgallery1-1.jpg → Fgallery1-3.jpg
Fgallery1-2.jpg → Fgallery1-4.jpg
Fgallery1-3.jpg → Fgallery1-5.jpg
Fgallery1-4.jpg → Fgallery1-6.jpg
Fgallery1-5.jpg → Fgallery1-7.jpg
Fgallery1-6.jpg → Fgallery1-8.jpg
Fgallery1-7.jpg → Fgallery1-9.jpg

…leaving Fgallery1-1.jpg and Fgallery1-2.jpg vacant, ready to insert new images.

Also, it would be cool if it could create blank files with appropriate titles to let me know where images need to be inserted - ie if 2 files are dropped, images need to be inserted into the spaces occupied by Fgallery1-1.jpg and Fgallery1-2.jpg. So perhaps two files could be created with names like this : Fnewimage1-1.jpg and Fnewimage1-2.jpg…?

I haven’t paid any attention to the thumbnail versions of these files - I assume once the fullsize image renaming procedure is working, it can be duplicated over to the thumbnail versions.

Thanks for working this out for me!

Matthew

PS - here’s the script as I have it:



on open these_items
	set gallery_folder to "SystemX:Users:Matt:Desktop:Gallery Folder:" as alias -- replace the string by the path of your folder
	set nbr_of_Newitems to (count these_items)
	if nbr_of_Newitems mod 2 = 0 then -- 2,4,6,8 , even number
		set nbr_of_Newitems to nbr_of_Newitems div 2
		
		tell application "Finder"
			tell (get name of item 1 of these_items)
				if it starts with "Fgallery" then
					set gallery_number to text 9 thru -6 of it -- the number after "Fgallery"  with the "-" ,  ex : "2-"
				else if it starts with "gallery" then
					set gallery_number to text 8 thru -6 of it
				else
					tell me to display dialog "Bad file name"
					return
				end if
			end tell
			
			--search files whose name start with "Fgallery"  coerced with the  variable gallery_number
			set These_Fgallery_files to files of gallery_folder whose name starts with "Fgallery" & gallery_number
			set These_gallery_files to files of gallery_folder whose name starts with "gallery" & gallery_number
			--the files is sorted by name in Tiger
			
			repeat with i from 9 to 1 by -1
				try
					if i ≤ (9 - nbr_of_Newitems) then --rename file
						set name of item i of These_Fgallery_files to "Fgallery" & gallery_number & (i + nbr_of_Newitems) & ".jpg"
						set name of item i of These_gallery_files to "gallery" & gallery_number & (i + nbr_of_Newitems) & ".jpg"
					else
						delete {item i of These_Fgallery_files, item i of These_gallery_files} --delete old image  files
					end if
				end try
			end repeat
			
			move these_items to gallery_folder -- add new image files in the folder
		end tell
	else
		display dialog "it's an odd number of files." & return & "You are dropped " & nbr_of_Newitems & " files."
	end if
end open



Hi Jacques

I haven’t tested it yet, but it looks like it will do the trick. I think the problem I encountered on my previous attempt was caused by me dropping only the ‘Fgallery’ files - 3 of them… If I had dropped the Fgallery files and their thumbnails (gallery), I would have had an even number.

I’ll try it as soon as I get the chance, and will let you know how it goes.

Matthew

PS - I’m not sure what you mean by this : "If you create the thumbnails by script , you can insert it in this script "

Hi Jacques

I finally found time to test the script. Sorry about the delay.

I started by dropping a file called Fgallery1-9.jpg. It disappeared, and then reappeared named Fgallery1-9 20-58-13.jpg. It seems to be the same file (it’s an image, and the image is the same after renaming) as the original Fgallery1-9.jpg, just with a different name. There was also a file created called gallery1-9 20-58-13.jpg. This appears to be a new file as it is not an image file like the original gallery1-9.jpg.

The problem as I see it with the above is that, by removing one file set (Fgallery1-9.jpg and gallery1-9.jpg), the other files in that set should have been moved down one step - ie Fgallery1-1 → Fgallery1-2, Fgallery1-2 → Fgallery1-3 etc and the same with the gallery files. This didn’t happen.

When I dropped two files (Fgallery1-8.jpg and Fgallery1-9.jpg) this happened : Fgallery1-8 didn’t get its name changed and the image stayed the same. Fgallery1-9’s name changed to Fgallery1-9 21-05-36.jpg and the image stayed the same. No files from the start of the series (ie Fgallery1-1 and 1-2) got moved as expected. gallery1-8.jpg was created (a new blank jpg file) and gallery1-9 21-05-36.jpg was created - also a new blank file. However the files at the start of this series didn’t get moved either.

Would it make it much more simple if I said that I will only ever remove 1 file at a time, and always from the end of the series? So if I was to remove one file, it would always be Fgalleryx-9.jpg…and if I wanted to remove two, I would first remove Fgalleryx-9.jpg, wait for Fgalleryx-8 to get renamed Fgalleryx-9, and then remove the ‘new’ Fgalleryx-9.jpg?

It would of course be nice to be able to remove more than one at a time, and discontinous files (ie Fgalleryx-3 and Fgalleryx-8) but if it makes it too tricky to script, I’ll deal with the other method!

Thanks for your patience!

Matthew

Here’s the script as I have it:




on open these_items
	set gallery_folder to "SystemX:Users:Matt:Desktop:gallery:" as alias -- replace the string by the path of your folder
	set {gallery_number, theStart, images_num, the_countdown, nbr_of_Newitems} to {"", 10, {}, 9, (count these_items)}
	
	tell application "Finder"
		repeat with i in these_items
			tell (get name of i) to if it starts with "Fgallery" then
				set num_x to text 9 thru -6 of it -- the number after "Fgallery"
				if gallery_number is "" then
					set gallery_number to num_x -- the number  with the "-" , ex : "2-"
				else if num_x ≠ gallery_number then
					tell me to display dialog "You must drop images with the same  number after 'Fgallery'" buttons "Cancel"
				end if
				set z to (text -5 of it) as number -- the number after "Fgallery2-"
				if z < theStart then set theStart to z
				set end of images_num to z
			else
				tell me to display dialog "Bad file name" buttons "Cancel"
			end if
		end repeat
		
		--search files whose name start with "Fgallery" coerced with the variable gallery_number
		set These_Fgallery_files to (files of gallery_folder whose name starts with "Fgallery" & gallery_number)
		set These_gallery_files to (files of gallery_folder whose name starts with "gallery" & gallery_number)
		--the files is sorted by name in Tiger
		
		set the_count to (count These_Fgallery_files)
		repeat with i from the_count to 1 by -1
			set file_num to (text -5 of (get name of item i of These_Fgallery_files)) as number -- the number after "Fgallery2-"
			if file_num > (9 - nbr_of_Newitems) then
				delete {item i of These_Fgallery_files, item i of These_gallery_files} -- delete old files
			else if file_num ≥ theStart then
				set name of item i of These_Fgallery_files to "Fgallery" & gallery_number & the_countdown & ".jpg"
				set name of item i of These_gallery_files to "gallery" & gallery_number & the_countdown & ".jpg"
				set the_countdown to the_countdown - 1
				--this check the number for not renaming file with the same number that new files
				repeat until the_countdown is not in images_num
					set the_countdown to the_countdown - 1
				end repeat
			else if file_num < theStart then
				exit repeat
			end if
		end repeat
		
		repeat with i in these_items -- add empty thumbnail  in gallery_folder, 4 Ko
			make new file at gallery_folder with properties {name:text 2 thru -1 of (get name of i)}
		end repeat
		move these_items to gallery_folder -- add new image files in gallery_folder
	end tell
end open



Hi Jacques

I fear this project has become way more complicated than I had envisaged! Although I was under no illusions that I could write an AppleScript to do the job myself, I figured that one of the experts such as yourself would be able to crack it without too much bother. It certainly seems that you have complete control of what the script is doing, however I’m kind of lost… :-((

Is there any way we can simplify the script so that it, when one file (always Fgalleryx-9.jpg is dropped, it does this:

  1. Removes Fgalleryx-9.jpg & galleryx-9.jpg

  2. Renames the Fgallery files as follows:

Fgalleryx-1.jpg → Fgalleryx-2.jpg
Fgalleryx-2.jpg → Fgalleryx-3.jpg
Fgalleryx-3.jpg → Fgalleryx-4.jpg
Fgalleryx-4.jpg → Fgalleryx-5.jpg
Fgalleryx-5.jpg → Fgalleryx-6.jpg
Fgalleryx-6.jpg → Fgalleryx-7.jpg
Fgalleryx-7.jpg → Fgalleryx-8.jpg
Fgalleryx-8.jpg → Fgalleryx-9.jpg

  1. Renames the gallery files as follows:

galleryx-1.jpg → galleryx-2.jpg
galleryx-2.jpg → galleryx-3.jpg
galleryx-3.jpg → galleryx-4.jpg
galleryx-4.jpg → galleryx-5.jpg
galleryx-5.jpg → galleryx-6.jpg
galleryx-6.jpg → galleryx-7.jpg
galleryx-7.jpg → galleryx-8.jpg
galleryx-8.jpg → galleryx-9.jpg

  1. Creates a new blank Fgallery file : perhaps " NEWFgalleryx-1.jpg" ?

  2. Creates a new blank gallery file : perhaps " NEWgalleryx-1.jpg" ?

If this is possible, it will work perfectly for my needs. There is no need to be able to remove discontinous files. If I want to remove more than one file, I can always run the script several times - ie if I need to remove 3 Fgallery files (ie Fgalleryx-7.jpg, Fgalleryx-8.jpg, Fgalleryx-9.jpg) I can run it once to remove Fgalleryx-9.jpg and then once Fgalleryx-8.jpg is renamed to Fgalleryx-9.jpg, run it again and remove the ‘new’ Fgalleryx-9.jpg.

I hope this makes things more simple, for you and me!

Regards

Matthew

Hi Jacques

Ok, I think we’re getting close.

Step 1 - ok
Step 2 - yes - when you say delete, you mean moving Fgalleryx-9.jpg to the trash right? So at this stage, there is no file called Fgalleryx-9.jpg in the folder “gallery”. Right?
Step 3 - yes - Fgalleryx-8.jpg gets renamed Fgalleryx-9.jpg.
Step 4 - a little bit yes, a little bit no. :slight_smile: The folder “gallery” contains Fgalleryx-9.jpg (actually the old Fgalleryx-8.jpg). However, if Step 2 above is correct (ie that the dropped file (Fgalleryx-9.jpg is in the trash), we don’t need to concern ourselves with Fgalleryx-9.jpg anymore. As long as the following happens, all will be ok for the renaming part:

Fgalleryx-1.jpg → Fgalleryx-2.jpg
Fgalleryx-2.jpg → Fgalleryx-3.jpg
Fgalleryx-3.jpg → Fgalleryx-4.jpg
Fgalleryx-4.jpg → Fgalleryx-5.jpg
Fgalleryx-5.jpg → Fgalleryx-6.jpg
Fgalleryx-6.jpg → Fgalleryx-7.jpg
Fgalleryx-7.jpg → Fgalleryx-8.jpg

So now we are left without a file called Fgalleryx-1.jpg. This is where I would like a blank file (NEWFgalleryx-1.jpg) to be inserted.

I can see a problem if I want to run the script more than once, as because there is already a file called Fgalleryx-9.jpg (the result of the first script run) in the trash, I can’t put another one in there with the same name. To solve this, is there a way to change the name of Fgalleryx-9.jpg before it is moved to the trash - by adding a random number string for example?

An alternative solution to this would be to not use a droplet, but merely a dialog box that opens at the start of the script - something like this :

Enter the number of the gallery you would like to modify? (this is asking for the x in Fgalleryx-1.jpg, let’s say for this example the gallery I want to modify is 1.
How many files would you like to remove? (so if i want to remove 3 files, I’d enter 3)

Using the sample numbers entered as above (ie gallery 1 and 3 files), the script will remove 3 files from gallery1 ie remove files Fgallery1-9.jpg, Fgallery1-8.jpg, Fgallery1-7.jpg. After that, it would rename as follows:

Fgalleryx-1.jpg → Fgalleryx-4.jpg
Fgalleryx-2.jpg → Fgalleryx-5.jpg
Fgalleryx-3.jpg → Fgalleryx-6.jpg
Fgalleryx-4.jpg → Fgalleryx-7.jpg
Fgalleryx-5.jpg → Fgalleryx-8.jpg
Fgalleryx-6.jpg → Fgalleryx-9.jpg

Now there are 3 ‘missing’ files : Fgallery1-1.jpg, Fgallery1-2.jpg, Fgallery1-3.jpg

It would be great if three blank files could be inserted : NEWFgallery1-1.jpg, NEWFgallery1-2.jpg, NEWFgallery1-3.jpg

Once the Fgallery files are dealt with, the exact same process came happen to the thumbnail gallery files.

What about that as an idea?

Matthew

Hi Jacques

You’re right, we were thinking of two different things! I was, all along, thinking of dropping existing files (ie files in “gallery”) that I wanted to get rid of -where as you were thinking of dropping new files to be added to “gallery”

I will test the new script and will let you know how it goes.

Your patience and tenacity to see this project through is awesome!

Regards

Matthew

Hi Jacques

I couldn’t wait to see if it worked so tested it immediately - it’s almost perfect!

There are only two small improvement as I see it :

I think it will be clearer if the new files called “galleryx-1.jpg” etc, could also have the “NEW” prefix. I modified the script to do this, as well as adding a space in front of the NEW to make them appear at the top of the Finder list when sorted by name.

What would be even cooler though, would be change the new files’ colour labels…maybe they could be made to appear RED (with no “NEW” prefix) and once I have replaced them, all I would have to do is to remove the RED label and not fiddle around with removing the “NEW” prefix… If you can show me how to do this it’d be brilliant.

A million thanks…again!

Matthew

PS - if there any way to get AppleSCript to resize and adjust the resolution of jpg images…?

Here’s what I have:



on open these_items
	set gallery_folder to "SystemX:Users:Matt:Desktop:gallery:" as alias -- replace the string by the path of your folder
	set {gallery_number, these_names, nbr_of_Newitems, the_countdown} to {"", {}, (count these_items), 10}
	
	tell application "Finder"
		repeat with i in these_items
			tell (get name of i) to if it starts with "Fgallery" then
				set end of these_names to text 2 thru -1 of it --remove character "F" 
				set num_x to text 9 thru -6 of it -- the number after "Fgallery"
				if gallery_number is "" then
					set gallery_number to num_x -- the number with the "-" , ex : "2-"
				else if num_x ≠ gallery_number then
					tell me to display dialog "You must drop images with the same number after 'Fgallery'" buttons "Cancel"
				end if
			else
				tell me to display dialog "Bad file name" buttons "Cancel"
			end if
		end repeat
		
		delete these_items --delete dropped files -->>"FgalleryX-"
		delete (files of gallery_folder whose name is in these_names) --delete files -->>"galleryX-"
		
		-- search files whose name start with "Fgallery" coerced with the variable gallery_number
		-- the files is sorted by name in Tiger
		set These_Fgallery_files to (files of gallery_folder whose name starts with "Fgallery" & gallery_number)
		set These_gallery_files to (files of gallery_folder whose name starts with "gallery" & gallery_number)
		set These_NewFgallery_files to (files of gallery_folder whose name starts with "NEWFgallery" & gallery_number)
		
		
		-- two repeat for renaming : the number of files it's not the same because if exists file with this name "NEWFgallery"
		repeat with i from (count These_Fgallery_files) to 1 by -1 --rename "Fgallery" file
			set the_countdown to the_countdown - 1
			set name of item i of These_Fgallery_files to ("Fgallery" & gallery_number & the_countdown & ".jpg")
		end repeat
		
		repeat with i from (count These_NewFgallery_files) to 1 by -1 -- if exists , it rename the "NEWFgallery" file
			set the_countdown to the_countdown - 1
			try
				set name of item i of These_NewFgallery_files to ("NEWFgallery" & gallery_number & the_countdown & ".jpg")
			end try
		end repeat
		
		set the_countdown to 10
		repeat with i from (count These_gallery_files) to 1 by -1 ----rename "gallery" file
			set the_countdown to the_countdown - 1
			set name of item i of These_gallery_files to ("gallery" & gallery_number & the_countdown & ".jpg")
		end repeat
		
		repeat with i from 1 to nbr_of_Newitems ---- add empty thumbnail and image in gallery_folder, 4 Ko
			make new file at gallery_folder with properties {name:" NEWgallery" & gallery_number & i & ".jpg"}
			try
				make new file at gallery_folder with properties {name:" NEWFgallery" & gallery_number & i & ".jpg"}
			end try
		end repeat
	end tell
end open



Hi Jacques

The new version of the delete/rename/colour script is perfect. Thanks a million!

I tested the file resizing script I found in the link you suggested - it’s great! It will make things so much faster for me - before I had to go into Photoshop, open each image, resize, save…and do the same for thumbnails. It took so long that I lost interest in updating the homepage.

This is the script I used, with a few small changes:


on open some_items
	repeat with this_item in some_items
		rescale_and_save(this_item)
	end repeat
end open

to rescale_and_save(this_item)
	choose from list {50, 100, 150, 200, 250, 300, 350, 400, 450, 500, 550, 600, 650, 700} with prompt "What size do you want to scale to?"
	if the result is not false then
		set The_choosen_size to the result as integer
		tell application "Finder" to set new_item to quoted form of POSIX path of ((container of this_item as string) & "scaled." & (name of this_item))
		set thisFile to quoted form of POSIX path of this_item
		-- scale to choosen size and compress at 80 % and save at the path of the variable new_item
		do shell script "/usr/bin/sips -Z " & The_choosen_size & " -s formatOptions 80 " & thisFile & " --out " & new_item
	end if
end rescale_and_save


I don’t need as many size options, so will remove some. It would be cool if this script could give you the option to select between a few different compression rates - eg maybe 75%, 85%, 95%, 100%. That way, if there was a really good photo, I could use a higher compression rate…giving a clearer pic. Is this possible?

What would also be great is if it could do two resize & compression procedures on the same image - ie so it creates the ‘full size’ image for viewing on the web, and at the same time, a very small (say 70 x 100 or 100 x 70 depending on the images orientation) thumbnail image. This would probably be compressed more than the ‘full size’ version, although maybe not as being so small, it might become unclear. Is this possible as well?

I guess the ultimate combination of these two scripts for me would be if I had a new image that I wanted to add, and all I had to do was to rename it (for the appropriate gallery and position) and then drop it on a droplet. The droplet would then run the resize script, and at the same time create the resized thumbnail version. The script would then place the two new files into the gallery folder, bumping out the old files (‘full size’ and thumbnail) that were being replaced. Do you think it’s possible…?

Matthew

Hi Jacques

I don’t know if you think that the final script is simple, but I think it’s mind boggling!!! It’s going to save me SO much time it’s amazing. Is this script writing easy for you? I have written some scripts, basic copying stuff, and I always ran into trouble. How did you ever manage to get that good at script writing?

All I hope is that you continue to hang around these forums because I don’t think I could find a task that you couldn’t script. Thanks so much - I’m indebted to you!

Matthew

PS - if you want a challenge (as if I haven’t given you enough challenges already!!) - how about getting it to display the file size (after resize and resolution selections) and the option to go back to make alterations (to file size and resolution) if the modified file is too big…that’s got to be tricky!!

PPS - when I drop more than one file, is there a way to know which file will become Fgalleryx-1.jpg and which Fgalleryx-2.jpg?

Hi Jacques

Completely wicked!!

Thanks a million!!

Matthew

PS - is it impolite to ask for a contact email address so that should I ever be unable to find you in these forums in the future, I might be able to contact you to request help solving AppleScript problems?