I’m parsing the text returned from ‘do shell script “hdiutil info”’.
use AppleScript version "2.4"
use scripting additions--needed because I'm using 'do shell script'
repeat with thisDMG in Mounted_DMGs() --Get and repeat through the list of DMG names.
tell application "Finder"
eject disk thisDMG--poof!
end tell
end repeat
on Mounted_DMGs()
try
set dmgData to do shell script "hdiutil info"
--Get a listing of mounted DMG data
set AppleScript's text item delimiters to "================================================" & return
--Header sparator string from hdiutil info
set dmgData to text items 2 thru -1 of dmgData--remove header
set MountedDMGs to {}
repeat with i from 1 to length of dmgData-- loop allows the handler to process multiple mounted DMGs.
set thisDMG to item i of dmgData
-->get the first MDGs hdiutil text in the list above. The only entry if only one DMG is mounted.
set AppleScript's text item delimiters to {"/Volumes/"}
-->set the delimiters to the point I want to break the string. This occurs in the final line of the text.
set thisDMGVolumeName to text item 2 of thisDMG
-->"LogCollectorTool"
set AppleScript's text item delimiters to return
set the end of MountedDMGs to text item 1 of thisDMGVolumeName ----Add the results to a list of DMG names
--using the FIRST text item here, while the delimiters are set to return is so that,
--in the case there are more than one reported DMGs I have to remove a trailing
--return on all but the final text block. When the text item delimiters do not occur
--in the string that you are getting text items of the input string is returned unchanged.
--So this line will remove returns from the string when they are present and not
--affect the text there it isn''t present.
end repeat
return MountedDMGs --> Return the list of DMG names.
on error
return {}
end try
end Mounted_DMGs
Which looks something like this.
framework : 665.80.2
driver : 665.80.2
images : 2
================================================
image-path : /Users/username/Downloads/Adobe LogCollectorTool.dmg
image-alias : /Users/username/Downloads/Adobe LogCollectorTool.dmg
shadow-path : <none>
icon-path : <none>
image-type : read-only disk image
system-image : false
blockcount : 80696
blocksize : 512
writeable : false
autodiskmount : TRUE
removable : TRUE
image-encrypted : false
mounting user : username
mounting mode : <unknown>
process ID : 15525
framework name : DiskImages2
/dev/disk5 Apple_partition_scheme
/dev/disk5s1 Apple_partition_map
/dev/disk5s2 Apple_HFS /Volumes/LogCollectorTool
When you set applescript’s text items to some string and then get the text items of some other string it ‘breaks’ the string at the occurrences of the text item delimiters, returning a list of the text NOT containing the text item delimiters.
Below I’m breaking the text returned from the shell script at the end of the header separator text.
Your use of the final two characters will behave exactly the same as long as there are no other occurrences of “=”&return in the text. I just included the full 48 character string to ensure I didn’t have any issues with unexpected occurrences.
This code should return only the DMG data listings below the header.
set AppleScript's text item delimiters to "================================================" & return
set dmgData to text items 2 thru -1 of dmgData
returns:
{"image-path : /Users/username/Downloads/Adobe LogCollectorTool.dmg
image-alias : /Users/username/Downloads/Adobe LogCollectorTool.dmg
shadow-path : <none>
icon-path : <none>
image-type : read-only disk image
system-image : false
blockcount : 80696
blocksize : 512
writeable : false
autodiskmount : TRUE
removable : TRUE
image-encrypted : false
mounting user : username
mounting mode : <unknown>
process ID : 15640
framework name : DiskImages2
/dev/disk5 Apple_partition_scheme
/dev/disk5s1 Apple_partition_map
/dev/disk5s2 Apple_HFS /Volumes/LogCollectorTool"}
The repeat loop allows the handler to process multiple mounted DMGs.
Inside the repeat loop I’m breaking the text at the string “/Volumes/” which occurs just before the name of the mounted DMG.
repeat with i from 1 to length of dmgData
set thisDMG to item i of dmgData
-->get the first MDGs hdiutil text in the list above. The only entry if only one DMG is mounted.
set AppleScript's text item delimiters to {"/Volumes/"}
-->set the delimiters to the point I want to break the string. This occurs in the final line of the text.
set thisDMGVolumeName to text item 2 of thisDMG
-->"LogCollectorTool"
set AppleScript's text item delimiters to return
--This is so that, in the case there are more than one reported DMGs I have to remove a trailing return on all but the final text block. When the text item delimiters do not occur in the string that you are getting text items of the input string is returned unchanged. So this line will remove returns from the string when they are present and not affect the tex there it isn''t present.
set the end of MountedDMGs to text item 1 of thisDMGVolumeName
-->"LogCollectorTool"
end repeat
return MountedDMGs--> Return the list of DMG names.
Then the outer repeat loop repeats with the values returned from the Mounted_DMGs() handler telling the finder to eject them.
repeat with thisDMG in Mounted_DMGs()
tell application "Finder"
eject disk thisDMG
end tell
end repeat
I should add a disclaimer that this type of text parsing is inherently fraught with potential for failure.