How can I make a password protected ZIP file from a selection in the Finder app

I would like to use "Quick Actions" within the Finder app from a selection of files and/or folders to make a password protected ZIP file.


This is not a direct solution, however it can be used as an alternate solution because it will create a disk image (.dmg) file of all selected Finder files and or folders, whether the selection is a single item or multiple items.

global myPassword

property destinationFolder : (path to documents folder)
property theName : missing value
property isTrue : missing value

set theName to "New Encrypted Disk Image"

run setPassword
delay 0.1
activate
set deleteOriginals to button returned of (display dialog "Would You Like To Delete The Original Files After The Disk Image Has Been Created?" buttons {"Yes", "No"} default button 1)
delay 0.1
activate
set theName to (display dialog ¬
    "Would You Like To Name Your New Disk Image?" default answer ¬
    "New Encrypted Disk Image" buttons {"No", "Re-Name"} default button 2 ¬
    with title "Name Disk Image?")
delay 0.1

if button returned of theName is "Re-Name" then
    set theName to text returned of theName
else
    set theName to "New Encrypted Disk Image"
end if

tell application "Finder"
    if exists of alias ((destinationFolder as text) & theName) then ¬
        delete alias ((destinationFolder as text) & theName)
    set tempFolder to (make new folder at destinationFolder ¬
        with properties {name:theName}) as alias
    set theFiles to selection as alias list
    duplicate theFiles to tempFolder
    set theContainer to POSIX path of (container of tempFolder as alias)
    set filesWithTheName to count of (items of (container of tempFolder as alias) ¬
        whose name contains theName and name extension is "dmg")
    if filesWithTheName is greater than 0 then ¬
        set theName to theName & filesWithTheName + 1
end tell

set theDMG to do shell script "printf  \"" & myPassword & "\" |  hdiutil create -encryption AES-256 -stdinpass -srcfolder '" & POSIX path of tempFolder & "' '" & theContainer & theName & "'"

tell application "Finder" to delete tempFolder

set theOffset to offset of "/" in theDMG
set theDMG to POSIX file (text theOffset thru -1 of theDMG) as alias

tell application "Finder" to set newName to name of theDMG

if deleteOriginals is "Yes" then
    tell application "Finder" to delete theFiles
end if

tell application "Finder"
    activate
    reveal theDMG
end tell

script failedPassVerify
    activate
    display dialog ¬
        "You Have Unsuccessfully Verified Your Password 3 Times In A Row... Please Try Again Later" buttons {"OK"} default button "OK" with title ¬
        "UNSUCCESSFUL PASSWORD VERIFICATION" with icon 0 giving up after 10
    quit me
end script

script setPassword
    set theCount to 0
    repeat until isTrue = true
        activate
        set myPassword to text returned of (display dialog ¬
            ("ENTER THE PASSWORD TO ENCRYPT DISK IMAGE: " & theName) ¬
                default answer "" with hidden answer)
        activate
        set myPassword2 to text returned of (display dialog ¬
            "PLEASE VERIFY YOUR PASSWORD" default answer "" with hidden answer)
        set isTrue to myPassword2 = myPassword
        if isTrue = false then
            set theCount to theCount + 1
            if theCount = 3 then run my failedPassVerify
            activate
            display alert "PASSWORDS DO NOT MATCH" message ¬
                "PASSWORDS DO NOT MATCH" giving up after 3
        end if
    end repeat
    set theCount to 0
    set isTrue to missing value
end script
  1. Open Automator
  2. Make a new Quick Action
  3. Make sure it receives 'files or folders' from Finder.app
  4. Select Run Apple Script and copy/paste in the code
  5. Save it as "New Encrypted Disk Image"