20260523

[AppleScript]mediainfoインストールへルパ

[AppleScript]mediainfoインストールへルパ

NOTE記事一覧ですnote.com

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

mediaInfoインストール.scpt.scpt
ソース
001#!/usr/bin/env osascript
002#coding: utf-8
003----+----1----+----2----+----3----+----4----+----5----+----6----+----7--
004(*
005
006デスクトップアプリケーションは有料AppStore
007https://apps.apple.com/jp/app/mediainfo/id510620098
008
009
010mediaInfoのバイナリーインストール
011インストール先は
012/Users/ユーザーID/Library/Application Support/bin/mediaInfo
013
014少し特殊ですので
015不安な方は実行しないでください
016
017com.cocolog-nifty.quicktimer.icefloe *)
018----+----1----+----2----+----3----+----4----+----5----+----6----+----7--
019use AppleScript version "2.8"
020use framework "Foundation"
021use framework "AppKit"
022use framework "UniformTypeIdentifiers"
023use scripting additions
024
025property refMe : a reference to current application
026
027##############################
028#インストール先
029set strBinDirPath to ("~/Library/Application Support/bin/mediaInfo") as text
030
031##############################
032#インストールチェック
033set appFileManager to refMe's NSFileManager's defaultManager()
034set strDefaultPath to ("/usr/local/bin/mediaInfo") as text
035set ocidDefaultPathStr to refMe's NSString's stringWithString:(strDefaultPath)
036set ocidDefaultPath to ocidDefaultPathStr's stringByStandardizingPath()
037set boolExists to appFileManager's fileExistsAtPath:(ocidDefaultPath) isDirectory:(false)
038if boolExists is true then
039   log "管理者権限でインストール済みです:処理を終了します"
040   return false
041end if
042
043##############################
044#インデックスから最新版のURLを取得
045set strIndexURL to ("https://mediaarea.net/download/binary/mediainfo/") as text
046set ocidIndexURLString to refMe's NSString's stringWithString:(strIndexURL)
047set ocidIndexURL to refMe's NSURL's alloc()'s initWithString:(ocidIndexURLString)
048#NSDATA
049set ocidOption to (refMe's NSDataReadingMappedIfSafe)
050set listResponse to refMe's NSData's alloc()'s initWithContentsOfURL:(ocidIndexURL) options:(ocidOption) |error|:(reference)
051if (item 2 of listResponse) = (missing value) then
052   log "initWithContentsOfURL 正常処理"
053   set ocidReadData to (item 1 of listResponse)
054else if (item 2 of listResponse) ≠ (missing value) then
055   set strErrorNO to (item 2 of listResponse)'s code() as text
056   set strErrorMes to (item 2 of listResponse)'s localizedDescription() as text
057   refMe's NSLog("■:" & strErrorNO & strErrorMes)
058   return "initWithContentsOfURL エラーしました" & strErrorNO & strErrorMes
059end if
060#XML
061set ocidOption to (refMe's NSXMLNodePreserveAll) + (refMe's NSXMLDocumentTidyHTML)
062set listResponse to refMe's NSXMLDocument's alloc()'s initWithData:(ocidReadData) options:(ocidOption) |error|:(reference)
063if (item 2 of listResponse) = (missing value) then
064   log "initWithContentsOfURL 正常処理"
065   set ocidReadXML to (item 1 of listResponse)
066else if (item 2 of listResponse) ≠ (missing value) then
067   set strErrorNO to (item 2 of listResponse)'s code() as text
068   set strErrorMes to (item 2 of listResponse)'s localizedDescription() as text
069   refMe's NSLog("■:" & strErrorNO & strErrorMes)
070   log "initWithData エラーしました" & strErrorNO & strErrorMes
071   set ocidReadXML to (item 1 of listResponse)
072end if
073set ocidRoot to ocidReadXML's rootDocument()
074set listResponse to (ocidRoot's objectsForXQuery:("//td/a/@href") |error|:(reference))
075set ocidModelArray to (first item of listResponse)'s objectAtIndex:(2)
076set ocidVerDir to ocidModelArray's stringValue()
077set ocidVer to (ocidVerDir's stringByReplacingOccurrencesOfString:("/") withString:(""))
078set strSubURL to ("https://mediaarea.net/download/binary/mediainfo/" & ocidVerDir & "MediaInfo_CLI_" & ocidVer & "_Mac.dmg") as text
079
080#
081set ocidURLString to refMe's NSString's stringWithString:(strSubURL)
082set ocidURL to refMe's NSURL's alloc()'s initWithString:(ocidURLString)
083set ocidDMGFileName to ocidURL's lastPathComponent()
084#
085set ocidTempDirURL to appFileManager's temporaryDirectory()
086set ocidUUID to refMe's NSUUID's alloc()'s init()
087set ocidUUIDString to ocidUUID's UUIDString
088set ocidSaveDirPathURL to ocidTempDirURL's URLByAppendingPathComponent:(ocidUUIDString) isDirectory:(true)
089set strSaveDirPath to (ocidSaveDirPathURL's |path|) as text
090set ocidAttrDict to refMe's NSMutableDictionary's alloc()'s init()
091ocidAttrDict's setValue:(511) forKey:(refMe's NSFilePosixPermissions)
092set listDone to appFileManager's createDirectoryAtURL:(ocidSaveDirPathURL) withIntermediateDirectories:true attributes:(ocidAttrDict) |error|:(reference)
093set ocidDMGFilePathURL to ocidTempDirURL's URLByAppendingPathComponent:(ocidDMGFileName) isDirectory:(true)
094set ocidDMGFilePath to ocidDMGFilePathURL's |path|()
095#NSDATA
096set ocidOption to (refMe's NSDataReadingMappedIfSafe)
097set listResponse to refMe's NSData's alloc()'s initWithContentsOfURL:(ocidURL) options:(ocidOption) |error|:(reference)
098set ocidDMGData to (item 1 of listResponse)
099set ocidOption to (refMe's NSDataWritingAtomic)
100set listDone to ocidDMGData's writeToURL:(ocidDMGFilePathURL) options:(ocidOption) |error|:(reference)
101#マウントポイントを作成
102set ocidMountPointDirPathURL to ocidSaveDirPathURL's URLByAppendingPathComponent:("MountPoint/DmgMount") isDirectory:(true)
103set listBoolMakeDir to appFileManager's createDirectoryAtURL:(ocidMountPointDirPathURL) withIntermediateDirectories:(true) attributes:(ocidAttrDict) |error|:(reference)
104set ocidMountPointDirPath to ocidMountPointDirPathURL's |path|()
105# マウント
106set strCmd to ("/usr/bin/hdiutil attach \"" & ocidDMGFilePath & "\" -noverify -nobrowse -noautoopen -mountpoint \"" & ocidMountPointDirPath & "\"") as text
107do shell script strCmd
108#マウントされたディスクからPKGのパスを取得
109set ocidOption to (refMe's NSDirectoryEnumerationSkipsSubdirectoryDescendants)
110set ocidPropertiesForKeysArray to refMe's NSMutableArray's alloc()'s init()
111ocidPropertiesForKeysArray's addObject:(refMe's NSURLNameKey)
112ocidPropertiesForKeysArray's addObject:(refMe's NSURLPathKey)
113set listDone to appFileManager's contentsOfDirectoryAtURL:(ocidMountPointDirPathURL) includingPropertiesForKeys:(ocidPropertiesForKeysArray) options:(ocidOption) |error|:(reference)
114set ocidContentsURLArray to (item 1 of listDone)
115repeat with itemArray in ocidContentsURLArray
116   #
117   set strExtension to itemArray's pathExtension() as text
118   if strExtension is "pkg" then
119      set ocidPkgFilePathURL to itemArray
120      exit repeat
121   end if
122end repeat
123#PKGを解凍
124set coidPkgFilePathURL to ocidPkgFilePathURL's |path|()
125set ocidExpandDirPathURL to ocidSaveDirPathURL's URLByAppendingPathComponent:("Expand") isDirectory:(true)
126set ocidExpandDirPath to ocidExpandDirPathURL's |path|()
127set strCmd to ("/usr/sbin/pkgutil --expand \"" & coidPkgFilePathURL & "\" \"" & ocidExpandDirPath & "\"") as text
128do shell script strCmd
129#ディスクをアンマウント
130set strCmd to ("/usr/bin/hdiutil detach \"" & ocidMountPointDirPath & "\" -force") as text
131do shell script strCmd
132#Payloadを解凍
133set ocidPayloadFilePathURL to ocidExpandDirPathURL's URLByAppendingPathComponent:("Payload") isDirectory:(true)
134set ocidPayloadFilePath to ocidPayloadFilePathURL's |path|()
135set ocidExtractDirPathURL to ocidSaveDirPathURL's URLByAppendingPathComponent:("Extract") isDirectory:(true)
136set ocidExtractDirPath to ocidExtractDirPathURL's |path|()
137set strCmd to ("/usr/bin/ditto -xz \"" & ocidPayloadFilePath & "\" \"" & ocidExtractDirPath & "\"") as text
138do shell script strCmd
139set ocidOriginalPathURL to ocidExtractDirPathURL's URLByAppendingPathComponent:("usr/local/bin/mediainfo") isDirectory:(false)
140set ocidOriginalPath to ocidOriginalPathURL's |path|()
141
142##############################
143#古いファイルをゴミ箱へ入れて
144set ocidBinDirPathStr to refMe's NSString's stringWithString:(strBinDirPath)
145set ocidBinDirPath to ocidBinDirPathStr's stringByStandardizingPath()
146set ocidBinDirPath to ocidBinDirPath's stringByExpandingTildeInPath()
147set ocidBinDirPathURL to refMe's NSURL's fileURLWithPath:(ocidBinDirPath) isDirectory:(true)
148set appFileManager to refMe's NSFileManager's defaultManager()
149set listDone to (appFileManager's trashItemAtURL:(ocidBinDirPathURL) resultingItemURL:(ocidBinDirPathURL) |error|:(reference))
150set ocidBinFilePathURL to ocidBinDirPathURL's URLByAppendingPathComponent:("mediainfo") isDirectory:(false)
151set ocidBinFilePath to ocidBinFilePathURL's |path|()
152
153##############################
154#フォルダ作り直して
155set ocidAttrDict to refMe's NSMutableDictionary's alloc()'s init()
156ocidAttrDict's setValue:(448) forKey:(refMe's NSFilePosixPermissions)
157set listDone to appFileManager's createDirectoryAtURL:(ocidBinDirPathURL) withIntermediateDirectories:(true) attributes:(ocidAttrDict) |error|:(reference)
158
159##############################
160#解凍したファイルを移動して
161
162set appFileManager to refMe's NSFileManager's defaultManager()
163set listDone to (appFileManager's moveItemAtPath:(ocidOriginalPath) toPath:(ocidBinFilePath) |error|:(reference))
164
165##############################
166#拡張属性Extended Attributes (EA)を削除して
167try
168   set theComandText to ("/usr/bin/xattr -cr  \"" & ocidBinDirPath & "\"") as text
169   do shell script theComandText
170on error
171   return false
172end try
173
174##############################
175#確認のためにインストール先を開く
176
177set appSharedWorkspace to refMe's NSWorkspace's sharedWorkspace()
178set boolDone to appSharedWorkspace's openURL:(ocidBinDirPathURL)
179set appFinderArray to refMe's NSRunningApplication's runningApplicationsWithBundleIdentifier:("com.apple.finder")
180set appFinder to appFinderArray's firstObject()
181set boolDone to appFinder's activateWithOptions:(refMe's NSApplicationActivateIgnoringOtherApps)
182
183
184return boolDone
AppleScriptで生成しました