Declaring Variables
      

This script adds up occurances of various file types contained inside all the subfolders of a given folder. I keep a copy of my entire web site on my local hard drive and the stats on my home page are generated from the folder containing the local copy of this site.

This script is kind of slow, it takes almost 10 minutes to run on my 840av. I only update my site stats every once in a while.



The basic workings of this script are fairly simple. The following tell statement returns all the files of the type " GIFf " contained inside whatever folder you decide to select:
AppleScript Formatting:
      Language keywords
      Operators, References &
      Application Keywords
      Values
      Variables
      comments

. tell application "Finder"
      count (items
in (choose folder) whose file type is " GIFf ")
end tell
I put my results into a record called stats. This record contains an entry for every type of file I am counting:
. copy { textfiles : 0 , Giffiles : 0 , jpegfiles : 0 , qtfiles : 0 } to stats
The following adds 1 to the value of jpegfiles in stats, another way of seeing this is X=X+1. In JavaScript the same could be accomplished with X++:
/ copy ( jpegfiles of stats ) + 1 to ( jpegfiles of stats )
To count every item in a folder, simply group several pieces together:
. tell application " Finder "
      
copy (count (items in thisfolder whose name contains " .htm ")) + ( textfiles of stats ) to ( textfiles of stats )
      
copy (count (items in thisfolder whose file type is " GIFf ")) + ( Giffiles of stats ) to ( Giffiles of stats )
      
copy (count (items in thisfolder whose file type is " JPEG ")) + ( jpegfiles of stats ) to ( jpegfiles of stats )
      
copy (count (items in thisfolder whose file type is " MooV ")) + ( qtfiles of stats ) to ( qtfiles of stats )
end tell
A Recursive Subroutine
      

Since I am going to be scanning lots of folders with the same commands I placed all the counting commands into a subroutine named foldergetter(). Subroutines are sort of like embedded applications within a script, and closely parallel JavaScript functions. Once defined, subroutine's allow me to execute a set of actions with only one command.

thisfolder, stats are placeholders for information that will be passed into the subroutine. thisfolder is the folder to scan and stats is the record mentioned above.

Once defined, entering foldergetter(thisfolder, stats) into the script executes the following commands on thisfolder:


. on foldergetter (thisfolder, stats)
      
tell application " Finder "
            
copy (count (items in thisfolder whose name contains " .htm ")) + (textfiles of stats) to (textfiles of stats)
            
copy (count (items in thisfolder whose file type is " GIFf ")) + (Giffiles of stats) to (Giffiles of stats)
            
copy (count (items in thisfolder whose file type is " JPEG ")) + (jpegfiles of stats) to (jpegfiles of stats)
            
copy (count (items in thisfolder whose file type is " MooV ")) + (qtfiles of stats) to (qtfiles of stats)
            log
stats
            
repeat with i in ( get folders of thisfolder )
                  
tell me to foldergetter ( i , stats )
            
end repeat
      
end tell
      
return stats
end foldergetter

Notice that foldergetter() calls itself from within its own repeat loop. This creates a recursive loop acts to check every folder within every folder for as many folders as there are. The repeat variable i refers to a folder within the current folder.

Basically, foldergetter() does this:

  1. Counts the specified file types in the scanned folder.
  2. Makes a list of all subfolders in the scanned folder

foldergetter() then runs itself using the list of subfolders and continues to run itself on each subfolder, counting all the files in each, until there are no more folders.

Sleep break, 3/6/98 4:48 am, I'll try to finish this over the weekend


 

tell application " BBEdit 4.5 "
        activate
        make new window
        insert text "
<TABLE BORDER=\"2\" CELLPADDING=\"2\" CELLSPACING=\"0\"><TR BGCOLOR=\"#CCFFFF\"><TD ALIGN=\"RIGHT\"> "
        insert text (
textfiles of stats )
        insert text "
</TD><TD>HTML files</TD></TR><TR><TD ALIGN=\"RIGHT\"> "
        insert text (
Giffiles of stats )
        insert text "
</TD><TD>GIF Files</TD></TR><TR BGCOLOR=\"#CCFFFF\"><TD ALIGN=\"RIGHT\"> "
        insert text (
jpegfiles of stats )
        insert text "
</TD><TD>JPEG files</TD></TR><TR><TD ALIGN=\"RIGHT\"> "
        insert text (
qtfiles of stats )
        insert text "
</TD><TD>Quicktime files</TD></TR><TR BGCOLOR=\"#CCFFFF\"><TD ALIGN=\"RIGHT\"> "
        insert text ((
qtfiles of stats ) + ( jpegfiles of stats ) + ( Giffiles of stats ) + ( textfiles of stats ))
        insert text "
</TD><TD><B>Total files</B></TD></TR></TABLE> " & return
end tell

 

 


Declaring Variables
  copy { textfilsdfhjsdf
Putting it all together (uneditied, but this is the complete final script)

I thought about saying something liek "I believe in beautiful code", but that would ahve been too ridiculous. It's important to structure your scripts, if you don't choose to believe that, we'd probably get along pretty well. Not onc e in my life have I ever listened to a warning. I still check to see if paint s really wet, no matter where I am. But after building a few crazy scripts that drift for several screens, it becomes pretty evident that organization is the only road ot success.

That said, I like to declare my variables first, follow those with the scripts activity, and then finally end the script with a and include subroutines at the end, so my final script looks like this:

copy { textfiles : 0 , Giffiles : 0 , jpegfiles : 0 , qtfiles : 0 } to stats

tell application " Finder "
        activate
        
copy (choose folder) to thisfolder -- select the folder your site is locally mirrored in
        
tell me to foldergetter ( thisfolder , stats )
        log
stats

end tell
tell application " BBEdit 4.5 "
        activate
        make new window
        insert text "
<TABLE BORDER=\"2\" CELLPADDING=\"2\" CELLSPACING=\"0\"><TR BGCOLOR=\"#CCFFFF\"><TD ALIGN=\"RIGHT\"> "
        insert text (
textfiles of stats )
        insert text "
</TD><TD>HTML files</TD></TR><TR><TD ALIGN=\"RIGHT\"> "
        insert text (
Giffiles of stats )
        insert text "
</TD><TD>GIF Files</TD></TR><TR BGCOLOR=\"#CCFFFF\"><TD ALIGN=\"RIGHT\"> "
        insert text (
jpegfiles of stats )
        insert text "
</TD><TD>JPEG files</TD></TR><TR><TD ALIGN=\"RIGHT\"> "
        insert text (
qtfiles of stats )
        insert text "
</TD><TD>Quicktime files</TD></TR><TR BGCOLOR=\"#CCFFFF\"><TD ALIGN=\"RIGHT\"> "
        insert text ((
qtfiles of stats ) + ( jpegfiles of stats ) + ( Giffiles of stats ) + ( textfiles of stats ))
        insert text "
</TD><TD><B>Total files</B></TD></TR></TABLE> " & return
end tell

 

on foldergetter ( thisfolder , stats )
        
tell application " Finder "
                
copy (count (items in thisfolder whose name contains " .htm ")) + ( textfiles of stats ) to ( textfiles of stats )
                
copy (count (items in thisfolder whose file type is " GIFf ")) + ( Giffiles of stats ) to ( Giffiles of stats )
                
copy (count (items in thisfolder whose file type is " JPEG ")) + ( jpegfiles of stats ) to ( jpegfiles of stats )
                
copy (count (items in thisfolder whose file type is " MooV ")) + ( qtfiles of stats ) to ( qtfiles of stats )
                log
stats
                
repeat with i in ( get folders of thisfolder )
                        
tell me to foldergetter ( i , stats )
                
end repeat
        
end tell
        
return stats
end foldergetter

Some notes:

I'm not starting on the ground floor. At least I don't think I am. OK, I probably will explain some simple ideas, but I don't think I'm a very sophisticated scripter, and these scripts are just collections of repetitive simple tasks. Which is one of the main reason scripting exists: To automate repetitive tasks. To date, little I';ve encountered has been more repetitive that updating my www pages. Which is the reson fo rth e long delay. I wanted to get this site into something that I could support with minimal work. I'm getting there. The scritps are allowing me to creqate pages that appear veryu dynamic, or at least they seem that I might do nothing else with my life than this. Whihc thanks to scripting is only half true.

I really like Applescript. I first got into it while trying to make the progress bar project actually work in the operating system. It didn't seem enough to me to use a computer only to make pictures. At that time I wan't working much and spent a whole summer in Barnes & Noble on 6th avenue and 22 st reading. Applescript was one of the things I rea about.

Whew, we're ranting again. If you're reading this, you asked for it. I said that it was barely a sketch and I meant it.

Alright, where to start...