Skip to content

Instantly share code, notes, and snippets.

@lordlycastle
Last active September 27, 2024 18:28
Show Gist options
  • Select an option

  • Save lordlycastle/f2d56498f068b0da2443d4215e591800 to your computer and use it in GitHub Desktop.

Select an option

Save lordlycastle/f2d56498f068b0da2443d4215e591800 to your computer and use it in GitHub Desktop.
This AppleScript creates a disk image (.dmg) with the given files/folders user provides.
set fileType to display dialog "Create disk image of a " buttons {"Folder", "File", "Cancel"} ¬
default button ¬
"Folder" cancel button ¬
"Cancel" with title "Create Disk Image"
if button returned of fileType is "Folder" then
set input to choose folder with prompt "Choose folder to make disk image of:"
else if button returned of fileType is "File" then
set input to choose file with prompt "Choose file to make disk image of:"
end if
tell application "System Events"
if kind of input is "Folder" then
set sourceFolder to POSIX path of input
set saveTID to AppleScript's text item delimiters
set AppleScript's text item delimiters to {"/"}
set sourceFolder to text 1 thru text item -1 of sourceFolder
set parentDir to text 1 thru text item -2 of sourceFolder & "/"
set AppleScript's text item delimiters to saveTID
tell application "Finder" to set dmgname to name of (input as alias)
set outputPath to parentDir & dmgname & ".dmg"
set debug to (do shell script "hdiutil create -volname " & quoted form of dmgname & " -srcfolder " & quoted form of sourceFolder & " -format UDZO -ov " & quoted form of outputPath & " -debug")
--Uncomment to write hdiutil's debug to a txt file.
--do shell script "echo " & quoted form of debug & " > ~/Desktop/debug.txt"
display notification "Out path:" & outputPath with title "Disk Image Created"
delay 1
else
set parentDir to POSIX path of (input as alias)
tell application "Finder" to set fullFileName to name of input
set saveTID to AppleScript's text item delimiters
set AppleScript's text item delimiters to {"/"}
set parentDir to text 1 thru text item -2 of parentDir & "/"
set AppleScript's text item delimiters to {"."}
set dmgname to text 1 thru text item -2 of fullFileName
set AppleScript's text item delimiters to saveTID
set outputPath to parentDir & dmgname & ".dmg"
set debug to (do shell script "hdiutil create -volname " & quoted form of dmgname & " -srcfolder " & quoted form of parentDir & fullFileName & " -format UDZO -ov " & quoted form of outputPath & " -debug")
--Uncomment to write hdiutil's debug to a txt file, and add "-debug" option to command above.
--do shell script "echo " & quoted form of debug & " > ~/Desktop/debug.txt"
delay 0.2
display notification "Out path:" & outputPath with title "Disk Image Created"
delay 1
end if
end tell
return outputPath
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment