Last active
January 2, 2019 11:16
-
-
Save pudquick/b5ab0281198d9c324394 to your computer and use it in GitHub Desktop.
Example AppleScriptObjC / Cocoa-Applescript function for parsing hdiutil mount plist output via ObjC calls
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
script AppDelegate | |
property parent: class "NSObject" | |
-- Syntax sugar | |
property myApp: current application | |
property None: missing value -- Be careful not to bind this as a IBOutlet! | |
-- Classes | |
property NSData: myApp's class "NSData" | |
property NSString: myApp's class "NSString" | |
property NSMutableDictionary: myApp's class "NSMutableDictionary" | |
property NSPropertyListSerialization: myApp's class "NSPropertyListSerialization" | |
-- Constants | |
property NSUTF8StringEncoding: myApp's NSUTF8StringEncoding | |
property NSPropertyListImmutable: myApp's NSPropertyListImmutable | |
-- IBOutlets | |
property theWindow : missing value | |
on applicationWillFinishLaunching_(aNotification) | |
-- Insert code here to initialize your application before any files are opened | |
end applicationWillFinishLaunching_ | |
on applicationShouldTerminate_(sender) | |
-- Insert code here to do any housekeeping before your application quits | |
return current application's NSTerminateNow | |
end applicationShouldTerminate_ | |
on exampleMounter_(sender) | |
-- capture some example hdiutil output | |
try | |
set mountOutputPlist to (do shell script "/usr/bin/hdiutil attach /Users/mike/Desktop/kodi-14.1-Helix-x86_64.dmg -plist") | |
on error | |
log ("ERROR: Unable to mount") | |
return | |
end try | |
-- convert the string into a NSString | |
set theString to NSString's stringWithString_(mountOutputPlist) | |
-- convert the NSString into NSData | |
set theData to theString's dataUsingEncoding_(NSUTF8StringEncoding) | |
-- Parse the NSData as a plist | |
set thePlist to NSPropertyListSerialization's propertyListFromData_mutabilityOption_format_errorDescription_(theData, NSPropertyListImmutable, None, None) | |
-- The returned plist is actually either a NSDictionary or NSArray, depending on the plist root object | |
-- In this case, the output of hdiutil is a dict with a single key we care about (which contains an array) | |
set theEntities to thePlist's objectForKey_("system-entities") | |
-- There can be (and usually are) multiple entities. We're looking for the one that contains a "mount-point" key | |
-- Start with a default value of None | |
set mountPoint to None | |
-- Loop through each entry, looking for the key's value | |
repeat with anItem in theEntities | |
set mountPoint to anItem's objectForKey_("mount-point") | |
-- If we found a value, exit with it | |
if (mountPoint is not equal to None) then exit repeat | |
-- Guess we didn't find one, let's check the next ... | |
end repeat | |
if (mountPoint is not equal to None) then | |
log (mountPoint) | |
log ("Success - found mount point") | |
else | |
log ("ERROR: Unknown failure - no mount point listed") | |
end if | |
end exampleMounter_ | |
end script |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment