Applescript - Number of files in folder to folder name

Discussion in 'Mac Programming' started by SkaFreaks, Mar 25, 2015.

  1. SkaFreaks macrumors newbie

    Joined:
    Jun 29, 2012
    #1
    Hello,
    I am trying to get a script that will count the number of files in a selected folder and append it to the end of the folder name. Below is one of the ways I tried which I thought would work but it just keeps giving errors. Any help would be greatly appreciated.

    Thanks

    Code:
    tell application "Finder"
    	set FolderPath to selection
    	set oldBase to name of FolderPath
    	set numFiles to number of files in folder (((FolderPath) as text))
    	set newBase to oldBase & numFiles
    	
    	set FolderPath to newBase
    	
    end tell
    
     
  2. superscape macrumors 6502a

    superscape

    Joined:
    Feb 12, 2008
    Location:
    East Riding of Yorkshire, UK
    #2
    Hmmm...

    I think you need to have a good read of the Finder scripting dictionary. You're missing a few of the basics. However, I'd approach it something like this:

    Code:
    tell application "Finder"
    	
    	set theSelectedFolderList to selection
    	
    	repeat with theFolder in theSelectedFolderList
    		
    		if kind of theFolder is "folder" then
    			
    			set theFileCount to 0
    			set theOriginalName to displayed name of theFolder
    			set theFileList to every paragraph of (do shell script ("find " & quoted form of POSIX path of (theFolder as alias) & " ! -path \"*/.*\" -type f "))
    			
    			set name of theFolder to (displayed name of theFolder & " (" & (count of the items of theFileList) & " files)")
    		end if
    		
    		
    	end repeat
    	
    end tell
    There are plenty of other methods - many probably better than this!
     
  3. SkaFreaks thread starter macrumors newbie

    Joined:
    Jun 29, 2012
    #3
    That worked great.
    I'm definitely still a beginner when it comes to applescript and everything I've written so far dealt with itunes. I'll do some more reading on the Finder parts of the script.
    Thanks for the help.
     
  4. superscape macrumors 6502a

    superscape

    Joined:
    Feb 12, 2008
    Location:
    East Riding of Yorkshire, UK
    #4
    No worries.

    A good session looking at the scripting dictionary and poring over a few example scripts will probably be very helpful for you. A bit of trial and error never hurt, and always feel free to ask questions here!

    Good luck.
     

Share This Page