3

By default Automator uses the 128 bit encryption for image creation. I do not want to open disk utility all the time to create encrypted images. Thus, I though about using hdiutil within Automator.

How do I define the source folder path in my workflow? How can I create image with the highest encryption using Automator?

Automator Workflow

2 Answers 2

4

You don't need Automator to do this. Just use Automators cousin, AppleScript.

  1. Open Script Editor in /Applications/Utilities
  2. Copy and Paste the code below
  3. Go to File>Export
  4. File Format: Application
  5. Drag files and folders onto the icon (you can even add the icon to the doc for easy access
  6. Enter your password
  7. Files will be created in the same directory with an added _EncryptedDMG.dmg

Code:

on open myFiles
    set theCount to 1
    display dialog "Enter the password to encrypt" default answer "" with hidden answer
    set myPassword to the text returned of the result as text
    set d to "•"
    set td to ""
    repeat length of myPassword times
        set td to td & d
    end repeat
    display dialog "Verify the password: " & td default answer "" with hidden answer
    set theVerify to the text returned of the result
    if myPassword is theVerify then
        tell application "System Events"
            repeat with myFile in myFiles
                set myPath to the POSIX path of myFile
                set myName to the characters 1 thru ((offset of "." in (name of myFile as text)) - 1) of (name of myFile as text)
                set myContainer to (the POSIX path of (container of myFile))
                do shell script "printf  \"" & myPassword & "\" |  hdiutil create -encryption AES-256 -stdinpass -srcfolder '" & myPath & "' '" & myContainer & "/" & myName & "'"
                do shell script "mv '" & myPath & "' ~/.Trash"
                display notification "File created at " & myContainer & "/" & myName & ".dmg. Original file deleted." with title "Encryption Complete" subtitle "File " & theCount & " of " & (length of myFiles) sound name "glass"
            end repeat
        end tell
    else
        display dialog "Error: Passwords did not match"
    end if
end open

If you would like to add to context menu copy and paste the following code in to a "Run Apple Script" Action.

on run {myFiles, parameters}
    set theCount to 1
    display dialog "Enter the password to encrypt" default answer "" with hidden answer
    set myPassword to the text returned of the result as text
    set d to "•"
    set td to ""
    repeat length of myPassword times
        set td to td & d
    end repeat
    display dialog "Verify the password: " & td default answer "" with hidden answer
    set theVerify to the text returned of the result
    if myPassword is theVerify then
        tell application "System Events"
            repeat with myFile in myFiles
                set myPath to the POSIX path of myFile
                set myName to the characters 1 thru ((offset of "." in (name of myFile as text)) - 1) of (name of myFile as text)
                set myContainer to (the POSIX path of (container of myFile))
                do shell script "printf  \"" & myPassword & "\" |  hdiutil create -encryption AES-256 -stdinpass -srcfolder '" & myPath & "' '" & myContainer & "/" & myName & "'"
                do shell script "mv '" & myPath & "' ~/.Trash"
                display notification "File created at " & myContainer & "/" & myName & ".dmg. Original file deleted." with title "Encryption Complete" subtitle "File " & theCount & " of " & (length of myFiles) sound name "glass"
            end repeat
        end tell
    else
        display dialog "Error: Passwords did not match"
    end if
end run

When opening Automator select Context Menu and then select these settings

enter image description here

Note: You will be happy to hear, macOS Mojave will bring similar functionality natively. There is a add password in context menu that zips and encrypts the file similar to how this application does it, but still there is not progress bar.

Note[2]: This script will not worked if the folder contains locked files (File>Info>Locked).

23
  • 1
    @howdytom Showing progress in AppleScript and Automator are both possible, but impossible when using so she’ll script. It’s just a limit of both. For the the contextual menu I will edit answer soon.
    – JBis
    Jun 11, 2018 at 10:42
  • 1
    Can’t test right now but you should be able to add just below to “do shell script” the following: “tell application "Finder" to move (myFile) to trash”. If this doesn’t work I’ll add in a couple hours when I can edit.
    – JBis
    Jun 11, 2018 at 11:28
  • 1
    @howdytom Thanks. I will probably not edit the actual answer but just add as comment (others may want script without it so it may be easier this way).
    – JBis
    Jun 11, 2018 at 11:35
  • 1
    @howdytom Updates have been added with some bonus features. Tell me if you need anything else.
    – JBis
    Jun 11, 2018 at 20:28
  • 1
    @howdytom Glad I could help :) Should work will multiple file selection too.
    – JBis
    Jun 11, 2018 at 20:53
2

I felt there were enough differences between my solution and the other posted answer, as to offer this script as another answer to the original post.

Save this script as an application in ScriptEditor app

This script allows the user to drop multiple files or folders, directly onto this application's icon... And also gives the user of the option whether to keep or delete the original files or folders.

If this script application is run in the normal fashion by double-clicking on the application, the user is given the option whether to choose single or multiple files or to choose single or multiple folders, to be processed. In addition, the user is given the option to delete or keep the original files.

This has been tested using the latest version of macOS High Sierra

UPDATE: This Code Has Been Edited To Deal With Files Or Folders That Are Locked

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

global deleteFiles, isTrue, theCount, myPassword, theName

on open theFiles
    --HANDLE THE CASE WHERE THE SCRIPT IS LAUNCHED BY DROPPING FILES ONTO APP ICON
    repeat with i from 1 to count of theFiles
        set thisItem to item i of theFiles
        tell application "Finder"
            if locked of (get properties of thisItem) then
                set locked of thisItem to false
            end if
        end tell
        set isTrue to missing value
        set theCount to 0
        set theName to missing value
        set theFolder to thisItem
        tell application "Finder"
            set theContainer to container of theFolder as alias
            set theName to name of (get properties of theFolder)
        end tell
        run my setPassword
        run my keepOriginals
        set myPath to POSIX path of theFolder
        set theContainer to POSIX path of theContainer
        do shell script "printf  \"" & myPassword & "\" |  hdiutil create -encryption AES-256 -stdinpass -srcfolder '" & ¬
            myPath & "' '" & theContainer & theName & "_Encrypted'"
        if deleteFiles = true then
            tell application "Finder" to delete theFolder
        end if
    end repeat
end open

on run
    --HANDLE THE CASE WHERE THE SCRIPT IS LAUNCHED  DROPPED FILES
    activate
    set theChoice to display dialog ¬
        "WOULD YOU LIKE TO CHOOSE FILES OR FOLDERS?" buttons {"Cancel", "Choose Files", "Choose Folders"} ¬
        default button ¬
        "Choose Folders" cancel button ¬
        "Cancel" with title ¬
        "Make Your Choice" with icon 1 ¬
        giving up after 20
    if button returned of theChoice is "Choose Files" then
        run my chooseFiles
    else if button returned of theChoice is "Choose Folders" then
        run my chooseFolders
    else if button returned of theChoice is "" then
        return
    end if
end run

script chooseFiles
    activate
    set theFiles to (choose file with multiple selections allowed)
    repeat with i from 1 to count of theFiles
        set thisItem to item i of theFiles
        tell application "Finder"
            if locked of (get properties of thisItem) then
                set locked of thisItem to false
            end if
        end tell
        set isTrue to missing value
        set theCount to 0
        set theName to missing value
        set theFolder to thisItem
        tell application "Finder"
            set theContainer to container of theFolder as alias
            set theName to name of (get properties of theFolder)
        end tell
        run my setPassword
        run my keepOriginals
        set myPath to POSIX path of theFolder
        set theContainer to POSIX path of theContainer
        do shell script "printf  \"" & myPassword & "\" |  hdiutil create -encryption AES-256 -stdinpass -srcfolder '" & ¬
            myPath & "' '" & theContainer & theName & "_Encrypted'"
        if deleteFiles = true then
            tell application "Finder" to delete theFolder
        end if
    end repeat
end script

script chooseFolders
    activate
    set theFiles to (choose folder with multiple selections allowed)
    repeat with i from 1 to count of theFiles
        set thisItem to item i of theFiles
        try
            tell application "Finder"
                set locked of every item of entire contents of thisItem to false
            end tell
        end try
        set isTrue to missing value
        set theCount to 0
        set theName to missing value
        set theFolder to thisItem
        tell application "Finder"
            set theContainer to container of theFolder as alias
            set theName to name of (get properties of theFolder)
        end tell
        run my setPassword
        run my keepOriginals
        set myPath to POSIX path of theFolder
        set theContainer to POSIX path of theContainer
        do shell script "printf  \"" & myPassword & "\" |  hdiutil create -encryption AES-256 -stdinpass -srcfolder '" & ¬
            myPath & "' '" & theContainer & theName & "_Encrypted'"
        if deleteFiles = true then
            tell application "Finder" to delete theFolder
        end if
    end repeat
end script

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 keepOriginals
    set keepOrDelete to display dialog ¬
        ("Would You Like To Delete The Original Item... " & theName & "?") buttons {"DELETE ORIGINALS", "KEEP ORIGINALS"} ¬
        default button 2 ¬
        with title ¬
        "KEEP OR DELETE ORIGINALS?" with icon 0 ¬
        giving up after 30
    if button returned of keepOrDelete is "DELETE ORIGINALS" then
        set deleteFiles to true
    else if button returned of keepOrDelete is "KEEP ORIGINALS" then
        set deleteFiles to false
    else if button returned of keepOrDelete is "" then
        set deleteFiles to false
    end if
end script

script setPassword
    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
            end if
            activate
            display alert ¬
                "PASSWORDS DO NOT MATCH" message ¬
                "PASSWORDS DO NOT MATCH" giving up after 3
        end if
    end repeat
end script
9
  • 1
    My answer has the option for a droplet. But I do like the ability to drop and open! Very cool idea.
    – JBis
    Jun 13, 2018 at 5:59
  • 1
    Thanks :-) If I really wanted to go overboard I would have set each dialog box to mention the name of the file or folder It will be processing. Your solution was solid... +1 BTW... All of the commands you have wrapped inside of the System Events tell blccks are actually handled by standard additions... Using System Events is not necessary
    – wch1zpink
    Jun 13, 2018 at 6:15
  • thanks! Yeah I had a different plan that required it and when I didn’t do that I forgot to delete it. Thanks though.
    – JBis
    Jun 14, 2018 at 3:51
  • Code Has Been Edited To Deal With Files Or Folders That Are Locked
    – wch1zpink
    Jul 11, 2018 at 21:12
  • Fancè (fancy) :)
    – JBis
    Jul 11, 2018 at 22:10

You must log in to answer this question.