Automate ‘passworded’ zip archiving of a selected file in Macintosh Finder

Surprisingly it’s a pain in the posterior to (zip) archive a file on MacOS with a password from the desktop.

Below is an Applescript that uses the library (available here) that automates the creation of a password protected zip archive of the currently selected file in MacOS Finder. Even better, the attached Automator Service Script containing this Applecript can be downloaded and double clicked to install a service called ‘Archive with password’ onto the Finder’s contextual menu (mouse right click). Whichever file is currently selected in the Finder can be archived with a preset password or the password can be changed in the dialog box that appears during runtime.

Edit this Applescript in Script Editor to change the default password and then drop it into your Fastscripts Apple Menu for easy access. Alternatively you can create an Automator Service where the Service receives selected files or folders in Finder and which runs an Applescript, and then add the contents of the script below (with a little modification noted with ‘*’ below) into the Automator Script and save it. Then double click this Automator Script in the Finder to install it into your system. (If none of that makes sense then download the automator workflow script below.

Don’t forget to download the library file into your User/Library/Scripting Additions folder or somewhere sensible and reference the location to load the library. For User/Library/Scripting Additions folder Select the Go menu in the Finder with the option key held down to first access the User Library folder). Once you start using the Library, you’ll find Applescript will be so much simpler easier and more functional.

Run the Applescript from the Fastscripts menu

Right click mouse to access the Archive selected file with password

--Applescript to zip (encrypt) with a password the selected file in the Finder.
--To add this script as an Automater Service (so as to be right mouse button clickable ) remove the 'property parent' line below 
-- and remove --* from all lines below.

global runIt

-- * set script_path to (((path to scripting additions from local domain) as text) & " Library")
-- * set myscript to load script file script_path
-- * tell myscript to run

property parent : load script alias (((path to scripting additions from local domain) as text) & " Library")

tell application "Finder"
	set sel to selection
	if sel is not {} then
		set myAlias to sel's item 1 as alias
		set myAliasPOSIX to POSIX path of myAlias
		display dialog "No file is selected for archiving"
	end if
end tell

set theFile to myAlias

-- * tell myscript 

set {theName, theFolder} to {name, folder} of DividePath(theFile)
set {theBase, theSuffix} to {base, suffix} of DivideFileName(theName)

-- * end tell

set sourceFilePOSIX to POSIX path of theFile
set q_sourceFilePOSIX to quoted form of sourceFilePOSIX

set archivePath to (theFolder & theBase & ".zip")
set archiveFile to (theBase & ".zip")
set q_archiveFile to quoted form of archiveFile

set archivePathPOSIX to POSIX path of archivePath
set q_archivePathPOSIX to quoted form of archivePathPOSIX

display dialog "Enter a password for encryption" default answer "Password"
set password1 to (text returned of result)
set runIt to ("zip -P " & password1 & " " & q_archivePathPOSIX & " " & q_sourceFilePOSIX)

tell application "Terminal"
	ignoring application responses
		do script (runIt)
	end ignoring
end tell

delay 3

tell application "Terminal"
end tell

Download the AppleScript file here

Archive selected with

Download the Automator Workflow file here

Automator Archive selected with