newbie stills needs renaming help

Hello there,

I am afraid I still need help on this renaming, I think my problem is that I get to the part where I want to set the newname for theitem in the last repeat and I get a message like cannot replace text, when I ask to return the class of theitem I get string, but it seems that the finder doesn’t know where it is. When ask for properties of theitem I get none. Any help would be appreciated.

thanks again

property type_list : {"TIFF", "JPEG", "PNGf", "PICT"}
property extension_list : {"tif", "tiff", "jpg", "jpeg", "png", "pict", "pct"}

set sourcefolder to choose folder
set sourcefolder to sourcefolder as text

set infolder_list to list folder sourcefolder without invisibles

set folder_list to {}
set file_list to {}
set maxlen to 9
set moved_images to {}

--separate folders from files
repeat with a_item in infolder_list
	set theitem to (sourcefolder & a_item)
	tell application "Finder"
		try
			list folder theitem
			set folder_list to folder_list & (a_item)
			
		on error
			set file_list to file_list & (a_item)
			
		end try
	end tell
end repeat

--list each folder and get name of folder
tell application "Finder"
	
	repeat with i from 1 to number of items in folder_list
		set afolder to item i of folder_list as text
		set listsub to list folder of (sourcefolder & afolder) without invisibles
		set foldername to text of afolder
		
		--rename each image in folder		
		repeat with i from 1 to number of items in listsub
			set theitem to item i of listsub
			set name_increment to 1
			set newname to (the foldername & name_increment & ".jpg" as text)
			set the name_increment to the name_increment + 1
			set name of theitem to newname
			set moved_images to moved_images & theitem
			return moved_images
		end repeat
	end repeat
end tell

picturedude,

I’m sure Nigel, Kai, or a number of the other better versed veterans can explain this better, but I’ll give it a shot.

I believe your thinking is correct in what you want to accomplish, however the problem starts with setting your source folder to text. In your first repeat, in the try statement, nothing gets set to what you want because the class of “theitem” is a string and not a folder or a file. I used your code and inserted a couple of display dialogs with a get class of item to find out what was going on. Here is that code.

(A bit of an edit). It looks to me that using the “list folder” returns a list of the contents of a folder as a string list as opposed to a reference list. So any objects in these lists need to be referenced by adding “folder” of “file” in front and probably with a location of these items. Since you don’t really know for sure which items are files or folders or where they are, for that matter, a different approach is needed.

property type_list : {"TIFF", "JPEG", "PNGf", "PICT"}
property extension_list : {"tif", "tiff", "jpg", "jpeg", "png", "pict", "pct"}

set sourcefolder to choose folder
set sourcefolder to sourcefolder as text

set infolder_list to list folder sourcefolder without invisibles

set folder_list to {}
set file_list to {}
set maxlen to 9
set moved_images to {}

--separate folders from files
repeat with a_item in infolder_list
	set theitem to (sourcefolder & a_item)
	tell application "Finder"
		--try
		set t to class of theitem
		display dialog t as string
		if class of theitem is folder then
			list folder theitem
			set folder_list to (folder_list & (a_item)) as alias list
		else
			--on error
			set file_list to (file_list & (a_item))
		end if
		--end try
	end tell
end repeat
display dialog folder_list as string
--list each folder and get name of folder
tell application "Finder"
	
	repeat with i from 1 to number of items in folder_list
		set afolder to item i of folder_list as text
		set listsub to (list folder of (sourcefolder & afolder) without invisibles)
		--display dialog listsub as string
		set foldername to text of afolder
		
		--rename each image in folder		
		repeat with i from 1 to number of items in listsub
			set theitem to item i of listsub as alias
			set name_increment to 1
			set newname to (the foldername & name_increment & ".jpg" as text)
			set the name_increment to the name_increment + 1
			get name of theitem
			set name of file theitem to newname
			set moved_images to moved_images & theitem
			return moved_images
		end repeat
	end repeat
end tell

I rewrote your script in the following way. This is the way I thought of. There are a couple elements missing from your original (namely the moved_images) that you can add. I changed the placement of your nameIncrement variable because it would always be 1 where you currently have it.
(another edit) You may want to put the nameIncrement after the first repeat in your renaming code. I just looked at my test results and the increment would be say “fold11”,“fold12”,“fold13” for the first folder and “fold24”,“fold25” and so on. I believe you probably want the names to be from 1 to x in each folder.

set sourcefolder to choose folder
set folderList to {}
set fileList to {}

tell application "Finder"
	repeat with aItem in folder sourcefolder
		if class of aItem is folder then
			set folderList's end to aItem
		else
			set fileList's end to aItem
		end if
	end repeat
	
	set nameIncrement to 1
	repeat with i from 1 to (count folderList)
		set afolder to item i of folderList
		set foldername to name of afolder
		set theItems to every item of afolder
		repeat with j from 1 to (count afolder)
			set theitem to item j of theItems
			set newname to (the foldername & nameIncrement & ".jpg") as text
			set nameIncrement to nameIncrement + 1
			set name of theitem to newname
		end repeat
	end repeat
end tell

Not a great explanation, but a start.

Hope this gives some insight. Note that in my script where you choose the folder and then reference it in the “Finder” tell block that you need to specify that it is indeed a folder. This goes the same if you say choose file and then reference it in a “Finder” tell block.

PreTech

Hi there,

thanks so much for the quick reply, it works great. I’ve got another question for you though, do you happen to know if there is a way to rename the files, but not in their actual location, that is keep them in kind of stasis so that I can pass them on to handler to resize. My problem is that many of the images are coming off of cd’s.
Again thanks for you help

picturedude,

How about this.

set sourcefolder to (choose folder with prompt "Select the source folder.")
set destinationFolder to (choose folder with prompt "Choose where files are to be saved.")
set folderList to {}
set fileList to {}

tell application "Finder"
	repeat with aItem in folder sourcefolder
		if class of aItem is folder then
			set folderList's end to aItem
		else
			set fileList's end to aItem
		end if
	end repeat
	
	
	repeat with i from 1 to (count folderList)
		set nameIncrement to 1
		set afolder to item i of folderList
		set foldername to name of afolder
		set theItems to every item of afolder
		repeat with j from 1 to (count afolder)
			
			set theitem to item j of theItems
			set thisName to name of theitem
			set newname to (the foldername & nameIncrement & ".jpg") as text
			set nameIncrement to nameIncrement + 1
			duplicate theitem to folder destinationFolder
			set name of file thisName of folder destinationFolder to newname
		end repeat
	end repeat

PreTech

Thanks so much for your quick and accurate responses, it all works great!

You’re welcome. Glad it helped.:slight_smile:

Very nice work, PreTech - in terms of both your explanation and the scripting. :slight_smile:

I realise it’s all too easy to come in with suggestions when the donkey work’s done. But just in case it’s of interest, this should also do something similar:

set sourcefolder to choose folder with prompt "Select the source folder:"
set destinationFolder to choose folder with prompt "Choose where files are to be saved:"
tell application "Finder" to repeat with afolder in (get sourcefolder's folders)
	set foldername to afolder's name
	set fileList to afolder's files
	repeat with i from 1 to (count fileList)
		set name of (duplicate fileList's item i to destinationFolder) to foldername & i & ".jpg"
	end repeat
end repeat

Ooneee Weenee Wanna! Oonee Weene Wanna!

I prostrate myself in deference to you for your (once again) incredibly concise scripting ability Kai.:wink: I’d have never thought to "tell application ‘Finder’ to repeat with afolder in (get sourcefolder’s folders). I think I can feel the synapses sputtering.:smiley: And thank you for the commendation.:slight_smile:

PreTech

Kai’s script is pretty darn cool, and super quick too.
thanks for the input