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