my first USEFUL script

Hi… I have directories which contain tons of .GZ & .zip files which I wanted to create a script to do so…

However, I also found that within these directories are a lot of subdirectories also containing lots of .GZ & .zip files… so this is my first useful script that I created:

set x to 0 --the number of files extracted
set y to 0 --the number of folders deleted
set folderDefinition to (choose folder with prompt “Collins Anti-GZIP & ZIP script” without invisibles)
set folderString to (folderDefinition as string)

tell application “Finder”
open alias folderString
set numfolders to the name of every folder in front window
set folderCount to (count numfolders)

if folderCount > 1 then
	set buttonResponse to the button returned of (display dialog "Move out all files from the " & folderCount & " subdirectories found in " & folderString & "?" buttons {"Yes", "No"} default button 1)
	
end if
if folderCount = 1 then
	set buttonResponse to the button returned of (display dialog "Move out all files from the " & folderCount & " subdirectory found in " & folderString & "?" buttons {"Yes", "No"} default button 1)
end if

if buttonResponse = "Yes" then
	repeat with curDir in numfolders
		set curString to (curDir as string)
		if curString ? "oldzips" then
			set curLocation to folderString & curString
			--display dialog "copying " & curLocation & " to " & folderString
			move every file of the folder curLocation to the folder folderString
			delete the folder curLocation
		end if
		set y to y + 1
	end repeat
end if
beep
display dialog "" & y & " Folders deleted and their contents were emptied to " & folderString

if not (the folder "oldzips" of folderDefinition exists) then
	make new folder in folderDefinition with properties {name:"oldzips"}
end if
set noFiles to every file in front window

repeat with curFile in noFiles
	(* this was to delete files unzipped files
	
	if the name of curFile contains ".dsk" and the name of curFile does not contain ".dsk." then
		delete curFile
	end if *)
	
	if the name of curFile contains ".gz" then
		set curString to (curFile as string)
		tell application "StuffIt Expander"
			expand the file curString
		end tell
		move the file curString to the folder "oldzips" of folderDefinition
		set x to x + 1
	else if the name of curFile contains ".zip" then
		set curString to (curFile as string)
		tell application "StuffIt Expander"
			expand the file curString
		end tell
		move the file curString to the folder "oldzips" of folderDefinition
		set x to x + 1
	end if
end repeat

end tell
beep
display dialog “You have successfully converted " & x & " files.”

-------- END OF SCRIPT ------------

So… my question is, does anyone have any suggestions on how I could easily make this recursive so that the first section (involving y) will see folders within folders within folders?

Secondly… I am finding that I still have some .gzip files but they don’t have a .gz extention… So should I be operating off of a filetype/creator comparison rather than “.gz” and “zip”?

and Finally… If I run this script and it creates the folder “oldzips”, when I run it a second time, it will see this folder and ask if I want to empty its contents (which I don’t)… Is there an easy way to say:

set numfolders to the name of every folder in front window EXCEPT for “oldzips”

??

also, is there a way to GOTO a place within a script without using subroutines? Like a way to say:

beginning:
set x to “10”
set x to x - 1
display dialog X
go to beginning

???

Or is the best way to accomplish this with repeat and end repeat? (is there a repeat for infinity?)

further, any suggestions on how I could be more efficient with my scripting?

thanks so much!!!

Hi Patrick,

Congratulations on your first useful script.

I haven’t looked through your whole script yet, but here’s some examples of recursion. There are many different ways to do this. The Finder is really helpful when doing this, but there might be a better way using unix and do shell script. Anyway, the followinf uses the Finder to get a list of all file with certain properties.

set f to (choose folder)
set file_list to DoProcess(f)
repeat with this_file in file_list
display dialog “I live at:” & return & (this_file as string)
end repeat
– returns list of mp3s of f
on DoProcess(f)
tell application “Finder”
try
set s to (every file of f whose ¬
file type is "mp3 " or ¬
name extension is “mp3” or ¬
name ends with “.mp3”) as alias list
on error – only one item
set s to (first file of f whose ¬
file type is "mp3 " or ¬
name extension is “mp3” or ¬
name ends with “.mp3”) as alias as list
end try
set folder_list to (every folder of f) – Finder reference, ok
end tell
repeat with this_folder in folder_list
set s to s & my DoProcess(this_folder as alias)
end repeat
return s
end DoProcess

When you use Finder, you get Finder references. Most apps won’t be able to use references in that form, so the must be coerced to alias reference or some other form. Currently, the Finder corcion ‘as alias list’ works pretty well except for one thing. If there is only one item when using the every element reference form, then it will error.

gl,

Hi,

The infinite repeat loop:

repeat
– statements
end repeat

You shouldn’t do this, because you may have problems quitting the script unless there’s a break in there for something to interrupt. Sometimes I use the unix ‘sleep’:

repeat
– some statements
do shell script “sleep 2”
end repeat

or an idle handler (see AppleScriptLanguageGuide).

gl,