20260226

[AppleScript]ディスクイメージのアンマウント


【スクリプトエディタで開く】 |

ディスクイメージをアンマウント.scpt
ソース
001#!/usr/bin/env osascript
002#coding: utf-8
003----+----1----+----2----+-----3----+----4----+----5----+----6----+----7--
004(*
005ディスクイメージ dmgファイルを
006マウントしている場合で アンマウントします
007
008
009com.cocolog-nifty.quicktimer.icefloe *)
010----+----1----+----2----+-----3----+----4----+----5----+----6----+----7--
011use AppleScript version "2.8"
012use framework "Foundation"
013use framework "AppKit"
014use scripting additions
015
016property refMe : a reference to current application
017
018########################
019#
020#   on run {listAliasFilePath}
021
022
023on run
024   set appBundle to current application's NSBundle's bundleWithIdentifier:("com.apple.osax.standardadditions")
025   set strChooseAFile to (appBundle's localizedStringForKey:("Choose a File") value:("Choose a File") table:("Localizable")) as text
026   set appBundle to current application's NSBundle's bundleWithIdentifier:("com.apple.AppKit")
027   set strMultiple to (appBundle's localizedStringForKey:("NSMultipleDocuments") value:("NSMultipleDocuments") table:("AccessibilityImageDescriptions")) as text
028   set strMsg to ("XXXXXX " & strMultiple & return & strChooseAFile & "") as text
029   set strPrompt to ("XXXXXX " & strMultiple & return & strChooseAFile & "") as text
030   set aliasDesktopDirPath to (path to desktop from user domain) as alias
031   set listUTI to {"public.item"} as list
032   try
033      tell application "SystemUIServer"
034         activate
035         set listAliasFilePath to (choose folder strMsg with prompt strPrompt default location aliasDesktopDirPath with invisibles and multiple selections allowed without showing package contents) as list
036      end tell
037   on error strErrMes number numErrNo
038      log strErrMes & numErrNo
039      return false
040   end try
041   if listAliasFilePath is {} then
042      return false
043   end if
044   
045   
046   set boolDone to (open listAliasFilePath)
047   return boolDone
048end run
049
050
051on open listAliasFilePath
052   
053   set listFileName to {} as list
054   
055   repeat with itemAliasFilePath in listAliasFilePath
056      #パス
057      set aliasItemPath to itemAliasFilePath as alias
058      set recordInfoFor to (info for aliasItemPath) as record
059      set strItemPath to (POSIX path of aliasItemPath) as text
060      set ocidItemPathStr to (refMe's NSString's stringWithString:(strItemPath))
061      set ocidItemPath to ocidItemPathStr's stringByStandardizingPath()
062      set ocidItemPathURL to (refMe's NSURL's fileURLWithPath:(ocidItemPath))
063      #
064      set ocidKeys to refMe's NSMutableArray's alloc()'s init()
065      (ocidKeys's addObject:(refMe's NSURLVolumeIsRemovableKey))
066      (ocidKeys's addObject:(refMe's NSURLVolumeIsEjectableKey))
067      (ocidKeys's addObject:(refMe's NSURLVolumeIsInternalKey))
068      (ocidKeys's addObject:(refMe's NSURLVolumeIsLocalKey))
069      (ocidKeys's addObject:(refMe's NSURLVolumeNameKey))
070      (ocidKeys's addObject:(refMe's NSURLVolumeIdentifierKey))
071      (ocidKeys's addObject:(refMe's NSURLVolumeURLKey))
072      (ocidKeys's addObject:(refMe's NSURLIsVolumeKey))
073      (ocidKeys's addObject:(refMe's NSURLPathKey))
074      set listResponse to (ocidItemPathURL's resourceValuesForKeys:(ocidKeys) |error|:(reference))
075      set ocidKeysValuesDict to (first item of listResponse)
076      set boolIsVol to (ocidKeysValuesDict's objectForKey:("NSURLIsVolumeKey")) as boolean
077      if boolIsVol is true then
078         set strCmd to ("/usr/sbin/diskutil info -plist \"" & ocidItemPath & "\"") as text
079         set strStdOut to (do shell script strCmd) as text
080         set ocidStdOut to (refMe's NSString's stringWithString:(strStdOut))
081         set ocidStdOutData to (ocidStdOut's dataUsingEncoding:(refMe's NSUTF8StringEncoding))
082         set ocidFromat to (refMe's NSPropertyListXMLFormat_v1_0)
083         set ocidOption to (refMe's NSPropertyListMutableContainersAndLeaves)
084         set listResponse to (refMe's NSPropertyListSerialization's propertyListWithData:(ocidStdOutData) options:(ocidOption) format:(ocidFromat) |error|:(reference))
085         set ocidPlistDict to (first item of listResponse)
086         set ocidEntry to (ocidPlistDict's objectForKey:("IORegistryEntryName"))
087         set boolIsEq to (ocidEntry's isEqualToString:("disk image"))
088         if boolIsEq is true then
089            set ocidProtocol to (ocidPlistDict's objectForKey:("BusProtocol"))
090            set boolIsEq to (ocidProtocol's isEqualToString:("Disk Image"))
091            if boolIsEq is true then
092               #アンマウントする
093               set appNSWorkspace to refMe's NSWorkspace's sharedWorkspace()
094               set listDone to (appNSWorkspace's unmountAndEjectDeviceAtURL:(ocidItemPathURL) |error|:(reference))
095               if (last item of listDone) ≠ (missing value) then
096                  log (item 2 of listDone)'s localizedDescription() as text
097                  log (item 2 of listDone)'s localizedRecoverySuggestion() as text
098               end if
099            end if
100         end if
101      end if
102   end repeat
103   
104   
105   return true
106end open
107
108
109
AppleScriptで生成しました