Create an encrypted DMG of a folder

Hello,

I am not at all confortable with scripts and command line and I would like to create an encrypted DMG of a folder.
I am able to do it with the script below, however I am not able to encrypt I get always the following error:
→ error “hdiutil: create failed - Appel ioctl incompatible avec le périphérique”
Also, I would like to not specify a size, but have the size of the folder content and also to be able to add files later (-format UDRW).

Help would be appreciated.

Thanks and regards
Henri

set sourceFolder to POSIX path of (choose folder with prompt "choose source folder")
set sourceFolderName to name of (info for sourceFolder)
set destinationFolder to POSIX path of (choose folder with prompt "choose destination folder")
set sourceFolder2 to POSIX path of sourceFolder


set dmgFileNamePath to (sourceFolderName & ".dmg")
set myNewPath to destinationFolder & dmgFileNamePath
try
	do shell script "hdiutil create -encryption AES-128 -size 500m -format UDRW -nospotlight -srcfolder " & sourceFolder2 & " -volname " & quoted form of sourceFolderName & " " & myNewPath
end try

Hi. I don’t see a “nospotlight” option in the hdiutil man page.
This works for me:


set isTarget to (choose folder) as text
do shell script "hdiutil create -encryption -format UDRW -srcfolder" & space & ((isTarget)'s POSIX path's quoted form) & space & (isTarget's text 1 thru -2 & ".dmg")'s POSIX path's quoted form

Hi Marc,

Thank you for the reply, I still get an error (see the response window result below):

tell application “Script Editor”
choose folder
→ alias “Macintosh HD:Users:hsp:Documents:sidenote:”
end tell
tell current application
do shell script “hdiutil create -encryption -format UDRW -srcfolder ‘/Users/hsp/Documents/sidenote/’ ‘/Users/hsp/Documents/sidenote.dmg’”
→ error “hdiutil: create failed - Appel ioctl incompatible avec le périphérique” number 1
Résultat :
error “hdiutil: create failed - Appel ioctl incompatible avec le périphérique” number 1

Concerning the -nospotlight, when you type into the terminal the following:
hdiutil create -help

you can see this:

Image from Folder options:
-srcfolder
-[no]spotlight do (not) create a Spotlightâ„¢ index

Thanks and regards
Henri

An ioctl, or input/output control, has something to do with the kernel; the error that its “incompatible with the peripheral” is not particularly helpful, and, unfortunately, is too arcane for me. Perhaps it will help others to help you, if you post your OS version. I tested my code in OS 10.9.5.

Hi Marc and thanks for your help.

I am under 10.11.3

Regards
Henri

I don’t know if the OP is still looking for an answer at this point, however, after installing Sierra and revisiting this question, the error appears related to a permissions issue.

set sourceFolder to (choose folder with prompt "select a folder to store") as text
set destination to (choose folder with prompt "define an end point") as text
set fileVar to "nom" --the dmg's filename


#iConcerning volname, not specifying same results in volname = sourceFolder
do shell script "printf 'whatever password' | hdiutil create -srcfolder " & sourceFolder's POSIX path's quoted form & " -encryption -stdinpass -fs HFS+" & space & (destination & fileVar)'s POSIX path's quoted form

--if volname desired
--set volumeVar to "expanded" --the mounted dmg's name
--do shell script "printf 'whatever password' | hdiutil create -srcfolder " & sourceFolder's POSIX path's quoted form & " -encryption -stdinpass -fs HFS+ -volname " & volumeVar's quoted form & space & (destination & fileVar)'s POSIX path's quoted form