| 001 | #!/usr/bin/env osascript |
| 002 | #coding: utf-8 |
| 003 | ----+----1----+----2----+-----3----+----4----+----5----+----6----+----7 |
| 004 | (* |
| 005 |
|
| 006 | cpdfが未インストールの場合インストールします |
| 007 | 困る場合は実行しないでください |
| 008 | cpdfインストール先は |
| 009 | /Users/ユーザーID/Library/Application Support/bin/cpdf |
| 010 |
|
| 011 |
|
| 012 | v1 初回作成 |
| 013 | v1.1 インストール機能をつけた |
| 014 |
|
| 015 |
|
| 016 | マニュアル |
| 017 | https://www.coherentpdf.com/cpdfmanual/cpdfmanualch9.html#x12-970009.4 |
| 018 |
|
| 019 |
|
| 020 |
|
| 021 | com.cocolog-nifty.quicktimer.icefloe *) |
| 022 | ----+----1----+----2----+-----3----+----4----+----5----+----6----+----7 |
| 023 | use AppleScript version "2.8" |
| 024 | use framework "Foundation" |
| 025 | use framework "UniformTypeIdentifiers" |
| 026 | use framework "AppKit" |
| 027 | use scripting additions |
| 028 | property refMe : a reference to current application |
| 029 |
|
| 030 |
|
| 031 | ################## |
| 032 | #cpdfバイナリーのパス |
| 033 | set strResponse to doChkInstall("cpdf") |
| 034 | if strResponse is false then |
| 035 | set strBinPath to ("~/Library/Application Support/bin/cpdf/cpdf") as text |
| 036 | else |
| 037 | set strBinPath to strResponse as text |
| 038 | end if |
| 039 | set ocidBinPathStr to refMe's NSString's stringWithString:(strBinPath) |
| 040 | set ocidBinPath to ocidBinPathStr's stringByStandardizingPath() |
| 041 | set ocidBinFilePathURL to refMe's NSURL's alloc()'s initFileURLWithPath:(ocidBinPath) isDirectory:(false) |
| 042 | #メタ文字 |
| 043 | set strLF to ("" & linefeed & "") as text |
| 044 | set strCR to ("" & return & "") as text |
| 045 | set strCRLF to ("" & return & linefeed & "") as text |
| 046 | set strSpace to ("" & space & "") as text |
| 047 | set strTab to ("" & tab & "") as text |
| 048 |
|
| 049 | ################## |
| 050 | #入力ファイル |
| 051 | set appFileManager to refMe's NSFileManager's defaultManager() |
| 052 | set ocidURLsArray to (appFileManager's URLsForDirectory:(refMe's NSDesktopDirectory) inDomains:(refMe's NSUserDomainMask)) |
| 053 | set ocidDesktopDirPathURL to ocidURLsArray's firstObject() |
| 054 | set aliasDesktopDirPath to (ocidDesktopDirPathURL's absoluteURL()) as alias |
| 055 | #ダイアログを全面に |
| 056 | set strName to (name of current application) as text |
| 057 | if strName is "osascript" then |
| 058 | tell application "Finder" to activate |
| 059 | else |
| 060 | tell current application to activate |
| 061 | end if |
| 062 | set listUTI to {"com.adobe.pdf"} as list |
| 063 | set strMes to ("PDFファイルを選んでください") as text |
| 064 | set strPrompt to ("PDFファイルを選んでください") as text |
| 065 | try |
| 066 | ### ファイル選択時 |
| 067 | tell application "SystemUIServer" |
| 068 | activate |
| 069 | set aliasFilePath to (choose file strMes with prompt strPrompt default location (aliasDesktopDirPath) of type listUTI with invisibles and showing package contents without multiple selections allowed) as alias |
| 070 | end tell |
| 071 | on error strErrMsg number numErrNo |
| 072 | log "No: " & numErrNo & linefeed & "Error:" & strErrMsg |
| 073 | return false |
| 074 | end try |
| 075 | ################## |
| 076 | #入力ファイルパス |
| 077 | set strFilePath to (POSIX path of aliasFilePath) as text |
| 078 | set ocidFilePathStr to refMe's NSString's stringWithString:(strFilePath) |
| 079 | set ocidFilePath to ocidFilePathStr's stringByStandardizingPath() |
| 080 | set ocidFilePathURL to (refMe's NSURL's alloc()'s initFileURLWithPath:(ocidFilePath) isDirectory:false) |
| 081 | ################## |
| 082 | #出力ファイル |
| 083 | set ocidFileName to ocidFilePathURL's lastPathComponent() |
| 084 | set strBaseFileName to (ocidFileName's stringByDeletingPathExtension()) as text |
| 085 | set strDefaultFileName to (strBaseFileName & "-左右分割済.pdf") as text |
| 086 | set strExtensionName to (ocidFilePathURL's pathExtension()) as text |
| 087 | set ocidContainerDirPathURL to ocidFilePathURL's URLByDeletingLastPathComponent() |
| 088 | set aliasContainerDirPath to (ocidContainerDirPathURL's absoluteURL()) as alias |
| 089 |
|
| 090 | set strPromptText to "名前を決めてください" as text |
| 091 | set strMesText to "名前を決めてください" as text |
| 092 | ###ファイル名 ダイアログ |
| 093 | tell application "SystemUIServer" |
| 094 | activate |
| 095 | set aliasFilePath to (choose file name strMesText default location aliasContainerDirPath default name strDefaultFileName with prompt strPromptText) as «class furl» |
| 096 | end tell |
| 097 | ################## |
| 098 | #インストールチェック |
| 099 | set boolExist to appFileManager's fileExistsAtPath:(ocidBinPath) isDirectory:(false) |
| 100 | if boolExist is false then |
| 101 | set boolDone to doInstall() |
| 102 | if boolDone is false then |
| 103 | log "インストールに失敗しました" |
| 104 | return false |
| 105 | end if |
| 106 | end if |
| 107 |
|
| 108 | ################## |
| 109 | #出力パス |
| 110 | set strSaveFilePath to (POSIX path of aliasFilePath) as text |
| 111 | set ocidSaveFilePathStr to refMe's NSString's stringWithString:(strSaveFilePath) |
| 112 | set ocidSaveFilePath to ocidSaveFilePathStr's stringByStandardizingPath() |
| 113 | set ocidSaveFilePathURL to (refMe's NSURL's alloc()'s initFileURLWithPath:(ocidSaveFilePath) isDirectory:false) |
| 114 | set ocidContainerDirPathURL to ocidFilePathURL's URLByDeletingLastPathComponent() |
| 115 | ###拡張子 |
| 116 | set strExtension to (ocidFilePathURL's pathExtension()) as text |
| 117 | ###最後のアイテムがファイル名 |
| 118 | set strFileName to (ocidFilePathURL's lastPathComponent()) as text |
| 119 | ###拡張子のつけ忘れ対策 |
| 120 | if strFileName does not contain strExtensionName then |
| 121 | set strFileName to (strFileName & "." & strExtensionName) as text |
| 122 | set ocidFilePathURL to ocidContainerDirPathURL's URLByAppendingPathComponent:(strFileName) |
| 123 | end if |
| 124 | ################## |
| 125 | #コマンド整形 |
| 126 | set strSaveFilePath to (ocidSaveFilePathURL's |path|()) as text |
| 127 | set strFilePath to (ocidFilePathURL's |path|()) as text |
| 128 | set strBinPath to (ocidBinFilePathURL's |path|()) as text |
| 129 | ################## |
| 130 | #まずはページの回転を0とする |
| 131 | set strCmd to ("'" & strBinPath & "' -upright '" & strFilePath & "' -o '" & strSaveFilePath & "'") as text |
| 132 | log strCmd |
| 133 | do shell script strCmd |
| 134 |
|
| 135 |
|
| 136 | ################## |
| 137 | #1ページ目のページの回転を取得 |
| 138 | set strCmd to ("\"" & strBinPath & "\" -page-info -json -utf8 \"" & strSaveFilePath & "\" 1 ") as text |
| 139 | log strCmd |
| 140 | try |
| 141 | set strJsonResponse to (do shell script strCmd) as text |
| 142 | on error |
| 143 | return "コマンドでエラーになりました" |
| 144 | end try |
| 145 | #戻り値のJSONから1ページ目の回転値を取得 |
| 146 | set ocidJsonStr to refMe's NSString's stringWithString:(strJsonResponse) |
| 147 | set ocidStringData to ocidJsonStr's dataUsingEncoding:(refMe's NSUTF8StringEncoding) |
| 148 | set ocidOption to (refMe's NSJSONReadingMutableContainers) |
| 149 | set listJSONSerialization to (refMe's NSJSONSerialization's JSONObjectWithData:(ocidStringData) options:(ocidOption) |error|:(reference)) |
| 150 | set ocidPlistDict to (item 1 of listJSONSerialization)'s firstObject() |
| 151 | set numPageRotation to (ocidPlistDict's valueForKey:("Rotation")) as integer |
| 152 |
|
| 153 | #ページの回転に合わせたコマンド整形 |
| 154 | if numPageRotation = 0 then |
| 155 | set strCmd to ("\"" & strBinPath & "\" -chop \"2 1\" \"" & strSaveFilePath & "\" -o \"" & strSaveFilePath & "\"") as text |
| 156 | else if numPageRotation = 90 then |
| 157 | set strCmd to ("\"" & strBinPath & "\" -chop \"1 2\" -chop-btt \"" & strSaveFilePath & "\" -o \"" & strSaveFilePath & "\"") as text |
| 158 | else if numPageRotation = 270 then |
| 159 | set strCmd to ("\"" & strBinPath & "\" -chop \"1 2\" \"" & strSaveFilePath & "\" -o \"" & strSaveFilePath & "\"") as text |
| 160 | else if numPageRotation = 180 then |
| 161 | set strCmd to ("\"" & strBinPath & "\" -chop \"2 1\" -chop-rtl \"" & strSaveFilePath & "\" -o \"" & strSaveFilePath & "\"") as text |
| 162 | end if |
| 163 | log strCmd |
| 164 | try |
| 165 | do shell script strCmd |
| 166 | on error strErrMsg number numErrNo |
| 167 | log "No: " & numErrNo & strLF & "Error:" & strErrMsg |
| 168 | return false |
| 169 | end try |
| 170 |
|
| 171 | set strCmd to ("'" & strBinPath & "' -upright '" & strSaveFilePath & "' -o '" & strSaveFilePath & "'") as text |
| 172 | log strCmd |
| 173 | do shell script strCmd |
| 174 |
|
| 175 | return 0 |
| 176 |
|
| 177 |
|
| 178 | ############################## |
| 179 | #すでにインストール済みで |
| 180 | #ユーザーがRC設定している場合は |
| 181 | #パスが通るのでこれがエラーにならない |
| 182 | to doChkInstall(argBinName) |
| 183 | try |
| 184 | set strCmd to ("/bin/zsh -c 'where " & argBinName & "'") |
| 185 | set strResponse to (do shell script strCmd) as text |
| 186 | return strResponse |
| 187 | on error strErrMsg number numErrNo |
| 188 | return false |
| 189 | end try |
| 190 | end doChkInstall |
| 191 |
|
| 192 | ############################## |
| 193 | to doQuitSelf() |
| 194 | try |
| 195 | tell application "System Events" to quit |
| 196 | end try |
| 197 | set strOutputString to (missing value) |
| 198 | set listAliasFilePath to (missing value) |
| 199 | set listBundleID to {"com.apple.automator.xpc.runner", "com.apple.automator.xpc.workflowServiceRunner"} as list |
| 200 | repeat with itemBundleID in listBundleID |
| 201 | set ocidAppArray to (refMe's NSRunningApplication's runningApplicationsWithBundleIdentifier:(itemBundleID)) |
| 202 | repeat with itemApp in ocidAppArray |
| 203 | try |
| 204 | set boolDone to itemApp's terminate() |
| 205 | end try |
| 206 | end repeat |
| 207 | end repeat |
| 208 | return true |
| 209 | end doQuitSelf |
| 210 |
|
| 211 | ############################## |
| 212 | #インストールサプ |
| 213 | to doInstall() |
| 214 | ############################## |
| 215 | #設定項目 |
| 216 | #ダウンロードURL |
| 217 | set strURL to ("https://codeload.github.com/coherentgraphics/cpdf-binaries/zip/refs/heads/master") as text |
| 218 | #インストール先 |
| 219 | set strSaveDir to ("~/Library/Application Support/bin/cpdf") as text |
| 220 | ############################## |
| 221 | #メタ文字 |
| 222 | set strLF to ("" & linefeed & "") as text |
| 223 | set strCR to ("" & return & "") as text |
| 224 | set strCRLF to ("" & return & linefeed & "") as text |
| 225 | set strSpace to ("" & space & "") as text |
| 226 | set strTab to ("" & tab & "") as text |
| 227 | ############################## |
| 228 | #ダウンロード先(起動時に削除される項目) |
| 229 | set appFileManager to refMe's NSFileManager's defaultManager() |
| 230 | set ocidTempDirURL to appFileManager's temporaryDirectory() |
| 231 | set ocidUUID to refMe's NSUUID's alloc()'s init() |
| 232 | set ocidUUIDString to ocidUUID's UUIDString |
| 233 | set ocidDownloadDirPathURL to ocidTempDirURL's URLByAppendingPathComponent:(ocidUUIDString) isDirectory:true |
| 234 | set ocidAttrDict to refMe's NSMutableDictionary's alloc()'s initWithCapacity:0 |
| 235 | ocidAttrDict's setValue:(511) forKey:(refMe's NSFilePosixPermissions) |
| 236 | set listDone to appFileManager's createDirectoryAtURL:(ocidDownloadDirPathURL) withIntermediateDirectories:true attributes:(ocidAttrDict) |error|:(reference) |
| 237 | set ocidDownloadFilePathURL to ocidDownloadDirPathURL's URLByAppendingPathComponent:("master.zip") isDirectory:(false) |
| 238 | set strDownloadFilePath to (ocidDownloadFilePathURL's |path|()) as text |
| 239 | set strExpandDirPath to (ocidDownloadDirPathURL's |path|()) as text |
| 240 | |
| 241 | ############################## |
| 242 | #保存先 最終的なインストール先 |
| 243 | set ocidSaveDirPathStr to refMe's NSString's stringWithString:(strSaveDir) |
| 244 | set ocidSaveDirPath to ocidSaveDirPathStr's stringByStandardizingPath() |
| 245 | set ocidSaveDirPathURL to (refMe's NSURL's alloc()'s initFileURLWithPath:(ocidSaveDirPath) isDirectory:false) |
| 246 | |
| 247 | ############################## |
| 248 | #今あるものをゴミ箱へ |
| 249 | set boolDirExists to appFileManager's fileExistsAtPath:(ocidSaveDirPath) isDirectory:(true) |
| 250 | if (boolDirExists as boolean) is true then |
| 251 | set appFileManager to refMe's NSFileManager's defaultManager() |
| 252 | set listDone to (appFileManager's trashItemAtURL:(ocidSaveDirPathURL) resultingItemURL:(ocidDownloadDirPathURL) |error|:(reference)) |
| 253 | end if |
| 254 | |
| 255 | ############################## |
| 256 | #新たにフォルダを作り直す |
| 257 | ocidAttrDict's setValue:(448) forKey:(refMe's NSFilePosixPermissions) |
| 258 | set listDone to appFileManager's createDirectoryAtURL:(ocidSaveDirPathURL) withIntermediateDirectories:true attributes:(ocidAttrDict) |error|:(reference) |
| 259 | |
| 260 | ############################## |
| 261 | #リダイレクト先 |
| 262 | set strCmd to ("/usr/bin/curl -s -L -I -o /dev/null -w '%{url_effective}' \"" & strURL & "\"") as text |
| 263 | try |
| 264 | set strRedirectURL to (do shell script strCmd) as text |
| 265 | on error strErrMsg number numErrNo |
| 266 | log "No: " & numErrNo & linefeed & "Error:" & strErrMsg |
| 267 | return false |
| 268 | end try |
| 269 | |
| 270 | ############################## |
| 271 | #ダウンロード |
| 272 | set strCmd to ("/usr/bin/curl -L -o \"" & strDownloadFilePath & "\" \"" & strRedirectURL & "\" --connect-timeout 20") as text |
| 273 | try |
| 274 | set strResponse to (do shell script strCmd) as text |
| 275 | on error strErrMsg number numErrNo |
| 276 | log "No: " & numErrNo & linefeed & "Error:" & strErrMsg |
| 277 | return false |
| 278 | end try |
| 279 | |
| 280 | ############################## |
| 281 | #解凍 |
| 282 | set strCmd to ("/usr/bin/bsdtar -xzf \"" & strDownloadFilePath & "\" -C \"" & strExpandDirPath & "\"") as text |
| 283 | try |
| 284 | set strResponse to (do shell script strCmd) as text |
| 285 | on error strErrMsg number numErrNo |
| 286 | log "No: " & numErrNo & linefeed & "Error:" & strErrMsg |
| 287 | return false |
| 288 | end try |
| 289 | |
| 290 | ############################## |
| 291 | #解凍結果 |
| 292 | set reocdSystemInfot to (system info) as record |
| 293 | set strCPU to (CPU type of reocdSystemInfot) as text |
| 294 | #CPUで分岐 |
| 295 | if strCPU contains "ARM" then |
| 296 | #ARM |
| 297 | set ocidExpandBinFilePathURL to ocidDownloadDirPathURL's URLByAppendingPathComponent:("cpdf-binaries-master/OSX-ARM/cpdf") isDirectory:(false) |
| 298 | |
| 299 | else |
| 300 | #INTEL |
| 301 | set ocidExpandBinFilePathURL to ocidDownloadDirPathURL's URLByAppendingPathComponent:("cpdf-binaries-master/OSX-Intel/cpdf") isDirectory:(false) |
| 302 | |
| 303 | end if |
| 304 | |
| 305 | #最終的なバイナリーの移動先 |
| 306 | set ocidDistBinFilePathURL to ocidSaveDirPathURL's URLByAppendingPathComponent:("cpdf") isDirectory:(false) |
| 307 | #移動 |
| 308 | set listDone to (appFileManager's moveItemAtURL:(ocidExpandBinFilePathURL) toURL:(ocidDistBinFilePathURL) |error|:(reference)) |
| 309 | |
| 310 | ############################## |
| 311 | #解凍前のファイルをゴミ箱へ |
| 312 | set appFileManager to refMe's NSFileManager's defaultManager() |
| 313 | set listDone to (appFileManager's trashItemAtURL:(ocidDownloadDirPathURL) resultingItemURL:(ocidDownloadDirPathURL) |error|:(reference)) |
| 314 | |
| 315 | return true |
| 316 | |
| 317 | end doInstall |