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