Folder Count

Sorry to be so dense with applescripting. I wanted to create a small script to change all files in a given folder. I Wanted to append the file name to any comments already in the comment window. Mostly I never use the comment window and I could probably leave the appending part out but I added that, just in case.

The reason I am doing this is I am embarking on a mass reorganization of years of poorly backed up photo files. I will be renaming files but before I do I want to migrate the current file manes to the comment field, just in case.

I get it to work with a single file but I don’t know how to make it work with every file in a folder? I have been Googleing for a half hour with no strong clues other than what I show below. I don’t know if what I have is right as it wont error but it also wont give good results. I know enough programming to know I need to get a count of items in a folder and set an increment loop to walk through each item in the folder. Assuming I have the list set correctly (and I may not) it should go each time for the next value in the list count. - Thanks anyone.

PS _ I know I could do this as a watched folder or a folder action but I am wanting this to be something that I can point at each folder I am working on before I actually go in and change the file names. OR if it works I can just let it go on everything as it shouldn’t hurt anything as far as I can tell(?)

tell application “Finder”
set n to (choose folder)
set lvCount to count of folder n
set lvRepeatCount to 0
set lvItemNumber to 1

repeat until lvRepeatCount = lvCount
	set y to name of item lvItemNumber of n
	set c to comment of item lvItemNumber of n
	set comment of item lvItemNumber of n to c & " " & y
	lvRepeatCount = lvRepeatCount + 1
	lvItemNumber = lvItemNumber + 1
end repeat

end tell

Hi,

try this


set theFolder to (choose folder)
tell application "Finder"
	repeat with oneFile in (get document files of theFolder)
		set {comment:Co, name:Nm} to oneFile
		if Co is "" then
			set comment of contents of oneFile to Nm
		else
			set comment of contents of oneFile to Co & " " & Nm
		end if
	end repeat
end tell

Thanks very much, works perfectly. I never would have guessed your solution though. Can you please tell me where I should look the next time to find your use of: {comment:Co, name:Nm}, I cant see them as documented in any place I was looking?

I plan on not reinventing the wheel as there are several file renamers out there, but just to conclude this thought. What would the added vars be to add the files creation date and how about a sequential number appended to the file? Just curious - mostly.

Much thanks

OOPS - ADDED: It works exactly as I had asked for one folder at a time. But, It cant drill down if I Select a folder with levels of directories within it. How can I alter thins to include all files with a folder? I would thing this would even apply to an entire drive, all files within get this treatment.

Thanks again

oneFile is a record, I assign certain properties of the record to variables
These 4 forms look different but do exactly the same (creation date is included)

tell application "Finder"
	set Co to comment of onFile
	set Nm to name of oneFile
	setMd to creation date of oneFile
end tell

--

tell application "Finder"
	tell oneFile
		set Co to comment
		set Nm to its name
		set Md to creation date
	end tell
end tell

--

tell application "Finder"
	set {Co, Nm, Cd} to {comment, name, creation date} of oneFile
end tell

--

tell application "Finder"
	set {comment:Co, name:Nm, creation date:Cd} to oneFile
end tell

Renaming file names with sequential numbers is indeed almost reinventing the wheel :wink:
There are some good freeware solutions

it takes a while to gather the files, but you can do it this way


.
 repeat with oneFile in (get document files of entire contents of theFolder)
.

Note: if it takes more than two minutes to gather the files, you will get a timeout error

Thanks, I just took a similar example from this site from a post by trashman and I hobbled your kind example and his idea together. I have just one last little problem if you can help? I am using set search_file_type to “JPEG” to search only JPEG files. But is there a way to use a wildcard or a grep item in an Applescript? I tried putting a single asterix in "JP*G but that got no results. Some file endings are JPG, some are JPEG, some TIFF. I also tried searching using image but it didn’t recognize that either. I also tried using “OR” as in; set search_file_type to “JPEG” or “JPG” oT “TIFF” but that also did not work.

I Tested the following with three levels of test images and it worked very well. Here is what I have now:

set theFolder to (choose folder)
tell application “Finder”
set search_file_type to “JPEG”
repeat with oneFile in (get every file of the entire contents of theFolder whose file type is search_file_type)
set {comment:Co, name:Nm} to oneFile
if Co is “” then
set comment of contents of oneFile to Nm
else
set comment of contents of oneFile to Co & " " & Nm
end if
end repeat
end tell

try


set theFolder to (choose folder)
tell application "Finder"
	repeat with oneFile in (get every file of the entire contents of theFolder whose name extension is in {"JPG", "JPEG", "TIFF", "TIF"})
		set {comment:Co, name:Nm} to oneFile
		if Co is "" then
			set comment of contents of oneFile to Nm
		else
			set comment of contents of oneFile to Co & " " & Nm
		end if
	end repeat
end tell

Thank you. One problem has come up, that pesky 2 minute processing limitation. How can I extend this, I would love to point this script at an entire drive. IT is having a problem when it gets stuck also. IT gives the finder a spinning beach ball for a very long time. I have to force restart the finder when this happens (or be more patient I am not sure)

Can you suggest way to error trap this? The routine I took part of the script from used to count all the files, perhaps I could decide how many files my system can process in the two minute time, less 5% or so to be sure, and have the folder size check that first. IF it is over a size show a message that it is to big and try again with a smaller folder.

Suggestions? Thanks.

You can extend the time with timeout command, see below:
set theFolder to (choose folder)

tell application "Finder"
	with timeout of 300 seconds
		repeat with oneFile in (get every file of the entire contents of theFolder whose name extension is in {"JPG", "JPEG", "TIFF", "TIF"})
			set {comment:Co, name:Nm} to oneFile
			if Co is "" then
				set comment of contents of oneFile to Nm
			else
				set comment of contents of oneFile to Co & " " & Nm
			end if
		end repeat
	end timeout
end tell

If it takes that long, try the shell version which uses Spotlight for searching

set theFolder to quoted form of POSIX path of (choose folder)
set theFiles to paragraphs of (do shell script "mdfind -onlyin " & theFolder & " 'kMDItemKind  = \"*TIF*\"|| kMDItemKind  = \"*JPEG*\" '")
tell application "Finder"
	repeat with oneFile in theFiles
		set {comment:Co, name:Nm} to POSIX file (contents of oneFile) as alias
		if Co is "" then
			set comment of F to Nm
		else
			if Co does not contain Nm then -- avoids apending the name more then once
				set comment of F to Co & " " & Nm
			end if
		end if
	end repeat
end tell