Add file count to folder name

Hello everyone,
My knowledge of Applescripts spans about 1 hour and I am trying to modify the script below. I am trying to get a directorys name to reflect the number of files within the directory but I am having trouble appending the number of files to the original directory name. For example, if the directorys name is “A” I would like it to say A (52). Currently I can only get it working bt defining the string “A” when it renames the folder. How would I go about saving the old Folder name to a variable so it can be used in the new directory name.

any help is greatly appreciated.

thanks!

on adding folder items to thisFolder after receiving addedItems
	my setTheCount(thisFolder)
end adding folder items to

on removing folder items from thisFolder after losing addedItems
	my setTheCount(thisFolder)
end removing folder items from

on setTheCount(theFolderAlias)
	tell application "Finder"
		set theFolder to (folder theFolderAlias)
		set myCount to the count of (every item of theFolder)
		set nameEnd to ""
		if myCount > 0 then set nameEnd to " (" & myCount & ")"
		set name of theFolder to ("A") & nameEnd
	end tell
end setTheCount

This is how you get the baseName and add it back. I didn’t make it into a Folder Action, so it just runs the code at the bottom changing the name of a file on the desktop from MyFile (2) to MyFile (‘n’) where ‘n’ is the number of files in the folder.

(*
on adding folder items to thisFolder after receiving addedItems
	my setTheCount(thisFolder)
end adding folder items to

on removing folder items from thisFolder after losing addedItems
	my setTheCount(thisFolder)
end removing folder items from
*)

on setTheCount(theFolderAlias)
	tell application "Finder"
		set theFolder to (folder theFolderAlias)
		set theName to name of theFolder
		set myCount to the count of (every item of theFolder)
		-- I added this part to get the name without the number
		set otid to AppleScript's text item delimiters
		set AppleScript's text item delimiters to "("
		set baseName to text item 1 of theName
		set AppleScript's text item delimiters to otid
		-- at this point I have the baseName
		if myCount > 0 then set nameEnd to "(" & myCount & ")" -- notice I took the space out
		set name of theFolder to baseName & nameEnd -- baseName instead of "A"
	end tell
end setTheCount

set theFolder to ((path to desktop as text) & "MyFile (2)") as alias
setTheCount(theFolder)

Would try to Adam? I’m having problems with this:

on adding folder items to thisFolder after receiving addedItems
	setTheCount(thisFolder)
end adding folder items to

on removing folder items from thisFolder after losing addedItems
	setTheCount(thisFolder)
end removing folder items from

on setTheCount(theFolder)
	tell application "Finder"
		set ASTID to AppleScript's text item delimiters
		set AppleScript's text item delimiters to {" ("}
		try
			set theName to text 1 thru text item -2 of (name of theFolder)
		on error
			set theName to (name of theFolder)
		end try
		set AppleScript's text item delimiters to ASTID
		
		set theCount to count (get every item of theFolder)
		
		try
			if result > 0 then
				set theFolder's name to theName & " (" & theCount & ")"
			else
				set theFolder's name to theName
			end if
		on error errorMsg number errorNum
			display dialog "Error (" & errorNum & "):" & return & return & errorMsg buttons "Cancel" default button 1 with icon caution
		end try
	end tell
end setTheCount

It doesn’t stop renaming the folder! (I delete the folder to make it stop.)

Thank a bunch for your help!

One thing I did notice is when I drag files into the directory it updates but when I remove them the count does not change. I’m just curious if you know what might be causing it. Im playing with it now.

its the space before the left paren

set theFolder’s name to theName & " (" & theCount & “)”

                                              ^___ this space makes it repeat.

No, that doesn’t change anything.

This is what I changed it to, and it works fine now.

on adding folder items to thisFolder after receiving addedItems
	my setTheCount(thisFolder)
end adding folder items to

on removing folder items from thisFolder after losing addedItems
	my setTheCount(thisFolder)
end removing folder items from

on setTheCount(theFolderAlias)
	tell application "Finder"
		set theFolder to (folder theFolderAlias)
		set theName to name of theFolder
		set myCount to the count of (every item of theFolder)
		-- I added this part to get the name without the number
		set otid to AppleScript's text item delimiters
		set AppleScript's text item delimiters to "("
		set baseName to text item 1 of theName
		set AppleScript's text item delimiters to otid
		-- at this point I have the baseName
		if myCount > 0 then
			set nameEnd to "(" & myCount & ")" -- notice I took the space out
			set name of theFolder to baseName & nameEnd -- baseName instead of "A"
		else
			if myCount = 0 then set name of theFolder to baseName
		end if
	end tell
end setTheCount

I think you need to change it in both places, here too:
set AppleScript’s text item delimiters to {" ("}

Still doesn’t change anything.

It works fine when not used as a folder action.

setTheCount(choose folder)

Something is causing the action to fire repeatedly.

Something strange going on then, Bruce. When I download Paulers last version and attach it to a folder, it works both ways very nicely. OS X 10.4.6, AppleScript version 1.10.6. Thanks to Paulers for the neat idea. He should post it to Code Exchange (see link below)

[SECOND THOUGHT] I’ve tested now even emptying the folder in which case the parentheses and number disappear. They return if I add a bunch of files back. It even gets it right for me if it has to wait for some movie files to copy in. Key item seems to be that if you want a space after the base name, you have to put it in the baseName with no parens attached (empty folder).

Adam

PS: to get AppleScript Version

-- Based on research by Richard Morton in the early days of "Code Exchange". Script by Nigel Garvey

-- The StandardAdditions's 'system attribute' used to be the Finder's 'computer' command.
tell application "Finder" to set ascv to (system attribute "ascv") mod 65536

-- For most practical purposes, scripts can compare this number
-- with some critical value worked out in advance.

-- But for display:
set hiDigit to ascv div 4096
if (hiDigit > 0) then
	set hiDigit to hiDigit as string
else
	set hiDigit to ""
end if
return hiDigit & ascv mod 4096 div 256 & "." & ascv mod 256 div 16 & "." & ascv mod 16

-- For the system version, change '(system attribute "ascv") mod 65536' to '(system attribute "sysv").

To get the text form of AppleScript’s version:

AppleScript's version

Indeed.

You’re quite right, Bruce.

The perpetual renaming problem was actually caused in the try statement:

try
	set theName to text 1 thru text item -2 of (name of theFolder)
on error
	set theName to (name of theFolder)
end try

That’s because the expression (name of theFolder) returns a property; it has not yet been evaluated as text. So the attempt to extract any text element fails with an error number -1728 [errAENoSuchObject]: “Can’t get text 1 thru text item -2 of name of alias…”, etc.

As a result of this error, the script assumes (for the most part, erroneously) that the folder’s name does not end with a parenthesised number. So it adds one. Every time.

Whenever the folder name changes, the Folder Action kicks in again, assuming that the existing items have just been added. (Wrong, but there you are.) Normally, things would settle down after the second iteration, because nothing else changes. However, in this case, the name change triggers the Folder Action, causing the name to change again, causing the Folder Action to trigger again… and so on.

The fix is to either attribute the name to a variable (as was done in the later version) or to use (get name of theFolder). In either case, the result is then evaluated as text - and returns the expected result.

That said, the idea behind the text extraction method was sound enough. The latest version won’t perform correctly if a name already contains an opening parenthesis. However, something like this might do the trick:

on setCount(folderAlias)
	tell application "Finder" to tell (folder folderAlias)
		tell (get name) to try
			if it ends with " [" & (last word as integer) & "]" then
				set tid to AppleScript's text item delimiters
				set AppleScript's text item delimiters to " ["
				set baseName to text 1 thru text item -2
				set AppleScript's text item delimiters to tid
			else
				set baseName to it
			end if
		on error
			set baseName to it
		end try
		set myCount to count items
		if myCount is 0 then
			set name to baseName
		else
			set name to baseName & " [" & myCount & "]"
		end if
	end tell
end setCount

on adding folder items to thisFolder
	setCount(thisFolder)
end adding folder items to

on removing folder items from thisFolder
	setCount(thisFolder)
end removing folder items from

Edit: parentheses changed to brackets (see discussion below).

I must have played with 7 versions (if that’s possible) of that, recalling that it existed, but not finding it. Somehow didn’t think of “version of AppleScript” all by itself. :mad:

Vis-a-vis your solution, as I see it anyway, you have added protection for the possibility that the name already contains an opening parenthesis, correct? (e.g. “myFile(jpegs)”) Was there anything else that I’ve missed?

Know what you mean, Adam. Sometimes the simplest things are also the easiest to overlook. :wink:

Besides version, Applescript’s properties also include pi, return, space, tab - and, of course, text item delimiters.

If the name ends with a space followed by a parenthesised integer, then my suggestion would consider it an appended item count. Anything else, such as “Project Files (Jack)”, “Year End (2005) Results” or “Mailshot (Easter 2006)” would be regarded as entire basenames - as would “Spring (Chick’n-Lick’n) Promotion(2006)” and “Australian Tour 2006”. However, a name like “Concert Schedule (2006)” would clearly not.

I’ve changed the version I saved to use [34] as being less likely to be in a file name. Same problem if they are, of course.

I tend to use brackets myself for this kind of thing - and for similar reasons. In this case, I was going along with the general trend of the discussion - but, since you’ve raised the matter, I’ve now modified the above routine accordingly.

I was assuming that.

Bah. Thanks for the clarification kai. :cool:

Adam, I mentioned AppleScript’s properties in an e-mail about a week ago.

Does this affect anything?

class of AppleScript's version
--> version

Actually, while AppleScript’s version class can be easily coerced to text, it’s a class in its own right:

{class of pi, class of return, class of space, class of tab, class of text item delimiters, class of version}
--> {real, string, string, string, list, version} 

The version class contains more complex data than its appearance suggests:

version --> "1.10.3"

For example, we can’t coerce text in such a form to a number or real:

"1.10.3" as real --> Can't make "1.10.3" into type real.

With version, however, we can:

version as real --> 1.1003

The version class even contains embedded copyright data - which, while normally not that easily extracted, is there, nonetheless. (Some non-printing characters had to be removed to get the result to display properly):

{{version:get version}} as string ”> "dle2TEXTjversversU£Ã„1.10.3G1.10.3, Copyright © 1990-2005 Apple Computer, Inc. All rights reserved."

Ahh, this much younger man assumes two things: 1) that I have total recall, and 2) that I made the connection between wanting the version of AS and AS properties listed by Kai. NOW, l remember.

Interesting that “AppleScript’s properties” errors.

A