Can't encrypt .zip files in Finder - revisited

There are many posts in these Discussion asking about encrypting zip files on MacBooks/OSX, but nearly all solutions say to use Terminal. However, Terminal is a bit clumsy, particularly for zipping multiple files, and being able to do it in Finder instead would be much simpler.


So the following AppleScript uses the standard, freely available OSX 'zip' command that the other Terminal solutions use, but wraps it into a user-friendly Finder solution. I have tried to post answers describing this solution to several related questions in these Discussions, but they all appear to be closed, and they all typically say you can't use Finder, which is wrong... With the following caveat, enabling your Finder to encrypt your zip files is very easy.


Caveat: this script uses the 'zip -P' optional parameter to supply the encryption password, and so users must be aware that the password you are using to encrypt the zip file will potentially be briefly visible to any other users on the MacBook that you are using. It will only be visible whilst the underlying 'zip -P' command is doing the encryption, and it's only visible if the other user runs one of a few special commands/tools just at the critical moment. If you are the only user at the time, and the encryption password you use is a "one-off" password just for that zip file, then the risks of the password being compromised are low.


To enable the following AppleScript on any MacBook, simply follow the instructions in the top 20 lines of the following script. Hope this is helpful - I find GUI-based encryption like this important and helpful, hope you will too.

--Doug.


on run

-- AppleScript to use OSX’s native ‘zip’ WITH OPTIONAL ENCRYPTION in the Mac Finder via Services

-- coded by Doug Fromoz Jan 2018.

-- Usage/installation of this script:

-- In Automator…

-- open a new Service workflow,

-- in the Actions > Library pane select "Utilities"

-- in the Actions sub-pane select "Run Applescript" and drag it into the workflow editor/builder

-- select drop-downs: Service receives selected "files or folders" in "Finder.app"

-- paste this whole script into the "Run Applescript" window

-- then go "File > Save as > osx_zip" (saves as a Service)

-- That completes the installation of the osx_zip Service for Finder.

-- This Finder Service optionally uses the native OSX zip -P command for encryption - note, -P is NOT SECURE on multi-user systems

-- To then use in Finder, either...

-- select a .zip file and go "Finder > Services > osx_zip",

-- then follow instructions about supplying a password if required, etc

-- or select one or more files and go "Finder > Services > osx_zip",

-- then follow instructions about setting password, etc

-- WARNING: the Service will overwrite existing zip files without asking permission, and will rename existing files with suffix ~[n] if files from the zip file clash

-- REPEAT WARNING: the ‘-P’ password parameter used for encryption is NOT SECURE in a multi-user environment - BE CAREFUL.


set exe_osx_zip to "/usr/bin/zip" --path to OSX’s zip programme

set exe_osx_unzip to "/usr/bin/unzip" --path to OSX’s unzip programme

tell application "Finder"

set these_files to the selection --get the files selected in Finder

if ((count of these_files) is 1 and the name extension of (item 1 of these_files) is "zip") then --unzip the selected archive

set zip_file to (item 1 of these_files) as alias --a collection of 1 files...

set zip_path to my quote_str(POSIX path of zip_file)

set question to display dialog "osx_unzip. WARNING: existing files will be renamed *~[n]. Enter password: (leave blank if none)" default answer "" buttons {"Cancel", "Unzip", "Show First"} default button 2

set choice to button returned of question

if choice is equal to "Cancel" then --abort (I think this is maybe redundant, the Cancel button aborts first)

error number -128

else if choice is equal to "Show First" then --display contents before unzipping

try

set outpt to do shell script exe_osx_unzip & " -l " & zip_path & " 2>&1"

on error error_message number error_number

display dialog "err msg: " & error_message & "; num: " & number & "; err: " & error_number buttons {"Cancel"} default button 1

end try

display dialog outpt buttons {"Cancel", "Unzip"}

end if

set pwd_str to text returned of question --the answer to the 1st question has optional password

set pwd_flag to my pwd_flag_fn(pwd_str) --password, if any

set folder_name to do shell script "/bin/echo `/usr/bin/dirname " & zip_path & "` 2>&1"

set folder_name to my quote_str(folder_name)

try

set outpt to do shell script exe_osx_unzip & " -B " & pwd_flag & zip_path & " 2>&1" -- '-B' creates ~[n] backups of files with same names as in ZIP

on error error_message number error_number

display dialog "err msg: " & error_message & "; num: " & number & "; err: " & error_number buttons {"Cancel"} default button 1

end try

display dialog outpt --the raw output message from ‘zip’

tell front window

update every item with necessity -- force Finder to show any new *~[n] backups created by the shell script

end tell

else -- zip the selected files into a zip archive

set question to display dialog "osx_zip. WARNING: existing zipfiles will be overwritten. Enter password: (leave blank if none)" default answer "" buttons {"Cancel", "Zip"} default button 2

set choice to button returned of question

if choice is equal to "Cancel" then --abort (I think this is maybe redundant, the Cancel button aborts first)

error number -128

end if

set pwd_str to text returned of question

set pwd_flag to my pwd_flag_fn(pwd_str) --password, if any

set this_file to (item 1 of these_files) as alias

set file_path to my quote_str(POSIX path of this_file)

set file_name to name of this_file

set folder_name to do shell script "/bin/echo `/usr/bin/dirname " & file_path & "` 2>&1"

set question to display dialog "Name of zip file (.zip will be added!): " default answer file_name buttons {"Cancel", "Continue"} default button 2

set zip_name to text returned of the result

set zip_path to my quote_str(folder_name & "/" & zip_name & ".zip")

repeat with i from 1 to the count of these_files

set this_file to (item i of these_files) as alias

set file_path to my quote_str(POSIX path of this_file)

try

set outpt to do shell script exe_osx_zip & " -r " & pwd_flag & zip_path & " " & file_path & " 2>&1" --the '-r' handles directories

on error error_message number error_number

display dialog "err msg: " & error_message & "; num: " & number & "; err: " & error_number buttons {"Cancel"} default button 1

end try

end repeat

end if

end tell

end run



--handlers (ie. functions)

on pwd_flag_fn(pwd_str)

--process the password string. if blank do nothing, else make it a parameter

if pwd_str is not "" then

set pwd_flag to " -P" & pwd_str & " "

else

set pwd_flag to " "

end if

return pwd_flag

end pwd_flag_fn



on quote_str(str)

--wrap string with quotes to protect it from the shell CLI

return "\"" & str & "\""

end quote_str

MacBook, macOS High Sierra (10.13.2)

Posted on Jan 28, 2018 11:28 PM

Reply

Similar questions

1 reply

Feb 14, 2018 11:25 PM in response to doug fromoz

This is a bit embarrassing, but the above script unzips files into a subdirectory with the absolute path of the original folder. To avoid that, you need to replace the zip line with following (after wrapping the 'folder_name' and 'file_name' in quotes using 'quote_str' )...


else -- zset outpt to do shell script "cd " & folder_name & ";" & exe_osx_zip & " -r " & pwd_flag & zip_path & " " & file_name & " 2>&1"


else -- z

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

Can't encrypt .zip files in Finder - revisited

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.