A droplet for zipping files

Hi there,

I’ve recently created a script to run on Mac OS 9.2. I set out to write a script that would ZIP a list of files dropped on to it. Each of the files dropped on to the app would be zipped as a separate file i.e. if you dropped 10 files on you’d end up with 10 individual zip files.

I’ve listed the script below. The problems I seem to get vary from the machine freezing, to only one file being zipped out of several.

Any help would be greatly appreciated.

Thanks in advance,

Nick

Here’s the script

on deleteExtension(name)
if character ((number of characters in name) - 3) of name is “.” then
return characters 1 through ((number of characters in name) - 4) of name as string
else
return name
end if
end deleteExtension

on open fileList
set i to 1

if fileList is {} then
	display dialog "Drop files onto this application to zip them." buttons {"OK"}
else
	-- the following code is just to figure out 
	-- what the name of the zip archive should be 
	
	repeat with file_to_zip in fileList
		copy name of (info for (item i of fileList)) to fileName
		set fileName to deleteExtension(fileName)
		
		copy (fileName & ".zip") to zipFileName
		copy item 1 of fileList to firstFile
		
		set zipFolder to firstFile as string
		set AppleScript's text item delimiters to ":"
		set zipFolder to "" & zipFolder's text items 1 thru -2 & ":"
		set AppleScript's text item delimiters to {""}
		set zipFolder to zipFolder as alias
		
		-- this is the part that actually adds the files and zips them 
		
		
		tell application "ZipIt"
			activate
			set the_window to (make new window at beginning)
			
			add file_to_zip to the_window
			compress the_window name zipFileName in zipFolder
			save window 1
			close window 1
			
		end tell
		set i to i + 1
	end repeat
end if

end open

You’ve based your script on the sample script that comes with ZiptIt which is your first problem since that script isn’t very good to being with. Next, you’re mixing two different ways of iterating through a list. This is a much better way of doing what you want:

on run
    open {choose file}
end run

on open fileList
    tell application "ZipIt" to activate
    repeat with i from 1 to (count of fileList)
        set this_file to (item i of fileList)
        set fileName to my deleteExtension(name of (info for this_file))
        set zipFileName to (fileName & ".zip")
        set zipFolder to my getContainer(this_file as string)
        my make_zip_archive(this_file, zipFileName, zipFolder)
    end repeat
    try
        tell application "ZipIt" to quit
    end try
end open

on make_zip_archive(this_file, zipFileName, zipFolder)
    tell application "ZipIt"
        set the_window to (make new window at beginning)
        add this_file to the_window
        compress the_window name zipFileName in (zipFolder as alias)
        close window 1
    end tell
end make_zip_archive

on deleteExtension(the_name)
    if character -4 of the_name = "." then
        return text 1 thru -5 of the_name
    else if character -5 of the_name = "." then
        return text 1 thru -6 of the_name
    else
        return the_name
    end if
end deleteExtension

on getContainer(the_path)
    set old_delim to AppleScript's text item delimiters
    set AppleScript's text item delimiters to ":"
    set the_container to (text items 1 thru -2 of the_path) as string
    set AppleScript's text item delimiters to old_delim
    return (the_container & ":") as string
end getContainer

Jon

Thanks for the help Jon. I have to admit the solution was rather cobbled together from bits I’d found.

I’ve tried the code you posted but I seem to be getting one or two problems, the first time it crashed with an ‘the application has unexpectedly quit, because an error of type 1 occured’. After that I had to restart the machine. When trying the script after that I get a freezes. Any ideas? Could it be to do with ZipIt?
I pasted the script in script editor and saved it as an application, is that correct.

Thanks again for the help,

Nick

Yes, saving this script as an application is the way to go. Once you have saved it as an app, try increasing the amount of memory allocated to it (select it in the Finder and then hit Command-I and increasing the memory settings). The code is very basic AppleScript so perhaps the issue is with ZipIt but it could also be a conflict with your system. To be honest, this was not tested on OS 9 but on OS X. Did the script work at all, were any zip archives created? Can anyone else test this on OS 9?

Jon

Hi Jon,

Thanks for the latest info. Since my last post I’ve tried one or two things and had different results. The script has been working, perhaps I was a little premature with my last post. The results have been intermittent again, on one or two occasions all files dropped on to the app have been converted however on the whole I get mixed results. :? These results are from and OS9.2 system, however someone has just literally told me that the script works fine on OS10.2.8 which is great news. :smiley:

Do you have any ideas as to why we get the intermittent results in OS 9.2. I’ve tried increasing the memory size but that hasn’t done any good.

Thanks once again for all the time you’ve spent on this, it has been a big help. :smiley:

Regards

Nick

I’m not sure what to tell you. I just opened a script I used on OS 9 in 2000 for zipping files via ZipIt and it looks virtually the same as this code. Here is a very slightly tweaked version. If this doesn’t work, I’m afraid I’m out of suggestions. Perhaps you can test on other OS 9 machines. If that works, good luck exorcising your machine.

on run
    open {choose file}
end run

on open fileList
    tell application "ZipIt" to activate
    repeat with i from 1 to (count of fileList)
        set this_file to (item i of fileList) as alias
        set zipFileName to ((my deleteExtension(name of (info for this_file))) & ".zip")
        set zipFolder to my getContainer(this_file as string)
        my make_zip_archive(this_file, zipFileName, zipFolder)
    end repeat
    try
        tell application "ZipIt" to quit
    end try
end open

on make_zip_archive(this_file, zipFileName, zipFolder)
    tell application "ZipIt"
        set the_window to (make new window at beginning)
        add this_file to the_window
        compress the_window name zipFileName in (zipFolder as alias)
        close window the_window
    end tell
end make_zip_archive

on deleteExtension(the_name)
    if character -4 of the_name = "." then
        return text 1 thru -5 of the_name
    else if character -5 of the_name = "." then
        return text 1 thru -6 of the_name
    else
        return the_name
    end if
end deleteExtension

on getContainer(the_path)
    set old_delim to AppleScript's text item delimiters
    set AppleScript's text item delimiters to ":"
    set the_container to (text items 1 thru -2 of the_path) as string
    set AppleScript's text item delimiters to old_delim
    return (the_container & ":") as string
end getContainer

Jon