Encrypt and Password Protect a .zip on a Mac using Terminal or Automator Service

I recently needed to provide some sensitive documents to someone and the quickest method was email. However, I wanted to secure the contents (even just a little) before sending.

I was able to quickly complete the task using the Terminal and entering the following command string thanks to an article I found on imore.com. First change your directory in the terminal as this will eliminate the user path file structure being included in the saved zip.

zip -er FILENAME.zip FILESorFOLDERStoCOMPRESS

Later, I thought this might be something I’d need to do again and I wondered if there might be a way to turn this into a service or app. There are are several options out there, but this method from Macworld has seemed to work great. I included the instructions below…

OpenAutomator and choose “Service” (the gear). Change “Service receives selected” to Files or folders in “Finder.app”

Add the “Run Applescript” step and then copy the code below and replace all the code in the “Run Applescript” command with this code. 

Choose save, naming it something like “Make Protected Zip”, then test it by going to the finder and selecting one or more files/folders. Scroll down to the “Services” Menu and select the service with the name you just saved as.

on run {input, parameters}
	set dialogResults to display dialog "Name for zipped file (no extension)" default answer "Archive" buttons {"OK", "Cancel"} default button "OK"
	if button returned of dialogResults is "OK" then
		
		set passwd to "x"
		set passwd_2 to "y"
		repeat while passwd ≠ passwd_2
			tell application "System Events"
				activate
				set passwd to text returned of (display dialog "password for zipped file" default answer "" buttons {"OK", "Cancel"} default button "OK" with hidden answer)
				set passwd_2 to text returned of (display dialog "Please reenter your password" default answer "" buttons {"OK", "Cancel"} default button "OK" with hidden answer)
			end tell
			
		end repeat
		
		set archiveName to text returned of dialogResults
		
		tell application "Finder"
			set archiveFileName to archiveName & ".zip"
			-- Append on a number if file exists.
			set suffix to 1
			set theFileExists to true
			repeat while theFileExists
				try
					set archiveFile to ((container of (item 1 of input) as Unicode text) & archiveFileName)
					if exists file archiveFile then
						set archiveFileName to archiveName & suffix & ".zip"
						set suffix to suffix + 1
					else
						set theFileExists to false
					end if
				end try
			end repeat
		end tell
		set itemStr to ""
		repeat with thisItem in input
			set itemPath to quoted form of (POSIX path of thisItem)
			tell application "Finder"
				set parentFolder to POSIX path of (container of thisItem as alias)
			end tell
			set itemStr to itemStr & " " & itemPath
		end repeat
		set zipFile to quoted form of (parentFolder & archiveFileName)
		set cmd to "zip -P " & passwd & " -rj " & zipFile & " " & itemStr & " -x *.DS_Store"
		do shell script cmd
	end if
	return
end run

Add a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.