
20260325
20260324
20260323
20260320
20260318
[cpdf 2.9] PDFの各ページの回転を現在の天地向きで回転0になるように設定する

[cpdf 2.9] PDFの各ページの回転を現在の天地向きで回転0になるように設定する
https://www.coherentpdf.com| ソース | |
|---|---|
| 001 | #!/usr/bin/env osascript |
| 002 | #coding: utf-8 |
| 003 | ----+----1----+----2----+-----3----+----4----+----5----+----6----+----7 |
| 004 | (* |
| 005 | cpdfが別途必要です |
| 006 | <https://github.com/coherentgraphics/cpdf-binaries> |
| 007 | オプション |
| 008 | Chapter 3 |
| 009 | Pages |
| 010 | https://www.coherentpdf.com/cpdfmanual/cpdfmanualch3.html |
| 011 | PDFの各ページが回転していても |
| 012 | 現在の表示を回転0として天地向きとします |
| 013 | cpdfインストーラー同封版 |
| 014 | cpdfが未インストールの場合インストールします |
| 015 | 困る場合は実行しないでください |
| 016 | cpdfインストール先は |
| 017 | /Users/ユーザーID/Library/Application Support/bin/cpdf |
| 018 | v1 初回作成 |
| 019 | v1.1 インストール機能をつけた |
| 020 | com.cocolog-nifty.quicktimer.icefloe *) |
| 021 | ----+----1----+----2----+-----3----+----4----+----5----+----6----+----7 |
| 022 | use AppleScript version "2.8" |
| 023 | use framework "Foundation" |
| 024 | use framework "UniformTypeIdentifiers" |
| 025 | use framework "AppKit" |
| 026 | use scripting additions |
| 027 | property refMe : a reference to current application |
| 028 | ################## |
| 029 | #cpdfバイナリーのパス |
| 030 | set strBinPath to ("~/Library/Application Support/bin/cpdf/cpdf") as text |
| 031 | set ocidBinPathStr to refMe's NSString's stringWithString:(strBinPath) |
| 032 | set ocidBinPath to ocidBinPathStr's stringByStandardizingPath() |
| 033 | set ocidBinFilePathURL to refMe's NSURL's alloc()'s initFileURLWithPath:(ocidBinPath) isDirectory:(false) |
| 034 | ################## |
| 035 | #インストールチェック |
| 036 | set appFileManager to refMe's NSFileManager's defaultManager() |
| 037 | set boolExist to appFileManager's fileExistsAtPath:(ocidBinPath) isDirectory:(false) |
| 038 | if boolExist is false then |
| 039 | set boolDone to doInstall() |
| 040 | if boolDone is false then |
| 041 | log "インストールに失敗しました" |
| 042 | return false |
| 043 | end if |
| 044 | end if |
| 045 | ################## |
| 046 | #入力ファイル |
| 047 | set ocidURLsArray to (appFileManager's URLsForDirectory:(refMe's NSDesktopDirectory) inDomains:(refMe's NSUserDomainMask)) |
| 048 | set ocidDesktopDirPathURL to ocidURLsArray's firstObject() |
| 049 | set aliasDesktopDirPath to (ocidDesktopDirPathURL's absoluteURL()) as alias |
| 050 | set listUTI to {"com.adobe.pdf"} as list |
| 051 | set strMes to ("PDFファイルを選んでください") as text |
| 052 | set strPrompt to ("" & return & "PDFファイルを選んでください" & return & "現在のPDFページ回転で天地向きにセットします" & return & "") as text |
| 053 | try |
| 054 | tell application "SystemUIServer" |
| 055 | activate |
| 056 | 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 |
| 057 | end tell |
| 058 | on error |
| 059 | log "エラーしました" |
| 060 | return "エラーしました" |
| 061 | end try |
| 062 | ################## |
| 063 | #入力ファイルパス |
| 064 | set strFilePath to (POSIX path of aliasFilePath) as text |
| 065 | set ocidFilePathStr to refMe's NSString's stringWithString:(strFilePath) |
| 066 | set ocidFilePath to ocidFilePathStr's stringByStandardizingPath() |
| 067 | set ocidFilePathURL to (refMe's NSURL's alloc()'s initFileURLWithPath:(ocidFilePath) isDirectory:false) |
| 068 | ################## |
| 069 | #出力ファイル |
| 070 | set ocidFileName to ocidFilePathURL's lastPathComponent() |
| 071 | set strBaseFileName to (ocidFileName's stringByDeletingPathExtension()) as text |
| 072 | set strExtensionName to (ocidFilePathURL's pathExtension()) as text |
| 073 | set ocidContainerDirPathURL to ocidFilePathURL's URLByDeletingLastPathComponent() |
| 074 | set aliasContainerDirPath to (ocidContainerDirPathURL's absoluteURL()) as alias |
| 075 | # |
| 076 | set strDefaultFileName to (strBaseFileName & "-編集済.pdf") as text |
| 077 | # |
| 078 | set strPromptText to ("名前を決めてください") as text |
| 079 | set strMesText to ("" & return & "名前を決めてください" & return & "天地向き修正後のPDFの名前") as text |
| 080 | ###ファイル名 ダイアログ |
| 081 | tell application "SystemUIServer" |
| 082 | activate |
| 083 | set aliasFilePath to (choose file name strMesText default location aliasContainerDirPath default name strDefaultFileName with prompt strPromptText) as «class furl» |
| 084 | end tell |
| 085 | ################## |
| 086 | #出力パス |
| 087 | set strSaveFilePath to (POSIX path of aliasFilePath) as text |
| 088 | set ocidSaveFilePathStr to refMe's NSString's stringWithString:(strSaveFilePath) |
| 089 | set ocidSaveFilePath to ocidSaveFilePathStr's stringByStandardizingPath() |
| 090 | set ocidSaveFilePathURL to (refMe's NSURL's alloc()'s initFileURLWithPath:(ocidSaveFilePath) isDirectory:false) |
| 091 | set ocidContainerDirPathURL to ocidFilePathURL's URLByDeletingLastPathComponent() |
| 092 | ###拡張子 |
| 093 | set strExtension to (ocidFilePathURL's pathExtension()) as text |
| 094 | ###最後のアイテムがファイル名 |
| 095 | set strFileName to (ocidFilePathURL's lastPathComponent()) as text |
| 096 | ###拡張子のつけ忘れ対策 |
| 097 | if strFileName does not contain strExtensionName then |
| 098 | set strFileName to (strFileName & "." & strExtensionName) as text |
| 099 | set ocidFilePathURL to ocidContainerDirPathURL's URLByAppendingPathComponent:(strFileName) |
| 100 | end if |
| 101 | ################## |
| 102 | #コマンド整形 |
| 103 | set strSaveFilePath to (ocidSaveFilePathURL's |path|()) as text |
| 104 | set strFilePath to (ocidFilePathURL's |path|()) as text |
| 105 | set strBinPath to (ocidBinFilePathURL's |path|()) as text |
| 106 | # |
| 107 | set strCmd to ("'" & strBinPath & "' -upright '" & strFilePath & "' -o '" & strSaveFilePath & "'") as text |
| 108 | log strCmd |
| 109 | do shell script strCmd |
| 110 | |
| 111 | ################## |
| 112 | #開く |
| 113 | set appSharedWorkspace to refMe's NSWorkspace's sharedWorkspace() |
| 114 | set ocidOpenURLsArray to refMe's NSMutableArray's alloc()'s init() |
| 115 | (ocidOpenURLsArray's addObject:(ocidSaveFilePathURL)) |
| 116 | appSharedWorkspace's activateFileViewerSelectingURLs:(ocidOpenURLsArray) |
| 117 | tell application "Finder" to activate |
| 118 | return true |
| 119 | ############################## |
| 120 | #インストールサプ |
| 121 | to doInstall() |
| 122 | ############################## |
| 123 | #設定項目 |
| 124 | #ダウンロードURL |
| 125 | set strURL to ("https://codeload.github.com/coherentgraphics/cpdf-binaries/zip/refs/heads/master") as text |
| 126 | #インストール先 |
| 127 | set strSaveDir to ("~/Library/Application Support/bin/cpdf") as text |
| 128 | ############################## |
| 129 | #メタ文字 |
| 130 | set strLF to ("" & linefeed & "") as text |
| 131 | set strCR to ("" & return & "") as text |
| 132 | set strCRLF to ("" & return & linefeed & "") as text |
| 133 | set strSpace to ("" & space & "") as text |
| 134 | set strTab to ("" & tab & "") as text |
| 135 | ############################## |
| 136 | #ダウンロード先(起動時に削除される項目) |
| 137 | set appFileManager to refMe's NSFileManager's defaultManager() |
| 138 | set ocidTempDirURL to appFileManager's temporaryDirectory() |
| 139 | set ocidUUID to refMe's NSUUID's alloc()'s init() |
| 140 | set ocidUUIDString to ocidUUID's UUIDString |
| 141 | set ocidDownloadDirPathURL to ocidTempDirURL's URLByAppendingPathComponent:(ocidUUIDString) isDirectory:true |
| 142 | set ocidAttrDict to refMe's NSMutableDictionary's alloc()'s initWithCapacity:0 |
| 143 | ocidAttrDict's setValue:(511) forKey:(refMe's NSFilePosixPermissions) |
| 144 | set listDone to appFileManager's createDirectoryAtURL:(ocidDownloadDirPathURL) withIntermediateDirectories:true attributes:(ocidAttrDict) |error|:(reference) |
| 145 | set ocidDownloadFilePathURL to ocidDownloadDirPathURL's URLByAppendingPathComponent:("master.zip") isDirectory:(false) |
| 146 | set strDownloadFilePath to (ocidDownloadFilePathURL's |path|()) as text |
| 147 | set strExpandDirPath to (ocidDownloadDirPathURL's |path|()) as text |
| 148 | ############################## |
| 149 | #保存先 最終的なインストール先 |
| 150 | set ocidSaveDirPathStr to refMe's NSString's stringWithString:(strSaveDir) |
| 151 | set ocidSaveDirPath to ocidSaveDirPathStr's stringByStandardizingPath() |
| 152 | set ocidSaveDirPathURL to (refMe's NSURL's alloc()'s initFileURLWithPath:(ocidSaveDirPath) isDirectory:false) |
| 153 | |
| 154 | ############################## |
| 155 | #今あるものをゴミ箱へ |
| 156 | set boolDirExists to appFileManager's fileExistsAtPath:(ocidSaveDirPath) isDirectory:(true) |
| 157 | if (boolDirExists as boolean) is true then |
| 158 | set appFileManager to refMe's NSFileManager's defaultManager() |
| 159 | set listDone to (appFileManager's trashItemAtURL:(ocidSaveDirPathURL) resultingItemURL:(ocidDownloadDirPathURL) |error|:(reference)) |
| 160 | end if |
| 161 | |
| 162 | ############################## |
| 163 | #新たにフォルダを作り直す |
| 164 | ocidAttrDict's setValue:(448) forKey:(refMe's NSFilePosixPermissions) |
| 165 | set listDone to appFileManager's createDirectoryAtURL:(ocidSaveDirPathURL) withIntermediateDirectories:true attributes:(ocidAttrDict) |error|:(reference) |
| 166 | ############################## |
| 167 | #リダイレクト先 |
| 168 | set strCmd to ("/usr/bin/curl -s -L -I -o /dev/null -w '%{url_effective}' \"" & strURL & "\"") as text |
| 169 | try |
| 170 | set strRedirectURL to (do shell script strCmd) as text |
| 171 | on error strErrMsg number numErrNo |
| 172 | log "No: " & numErrNo & linefeed & "Error:" & strErrMsg |
| 173 | return false |
| 174 | end try |
| 175 | ############################## |
| 176 | #ダウンロード |
| 177 | set strCmd to ("/usr/bin/curl -L -o \"" & strDownloadFilePath & "\" \"" & strRedirectURL & "\" --connect-timeout 20") as text |
| 178 | try |
| 179 | set strResponse to (do shell script strCmd) as text |
| 180 | on error strErrMsg number numErrNo |
| 181 | log "No: " & numErrNo & linefeed & "Error:" & strErrMsg |
| 182 | return false |
| 183 | end try |
| 184 | ############################## |
| 185 | #解凍 |
| 186 | set strCmd to ("/usr/bin/bsdtar -xzf \"" & strDownloadFilePath & "\" -C \"" & strExpandDirPath & "\"") as text |
| 187 | try |
| 188 | set strResponse to (do shell script strCmd) as text |
| 189 | on error strErrMsg number numErrNo |
| 190 | log "No: " & numErrNo & linefeed & "Error:" & strErrMsg |
| 191 | return false |
| 192 | end try |
| 193 | ############################## |
| 194 | #解凍結果 |
| 195 | set reocdSystemInfot to (system info) as record |
| 196 | set strCPU to (CPU type of reocdSystemInfot) as text |
| 197 | #CPUで分岐 |
| 198 | if strCPU contains "ARM" then |
| 199 | #ARM |
| 200 | set ocidExpandBinFilePathURL to ocidDownloadDirPathURL's URLByAppendingPathComponent:("cpdf-binaries-master/OSX-ARM/cpdf") isDirectory:(false) |
| 201 | |
| 202 | else |
| 203 | #INTEL |
| 204 | set ocidExpandBinFilePathURL to ocidDownloadDirPathURL's URLByAppendingPathComponent:("cpdf-binaries-master/OSX-Intel/cpdf") isDirectory:(false) |
| 205 | |
| 206 | end if |
| 207 | |
| 208 | #最終的なバイナリーの移動先 |
| 209 | set ocidDistBinFilePathURL to ocidSaveDirPathURL's URLByAppendingPathComponent:("cpdf") isDirectory:(false) |
| 210 | #移動 |
| 211 | set listDone to (appFileManager's moveItemAtURL:(ocidExpandBinFilePathURL) toURL:(ocidDistBinFilePathURL) |error|:(reference)) |
| 212 | ############################## |
| 213 | #解凍前のファイルをゴミ箱へ |
| 214 | set appFileManager to refMe's NSFileManager's defaultManager() |
| 215 | set listDone to (appFileManager's trashItemAtURL:(ocidDownloadDirPathURL) resultingItemURL:(ocidDownloadDirPathURL) |error|:(reference)) |
| 216 | |
| 217 | return true |
| 218 | |
| 219 | end doInstall |
| AppleScriptで生成しました | |
登録:
コメント (Atom)



