Zip for Web (UNIX zip tool)

Something about the way files are zipped when using Finder’s “Create Archive” contextual menu item causes problems with my web hosting provider. Because of these problems, I have taken to using the UNIX zip tool instead.

If you find yourself needing an AppleScript way of using zip, then you can use this as a starting point:

on run
	display dialog "Choose file or folders?" buttons {"Cancel", "Folders", "File"} default button 3 with icon note
	
	if button returned of result is "File" then
		choose file with prompt "Create ZIP file of this file:" without invisibles
		get {result}
	else
		choose folder with prompt "Create ZIP files of these folders:" with multiple selections allowed		
	end if
	
	open result
end run

on open droppedItems
	set ASTID to AppleScript's text item delimiters
	
	repeat with sourceFile in droppedItems
		set {sourceName, sourceExtension, sourceIsFolder} to {name, name extension, folder} of (info for sourceFile without size)
		
		set sourceFile to sourceFile as Unicode text
		set AppleScript's text item delimiters to {":"}
		
		if sourceIsFolder then
			set sourceParent to POSIX path of (text 1 thru text item -3 of sourceFile)
			set recursive to "r"
			
			get sourceName
		else
			set sourceParent to POSIX path of (text 1 thru text item -2 of sourceFile)
			set recursive to ""
		end if
		
		-- Remove extension from sourceName
		set AppleScript's text item delimiters to {"."}
		try
			get text 1 thru text item -2 of sourceName
		on error
			get sourceName
		end try
		
		do shell script "/usr/bin/python -c \"import sys; print unicode(sys.argv[1], 'utf8').lower().replace(' ','_').encode('utf8')\" " & quoted form of result
		
		if sourceExtension is in {"applescript", "scpt", "scptd", "app"} then
			get result & "_"
		else if sourceExtension is "dmg" then
			get result & ".dmg.zip"
		else
			get result & ".zip"
		end if
		
		-- Get path to zip file
		choose file name with prompt "Save zip file as:" default name (result)
		get result as Unicode text
		
		if (text -4 through -1 of result) is not ".zip" then
			set zipFile to POSIX path of result & ".zip"
		else
			set zipFile to POSIX path of result
		end if
		
		try
			do shell script "cd " & quoted form of sourceParent & "; /usr/bin/zip -qo9" & recursive & space & quoted form of zipFile & space & quoted form of sourceName
		on error errMsg number errNum
			set AppleScript's text item delimiters to ASTID
			display alert "Error while creating ZIP file. (" & errNum & ")" message errMsg buttons {"Cancel"} default button 1
			error number -128
		end try
	end repeat
	
	beep 2
	set AppleScript's text item delimiters to ASTID
end open

is there a way to encrypt a zipped file using applescript!?

thx

tony

Tony, the (command line) zip tool that ships with OS X does not support encryption. (If you’d like to add it, then check out this hint at Mac OS X Hints.)

If it did, then you could use something like this.

Find these lines.

try
			do shell script "cd " & quoted form of sourceParent & "; /usr/bin/zip -qo9" & recursive & space & quoted form of zipFile & space & quoted form of sourceName

.and replace them with something like this:

display dialog "Password:" default answer "" buttons {"Encrypt", "Cancel", "Don't Encrypt"} default button 3 with hidden answer
		
		if (button returned of result) is "Encrypt" then
			set encryption to "e -p " & (text returned of result)
		else
			set encryption to ""
		end if
		
		try
			do shell script "cd " & quoted form of sourceParent & "; /usr/bin/zip -qo9" & recursive & encryption & space & quoted form of zipFile & space & quoted form of sourceName