Zip with Shell command

LONG STORY SHORT: I created a script a while back that uses quite a few different apps to accomplish it’s task. Now, I am trying to remove any software from the script that could be done with the OS and/or shell scripts. (I’m thinking it ought to simplify things!) I originally used Stuffit to Zip files but, instead, I want to use a shell command I found somewhere a while back to simplify the script.

Here is the segment of script I’m trying to work on that will ultimately go into the larger script. (It works sometimes.):

tell application "Finder"
	set this_folder to choose folder
	set only_folders to every folder of this_folder -- Don't need aliases here. The Finder understands its own references.
	repeat with i in only_folders
		set theName to name of i
		if length of theName is greater than 20 then
			set theName to text 1 through 20 of theName
			set the name of i to theName
		end if
	end repeat
	set only_folders to every folder of this_folder
	repeat with e in only_folders
		set theItem to e as alias
		set itemPath to (quoted form of POSIX path of theItem)
		set fileName to name of theItem
		set theFolder to POSIX path of (container of theItem as alias)
		set zipFile to quoted form of (theFolder & fileName & ".zip")
		try
			do shell script "zip -r -j " & zipFile & " " & itemPath
			-- delay 1 -- this may be needed (and adjusted) for larger folders?
		on error -- We don't need to say anything if there are no .DS_Store files
			do shell script "zip -d " & zipFile & " '*.DS_Store'"
		end try
	end repeat
end tell

The error I get about half of the time has to do with “.DS_Store” files. Usually, this occurs whenever I am Zipping a folder that contains other folders within it. I get an error that reads:
[b]zip warning: first full name: /Users/Account/Documents/Folder/Folder/FolderName/Fonts/.DS_Store
zip warning: first full name: /Users/Account/Documents/Folder/Folder/FolderName/.DS_Store
zip warning: name in zip file repeated: .DS_Store

zip error: Invalid command arguments (cannot repeat names in zip file)[/b]

Ideally, this little segment of script should Zip every folder inside of the chosen folder. For my purposes, the folders it will zip are collected artwork folders created by software such as FlightCheck or ArtFiles. Within each of these “collected folders” are subfolders for “fonts” and “images”. It seems that the shell is having a hard time with some of the invisible “.DS_Store” files assigned to these folders.

The script I have put together here seems to have an extra shell command that ought to deal with “.DS_Store” files but I’m not sure how to make it work (or even know if this is the proper solution for my issue). Any suggestions appreciated! (Please comment out any suggestions.)

See StefanK’s ZipIt. He has one for multiple files here too.

This will cause problems for any files that have the same name.

Why not exclude those files in the first place (with the -x option)?

See also: Zip for Web

OK folks, thanks for the suggestions. I’ve come up with another option that works, sort of.

tell application "Finder"
	set this_folder to choose folder
	set only_folders to every folder of this_folder -- Don't need aliases here. The Finder understands its own references.
	repeat with i in only_folders
		set theName to name of i
		if length of theName is greater than 20 then
			set theName to text 1 through 20 of theName
			set the name of i to theName
		end if
	end repeat
	set only_folders to every folder of this_folder
	get only_folders
	delay 3
	repeat with e in only_folders
		set {name:Nm, name extension:Ex} to info for e as alias
		set source to POSIX path of (e as alias)
		set parentFolder to text 1 thru ((offset of Nm in source) - 1) of source
		if Ex is missing value then set Ex to ""
		if Ex is not "" then set Nm to text 1 thru ((count Nm) - (count Ex) - 1) of Nm
		set destination to parentFolder & Nm & ".zip"
		do shell script "/usr/bin/ditto -c -k -rsrc --keepParent " & quoted form of source & " " & quoted form of destination
		return POSIX file destination as alias
	end repeat
end tell

This handles the folders that the other one had problems with. (No errors created by “.DS_Store” files.) Only problem left is that I can only get it to process the first folder. Any thoughts on how to get it to move through all the folders?

Also, if I wanted to stick with the

"do shell script "zip""

format, can someone point me toward a clear dictionary of what all the -r, -j, -d, -x, etc. are and how they function? I’d like to understand better.

You can read the man page; Enter this into Terminal:

man zip

Alternatively, you can read Apple’s online copy: http://developer.apple.com/documentation/Darwin/Reference/ManPages/index.html