| 001 | #! /usr/bin/env osascript |
| 002 | ----+----1----+----2----+-----3----+----4----+----5----+----6----+----7 |
| 003 | (* |
| 004 | cpdf ARM版 |
| 005 | https://github.com/coherentgraphics/cpdf-binaries |
| 006 | バイナリーのインストール |
| 007 | サイズも小さいので、入れ替えインストールします |
| 008 | (古いバージョンはゴミ箱へ) |
| 009 |
|
| 010 | インストール先は |
| 011 | /Users/ユーザーID/Library/Application Support/bin/cpdf |
| 012 | と |
| 013 | ちょっと変わっていますのでカスタマイズして利用してください |
| 014 | インストール後にバイナリーを実行してヘルプ表示します |
| 015 |
|
| 016 | cpdf は署名されていますので安心して利用できます |
| 017 |
|
| 018 | v1 初回作成 |
| 019 | v1.1 入れ替え処理を入れた |
| 020 |
|
| 021 |
|
| 022 | com.cocolog-nifty.quicktimer.icefloe *) |
| 023 | ----+----1----+----2----+-----3----+----4----+----5----+----6----+----7 |
| 024 | use AppleScript version "2.8" |
| 025 | use framework "Foundation" |
| 026 | use framework "UniformTypeIdentifiers" |
| 027 | use framework "AppKit" |
| 028 | use scripting additions |
| 029 |
|
| 030 | property refMe : a reference to current application |
| 031 | ############################## |
| 032 | #設定項目 |
| 033 | #ダウンロードURL |
| 034 | set strURL to ("https://codeload.github.com/coherentgraphics/cpdf-binaries/zip/refs/heads/master") as text |
| 035 | #インストール先 |
| 036 | set strSaveDir to ("~/Library/Application Support/bin/cpdf") as text |
| 037 |
|
| 038 | ############################## |
| 039 | #メタ文字 |
| 040 | set strLF to ("" & linefeed & "") as text |
| 041 | set strCR to ("" & return & "") as text |
| 042 | set strCRLF to ("" & return & linefeed & "") as text |
| 043 | set strSpace to ("" & space & "") as text |
| 044 | set strTab to ("" & tab & "") as text |
| 045 |
|
| 046 | ############################## |
| 047 | #ダウンロード先(起動時に削除される項目) |
| 048 | set appFileManager to refMe's NSFileManager's defaultManager() |
| 049 | set ocidTempDirURL to appFileManager's temporaryDirectory() |
| 050 | set ocidUUID to refMe's NSUUID's alloc()'s init() |
| 051 | set ocidUUIDString to ocidUUID's UUIDString |
| 052 | set ocidDownloadDirPathURL to ocidTempDirURL's URLByAppendingPathComponent:(ocidUUIDString) isDirectory:true |
| 053 | set ocidAttrDict to refMe's NSMutableDictionary's alloc()'s initWithCapacity:0 |
| 054 | ocidAttrDict's setValue:(511) forKey:(refMe's NSFilePosixPermissions) |
| 055 | set listDone to appFileManager's createDirectoryAtURL:(ocidDownloadDirPathURL) withIntermediateDirectories:true attributes:(ocidAttrDict) |error|:(reference) |
| 056 | if (second item of listDone) ≠ (missing value) then |
| 057 | set strErrorNO to (second item of listDone)'s code() as text |
| 058 | set strErrorMes to (second item of listDone)'s localizedDescription() as text |
| 059 | refMe's NSLog("■:" & strErrorNO & strErrorMes) |
| 060 | return "エラーしました" & strErrorNO & strErrorMes |
| 061 | end if |
| 062 | set ocidDownloadFilePathURL to ocidDownloadDirPathURL's URLByAppendingPathComponent:("master.zip") isDirectory:(false) |
| 063 | set strDownloadFilePath to (ocidDownloadFilePathURL's |path|()) as text |
| 064 | set strExpandDirPath to (ocidDownloadDirPathURL's |path|()) as text |
| 065 |
|
| 066 |
|
| 067 | ############################## |
| 068 | #保存先 |
| 069 | set ocidSaveDirPathStr to refMe's NSString's stringWithString:(strSaveDir) |
| 070 | set ocidSaveDirPath to ocidSaveDirPathStr's stringByStandardizingPath() |
| 071 | set ocidSaveDirPathURL to (refMe's NSURL's alloc()'s initFileURLWithPath:(ocidSaveDirPath) isDirectory:false) |
| 072 | ############################## |
| 073 | #今あるものをゴミ箱へ |
| 074 | set boolDirExists to appFileManager's fileExistsAtPath:(ocidSaveDirPath) isDirectory:(true) |
| 075 | if (boolDirExists as boolean) is true then |
| 076 | set appFileManager to refMe's NSFileManager's defaultManager() |
| 077 | set listDone to (appFileManager's trashItemAtURL:(ocidSaveDirPathURL) resultingItemURL:(ocidDownloadDirPathURL) |error|:(reference)) |
| 078 | if (second item of listDone) ≠ (missing value) then |
| 079 | set strErrorNO to (second item of listDone)'s code() as text |
| 080 | set strErrorMes to (second item of listDone)'s localizedDescription() as text |
| 081 | refMe's NSLog("■:" & strErrorNO & strErrorMes) |
| 082 | return "エラーしました" & strErrorNO & strErrorMes |
| 083 | end if |
| 084 | end if |
| 085 |
|
| 086 |
|
| 087 | ############################## |
| 088 | #新たにフォルダを作り直す |
| 089 | ocidAttrDict's setValue:(448) forKey:(refMe's NSFilePosixPermissions) |
| 090 | set listDone to appFileManager's createDirectoryAtURL:(ocidSaveDirPathURL) withIntermediateDirectories:true attributes:(ocidAttrDict) |error|:(reference) |
| 091 | if (second item of listDone) ≠ (missing value) then |
| 092 | set strErrorNO to (second item of listDone)'s code() as text |
| 093 | set strErrorMes to (second item of listDone)'s localizedDescription() as text |
| 094 | refMe's NSLog("■:" & strErrorNO & strErrorMes) |
| 095 | return "エラーしました" & strErrorNO & strErrorMes |
| 096 | end if |
| 097 |
|
| 098 | ############################## |
| 099 | #リダイレクト先 |
| 100 | set strCommandText to ("/usr/bin/curl -s -L -I -o /dev/null -w '%{url_effective}' \"" & strURL & "\"") as text |
| 101 | try |
| 102 | set strRedirectURL to (do shell script strCommandText) as text |
| 103 | on error |
| 104 | return "URLのリダイレクトの取得に失敗しました" |
| 105 | end try |
| 106 |
|
| 107 | ############################## |
| 108 | #ダウンロード |
| 109 | set strCommandText to ("/usr/bin/curl -L -o \"" & strDownloadFilePath & "\" \"" & strRedirectURL & "\" --connect-timeout 20") as text |
| 110 | try |
| 111 | set strResponse to (do shell script strCommandText) as text |
| 112 | on error |
| 113 | return "ダウンロードに失敗しました" |
| 114 | end try |
| 115 |
|
| 116 | ############################## |
| 117 | #解凍 |
| 118 | set strCommandText to ("/usr/bin/bsdtar -xzf \"" & strDownloadFilePath & "\" -C \"" & strExpandDirPath & "\"") as text |
| 119 | try |
| 120 | set strResponse to (do shell script strCommandText) as text |
| 121 | on error |
| 122 | return "Zipの解凍に失敗しました" |
| 123 | end try |
| 124 |
|
| 125 | ############################## |
| 126 | #移動 |
| 127 | #解凍結果 |
| 128 | set ocidExpandBinFilePathURL to ocidDownloadDirPathURL's URLByAppendingPathComponent:("cpdf-binaries-master/OSX-ARM/cpdf") isDirectory:(false) |
| 129 | #最終的なバイナリーの移動先 |
| 130 | set ocidDistBinFilePathURL to ocidSaveDirPathURL's URLByAppendingPathComponent:("cpdf") isDirectory:(false) |
| 131 | # |
| 132 | set listDone to (appFileManager's moveItemAtURL:(ocidExpandBinFilePathURL) toURL:(ocidDistBinFilePathURL) |error|:(reference)) |
| 133 | if (second item of listDone) ≠ (missing value) then |
| 134 | set strErrorNO to (second item of listDone)'s code() as text |
| 135 | set strErrorMes to (second item of listDone)'s localizedDescription() as text |
| 136 | refMe's NSLog("■:" & strErrorNO & strErrorMes) |
| 137 | ##### |
| 138 | set listDone to (appFileManager's trashItemAtURL:(ocidDistBinFilePathURL) resultingItemURL:(ocidDistBinFilePathURL) |error|:(reference)) |
| 139 | if (item 1 of listDone) is true then |
| 140 | set listDone to (appFileManager's moveItemAtURL:(ocidExpandBinFilePathURL) toURL:(ocidDistBinFilePathURL) |error|:(reference)) |
| 141 | log "最新版と入れ替えました" |
| 142 | if (item 1 of listDone) is false then |
| 143 | set strErrorNO to (second item of listDone)'s code() as text |
| 144 | set strErrorMes to (second item of listDone)'s localizedDescription() as text |
| 145 | refMe's NSLog("■:" & strErrorNO & strErrorMes) |
| 146 | return "エラーしました" & strErrorNO & strErrorMes |
| 147 | end if |
| 148 | else |
| 149 | return "移動でエラーしました" |
| 150 | end if |
| 151 | |
| 152 | end if |
| 153 |
|
| 154 | ############################## |
| 155 | #実行チェック |
| 156 | set strBinPath to (ocidDistBinFilePathURL's |path|()) as text |
| 157 | tell application "Terminal" |
| 158 | launch |
| 159 | activate |
| 160 | #リターンで改行を送る=新規Window |
| 161 | set strCmd to ("" & strLF & strLF & "") as text |
| 162 | set objWindowID to (do script strCmd) |
| 163 | delay 1 |
| 164 | #コマンド実行 |
| 165 | set strCmd to ("\"" & strBinPath & "\" -help") as text |
| 166 | log strCmd |
| 167 | do script strCmd in objWindowID |
| 168 | end tell |
| 169 |
|
| 170 |
|
| 171 | ############################## |
| 172 | # 保存先を開く |
| 173 | set appSharedWorkspace to refMe's NSWorkspace's sharedWorkspace() |
| 174 | set boolDone to appSharedWorkspace's openURL:(ocidSaveDirPathURL) |
| 175 |
|
| 176 |
|
| 177 | ############################## |
| 178 | #解凍前のファイルをゴミ箱へ |
| 179 | set appFileManager to refMe's NSFileManager's defaultManager() |
| 180 | set listDone to (appFileManager's trashItemAtURL:(ocidDownloadDirPathURL) resultingItemURL:(ocidDownloadDirPathURL) |error|:(reference)) |
| 181 | if (second item of listDone) ≠ (missing value) then |
| 182 | set strErrorNO to (second item of listDone)'s code() as text |
| 183 | set strErrorMes to (second item of listDone)'s localizedDescription() as text |
| 184 | refMe's NSLog("■:" & strErrorNO & strErrorMes) |
| 185 | return "エラーしました" & strErrorNO & strErrorMes |
| 186 | end if |
| 187 |
|
| 188 |
|
| 189 | return true |