Create encrypted Zip archive with password

I created a script application that will create an password protected encrypted zip archive.
It works both as double clickable, where it asks for files, and drag and drop where you drop files on it.

It will ask for password, checking to make sure it doesn’t have spaces in it, 3 times.

enjoy


use AppleScript version "2.4" -- Yosemite (10.10) or later
use scripting additions

on run
	local mfiles, fpaths, tmp
	try
		set mfiles to choose file with prompt "Choose files to create archive…" with multiple selections allowed
	on error
		return
	end try
	set fpaths to {}
	repeat with i in mfiles
		set tmp to quoted form of POSIX path of i
		-- remove trailing "/" on folders
		if text -2 of tmp is "/" then set tmp to text 1 thru -3 of tmp & "'"
		set end of fpaths to tmp
	end repeat
	compressArchive(fpaths)
end run

on open mfiles
	local fpaths, pwd, tmp
	if (count mfiles) = 0 then return
	set fpaths to {}
	repeat with i in mfiles
		set tmp to quoted form of POSIX path of i
		-- remove trailing "/" on folders
		if text -2 of tmp is "/" then set tmp to text 1 thru -3 of tmp & "'"
		set end of fpaths to tmp
	end repeat
	compressArchive(fpaths)
end open

on compressArchive(fpaths)
	local pb, pwd
	set pb to true
	repeat with i from 1 to 3
		try
			set pwd to text returned of (display dialog "Enter password to encrypt this archive…" default answer "")
		on error
			return
		end try
		if (pwd contains " ") then
			(item (((i = 3) as integer) + 1) of {"Please try entering password again.", ""})
			display alert "Error! Password cannot contain spaces." message (item (((i = 3) as integer) + 1) of {"Please try entering password again.", "Cancelling archive."}) as warning
		else
			set pb to false
			exit repeat
		end if
	end repeat
	if pb then return false
	set text item delimiters to " "
	do shell script "zip -er ~/Desktop/archive" & (random number from 1000 to 9999) & " -P " & pwd & " " & (fpaths as text)
	set text item delimiters to ""
end compressArchive

I just noticed a slight bug.

I’m on Catalina and when I drag & drop 2 files on this app to compress into one archive,
sometimes it runs “on open” once with 2 files passed to it, and sometimes it runs it twice
with one file passed to it each time.

Weird

I made a modified version with a stay-open app and an idle handler to work around it.

Let me know if anyone wants it…

That’s an issue with quarantined files in all droplets – generally files you’ve downloaded somewhere and haven’t yet moved them somewhere else.

And last I looked, ditto was a better choice for retaining all Mac info than zip.

Newbie question here: Is it possible to set the password in the applescript code itself instead of asking for it in a dialog ?

Thanks!
I can see the path is represented as folders in the zip file - if I don’t want that, what should be changed in the code?

The zip utility has an option that will do what you want:

-j --junk-paths. Store just the name of a saved file (junk the path), and do not store directory names. By default, zip will store the full path (relative to the current directory).

So, just change “-er” in Robert’s script to “-erj” (no quotes), and paths are not saved. BTW, with this option enabled, the script will throw an error if the files being zipped contain duplicate files names.