20260407

【PKG】Appleインストールパッケージの解凍


【PKG】Appleインストールパッケージの解凍

ダウンロード
NOTE記事一覧ですnote.com

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

PKG2Extracter.scpt
ソース
001#!/usr/bin/env osascript
002#coding: utf-8
003----+----1----+----2----+-----3----+----4----+----5----+----6----+----7--
004(*
005インストールPKGの解凍用スクリプト
006
007
008v1初回作成
009
010
011com.cocolog-nifty.quicktimer.icefloe *)
012----+----1----+----2----+-----3----+----4----+----5----+----6----+----7--
013use AppleScript version "2.8"
014use framework "Foundation"
015use framework "AppKit"
016use framework "UniformTypeIdentifiers"
017use scripting additions
018property refMe : a reference to current application
019property listAliasFilePath : {} as list
020########################
021# RUN
022#on run listAliasFilePath
023
024(*
025   ###テスト用
026      *)
027on run
028   
029   #   if listAliasFilePath is {} then
030   ########################
031   #ダイアログ
032   set strMsg to ("PKG 選択") as text
033   set strPrompt to ("PKG を選択してください" & return & "複数選択可") as text
034   set aliasDesktopDirPath to (path to desktop from user domain) as alias
035   set listUTI to {"com.apple.installer-package-archive"} as list
036   try
037      tell application "SystemUIServer"
038         activate
039         set listAliasFilePath to (choose file strMsg with prompt strPrompt default location aliasDesktopDirPath of type listUTI with invisibles and multiple selections allowed without showing package contents) as list
040      end tell
041   on error strErrMes number numErrNo
042      log strErrMes & numErrNo
043      return false
044   end try
045   if listAliasFilePath is {} then
046      return false
047   end if
048   #   end if
049   
050   
051   set boolDone to (open listAliasFilePath)
052   return boolDone
053end run
054########################
055# OPEN
056on open listAliasFilePath
057   #対象のファイルのみ次工程に回す
058   repeat with itemAliasFilePath in listAliasFilePath
059      set aliasItemFilePath to itemAliasFilePath as alias
060      set recordInfoFor to (info for aliasItemFilePath) as record
061      set strUTI to (type identifier of recordInfoFor) as text
062      set strMatchUTI to ("com.apple.installer-package-archive") as text
063      if strUTI is strUTI then
064         #保存先
065         set ocidSaveDirPathURL to doChkVolume(aliasItemFilePath)
066         #コマンド実行
067         set boolDone to doExecCmd(aliasItemFilePath, ocidSaveDirPathURL)
068      end if
069      if boolDone is true then
070         set boolDone to doFullExpand(ocidSaveDirPathURL)
071         #   end if
072         #   if boolDone is true then
073         set boolDone to doCleanup(ocidSaveDirPathURL)
074      end if
075      
076   end repeat
077   
078   return boolDone
079end open
080
081
082#########################
083#
084to doFullExpand(ocidSaveDirPathURL)
085   set appFileManager to refMe's NSFileManager's defaultManager()
086   set ocidProperties to refMe's NSMutableArray's alloc()'s init()
087   (ocidProperties's addObject:(refMe's NSURLPathKey))
088   (ocidProperties's addObject:(refMe's NSURLNameKey))
089   set ocidOption to (refMe's NSDirectoryEnumerationSkipsHiddenFiles)
090   set listResult to appFileManager's contentsOfDirectoryAtURL:(ocidSaveDirPathURL) includingPropertiesForKeys:(ocidProperties) options:(ocidOption) |error|:(reference)
091   set ocidFilePathURLArray to (first item of listResult)
092   #内包されているファイル
093   repeat with itemFilePathURL in ocidFilePathURLArray
094      #拡張子PKGだけ実施
095      set strExtension to itemFilePathURL's pathExtension() as text
096      if strExtension is "pkg" then
097         #拡張子をとったパス
098         set ocidMakeSubDirPathURL to itemFilePathURL's URLByDeletingPathExtension()
099         set appFileManager to refMe's NSFileManager's defaultManager()
100         set listDone to (appFileManager's moveItemAtURL:(itemFilePathURL) toURL:(ocidMakeSubDirPathURL) |error|:(reference))
101      end if
102   end repeat
103   
104   return listDone
105end doFullExpand
106
107########################
108# クリーニング
109to doCleanup(ocidSaveDirPathURL)
110   set appFileManager to refMe's NSFileManager's defaultManager()
111   set ocidProperties to refMe's NSMutableArray's alloc()'s init()
112   (ocidProperties's addObject:(refMe's NSURLPathKey))
113   (ocidProperties's addObject:(refMe's NSURLNameKey))
114   set ocidOption to (refMe's NSDirectoryEnumerationSkipsHiddenFiles)
115   set ocidEmuDict to (appFileManager's enumeratorAtURL:(ocidSaveDirPathURL) includingPropertiesForKeys:(ocidProperties) options:(ocidOption) errorHandler:(reference))
116   #
117   repeat
118      set ocidEnuURL to ocidEmuDict's nextObject()
119      if ocidEnuURL = (missing value) then
120         exit repeat
121      end if
122      set ocidFileName to ocidEnuURL's lastPathComponent()
123      set boolIsEq to (ocidFileName's isEqualToString:("Bom"))
124      if boolIsEq is true then
125         set ocidBomFilePathURL to ocidEnuURL's URLByAppendingPathExtension:("list.txt")
126         set ocidBomFilePath to ocidBomFilePathURL's |path|()
127         set ocidBomFilePath to doPathEscape(ocidBomFilePath)
128         set ocidEnuPath to ocidEnuURL's |path|()
129         set ocidEnuPath to doPathEscape(ocidEnuPath)
130         #コマンド整形して実行
131         set strCmd to ("/usr/bin/lsbom \\\"" & ocidEnuPath & "\\\" | /usr/bin/tee \\\"" & ocidBomFilePath & "\\\"") as text
132         set boolDone to doExecZsh(strCmd)
133      end if
134      set boolIsEq to (ocidFileName's isEqualToString:("PackageInfo"))
135      if boolIsEq is true then
136         set ocidXMLFilePathURL to ocidEnuURL's URLByAppendingPathExtension:("xml")
137         set listDone to (appFileManager's moveItemAtURL:(ocidEnuURL) toURL:(ocidXMLFilePathURL) |error|:(reference))
138      end if
139   end repeat
140   return (first item of listDone)
141end doCleanup
142
143
144########################
145# PKGがDMG上かディスク上か
146to doChkVolume(aliasItemFilePath)
147   #パス
148   set aliasFilePath to aliasItemFilePath as alias
149   set strFilePath to (POSIX path of aliasFilePath) as text
150   set ocidFilePathStr to refMe's NSString's stringWithString:(strFilePath)
151   set ocidFilePath to ocidFilePathStr's stringByStandardizingPath()
152   set ocidFilePathURL to refMe's NSURL's fileURLWithPath:(ocidFilePath) isDirectory:(false)
153   #   
154   set ocidKey to (refMe's NSURLVolumeURLKey)
155   set listResponse to (ocidFilePathURL's getResourceValue:(reference) forKey:(ocidKey) |error|:(reference))
156   set ocidVolumePathURL to (second item of listResponse)
157   #
158   set ocidKey to (refMe's NSURLVolumeIsInternalKey)
159   set listResponse to (ocidVolumePathURL's getResourceValue:(reference) forKey:(ocidKey) |error|:(reference))
160   set boolInternal to (second item of listResponse)
161   ####次工程に回す
162   if boolInternal = (missing value) then
163      set ocidSaveDirPathURL to doMakeSaveDirDesktop(ocidFilePathURL)
164   else if (boolInternal as boolean) is true then
165      set ocidSaveDirPathURL to doMakeSaveDirSamePath(ocidFilePathURL)
166   end if
167   return ocidSaveDirPathURL
168end doChkVolume
169########################
170# デスクトップに解凍先
171to doMakeSaveDirDesktop(ocidFilePathURL)
172   #デスクトップ
173   set appFileManager to refMe's NSFileManager's defaultManager()
174   set ocidURLsArray to (appFileManager's URLsForDirectory:(refMe's NSDesktopDirectory) inDomains:(refMe's NSUserDomainMask))
175   set ocidDesktopDirPathURL to ocidURLsArray's firstObject()
176   #
177   set ocidFileName to ocidFilePathURL's lastPathComponent()
178   set ocidFileName to ocidFileName's decomposedStringWithCanonicalMapping()
179   set ocidBaseFileName to ocidFileName's stringByDeletingPathExtension()
180   set strSaveDirName to ("" & ocidBaseFileName & "_解凍済")
181   set ocidSaveDirPathURL to ocidDesktopDirPathURL's URLByAppendingPathComponent:(strSaveDirName) isDirectory:(true)
182   #
183   return ocidSaveDirPathURL
184end doMakeSaveDirDesktop
185
186########################
187# 同じ階層に解凍先
188to doMakeSaveDirSamePath(ocidFilePathURL)
189   #
190   set ocidFileName to ocidFilePathURL's lastPathComponent()
191   set ocidFileName to ocidFileName's decomposedStringWithCanonicalMapping()
192   set ocidBaseFileName to ocidFileName's stringByDeletingPathExtension()
193   set ocidContainerDirPathURL to ocidFilePathURL's URLByDeletingLastPathComponent()
194   set strSaveDirName to ("" & ocidBaseFileName & "_解凍済")
195   set ocidSaveDirPathURL to ocidContainerDirPathURL's URLByAppendingPathComponent:(strSaveDirName) isDirectory:(true)
196   return ocidSaveDirPathURL
197end doMakeSaveDirSamePath
198########################
199#コマンド整形
200to doExecCmd(aliasItemFilePath, ocidSaveDirPathURL)
201   #エイリアス確定させてからのUNIXパス
202   set aliasFilePath to aliasItemFilePath as alias
203   set strFilePath to (POSIX path of aliasFilePath) as text
204   set ocidFilePath to doPathEscape(strFilePath)
205   #
206   set ocidSaveDirPath to ocidSaveDirPathURL's |path|()
207   #コマンド整形して実行
208   set strCmd to ("/usr/sbin/pkgutil  --expand-full  \\\"" & ocidFilePath & "\\\" \\\"" & ocidSaveDirPath & "\\\"") as text
209   set boolDone to doExecZsh(strCmd)
210   return boolDone
211   
212end doExecCmd
213
214########################
215#パスのエスケープ
216to doPathEscape(strFilePath)
217   set ocidFilePathStr to refMe's NSString's stringWithString:(strFilePath)
218   set ocidFilePath to ocidFilePathStr's stringByStandardizingPath()
219   set ocidFilePathURL to refMe's NSURL's fileURLWithPath:(ocidFilePath) isDirectory:(false)
220   set listEscChar to {"\\", "\"", "$", "`"} as list
221   repeat with itemEscChar in listEscChar
222      set ocidFilePath to (ocidFilePath's stringByReplacingOccurrencesOfString:(itemEscChar) withString:("\\\\\\" & itemEscChar & ""))
223   end repeat
224   set ocidFilePath to (ocidFilePath's stringByReplacingOccurrencesOfString:("!") withString:("\\\"'!'\\\""))
225   
226   return ocidFilePath
227end doPathEscape
228
229########################
230#コマンド実行
231to doExecZsh(argCmdText)
232   set strCmdText to argCmdText as text
233   set strExecZsh to ("/bin/zsh -c \"" & strCmdText & "\"") as text
234   
235   try
236      log "コマンド開始"
237      set strStdOut to (do shell script strExecZsh) as text
238      log "コマンド終了"
239      
240   on error strErroMsg number numErrorNo
241      return false
242   end try
243   return true
244end doExecZsh
AppleScriptで生成しました