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