Macos – Bypass a licence agreement when mounting a DMG on the command line

dmg-imagehdiutilmacos

I'm automating my Mac installation using puppet. As a part of it I need to install several programs that come in a .dmg format.

I use the following to mount them:

sudo /usr/bin/hdiutil mount -plist -nobrowse -readonly -quiet -mountrandom /tmp Program.dmg

The problem is that some .dmg files come with a license attached, and so script is stuck accepting the license. (There is no stdin/out when running with puppet, so I can't manually approve it to continue.)

Is there a way to pre-approve or force-approve the license?

Best Answer

If you have a GUI and are able to perform two command-line calls in parallel, you can use

/System/Library/CoreServices/DiskImageMounter.app/Contents/MacOS/DiskImageMounter /path/to/file.dmg

and

osascript accept.scpt

the latter of which executes the following AppleScript:

tell application "System Events"
    delay 5 # wait 5 seconds -- I tested it using two terminal tabs and needed the time
    key code 48 # press tab 4 times in the license window
    key code 48
    key code 48
    key code 48
    keystroke " " # press space to click "accept"
end tell

In bash, I'm able to write

/System/Library/CoreServices/DiskImageMounter.app/Contents/MacOS/DiskImageMounter file.dmg & osascript accept.scpt