20260323

【cpdf】PDFページの分割(右から左への2分割 RTL)


【cpdf】PDFページの分割(右から左への2分割 RTL)

NOTE記事一覧ですnote.com

【スクリプトエディタで開く】 |

【1】ChopR2L右→左分割.scpt
ソース
001#!/usr/bin/env osascript
002#coding: utf-8
003----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
004(*
005
006cpdfが未インストールの場合インストールします
007困る場合は実行しないでください
008cpdfインストール先は
009/Users/ユーザーID/Library/Application Support/bin/cpdf
010
011
012v1 初回作成
013v1.1 インストール機能をつけた
014
015マニュアル
016https://www.coherentpdf.com/cpdfmanual/cpdfmanualch9.html#x12-970009.4
017
018
019com.cocolog-nifty.quicktimer.icefloe *)
020----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
021use AppleScript version "2.8"
022use framework "Foundation"
023use framework "UniformTypeIdentifiers"
024use framework "AppKit"
025use scripting additions
026property refMe : a reference to current application
027
028
029##################
030#cpdfバイナリーのパス
031set strResponse to doChkInstall("cpdf")
032if strResponse is false then
033   set strBinPath to ("~/Library/Application Support/bin/cpdf/cpdf") as text
034else
035   set strBinPath to strResponse as text
036end if
037set ocidBinPathStr to refMe's NSString's stringWithString:(strBinPath)
038set ocidBinPath to ocidBinPathStr's stringByStandardizingPath()
039set ocidBinFilePathURL to refMe's NSURL's alloc()'s initFileURLWithPath:(ocidBinPath) isDirectory:(false)
040#メタ文字
041set strLF to ("" & linefeed & "") as text
042set strCR to ("" & return & "") as text
043set strCRLF to ("" & return & linefeed & "") as text
044set strSpace to ("" & space & "") as text
045set strTab to ("" & tab & "") as text
046
047
048
049##################
050#入力ファイル
051set appFileManager to refMe's NSFileManager's defaultManager()
052set ocidURLsArray to (appFileManager's URLsForDirectory:(refMe's NSDesktopDirectory) inDomains:(refMe's NSUserDomainMask))
053set ocidDesktopDirPathURL to ocidURLsArray's firstObject()
054set aliasDesktopDirPath to (ocidDesktopDirPathURL's absoluteURL()) as alias
055#ダイアログを全面に
056set strName to (name of current application) as text
057if strName is "osascript" then
058   tell application "Finder" to activate
059else
060   tell current application to activate
061end if
062set listUTI to {"com.adobe.pdf"} as list
063set strMes to ("PDFファイルを選んでください") as text
064set strPrompt to ("PDFファイルを選んでください") as text
065try
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
071on error strErrMsg number numErrNo
072   log "No: " & numErrNo & linefeed & "Error:" & strErrMsg
073   return false
074end try
075
076##################
077#入力ファイルパス
078set strFilePath to (POSIX path of aliasFilePath) as text
079set ocidFilePathStr to refMe's NSString's stringWithString:(strFilePath)
080set ocidFilePath to ocidFilePathStr's stringByStandardizingPath()
081set ocidFilePathURL to (refMe's NSURL's alloc()'s initFileURLWithPath:(ocidFilePath) isDirectory:false)
082
083##################
084#出力ファイル
085set ocidFileName to ocidFilePathURL's lastPathComponent()
086set strBaseFileName to (ocidFileName's stringByDeletingPathExtension()) as text
087set strDefaultFileName to (strBaseFileName & "-左右分割済.pdf") as text
088set strExtensionName to (ocidFilePathURL's pathExtension()) as text
089set ocidContainerDirPathURL to ocidFilePathURL's URLByDeletingLastPathComponent()
090set aliasContainerDirPath to (ocidContainerDirPathURL's absoluteURL()) as alias
091
092set strPromptText to "名前を決めてください" as text
093set strMesText to "名前を決めてください" as text
094###ファイル名 ダイアログ
095tell application "SystemUIServer"
096   activate
097   set aliasFilePath to (choose file name strMesText default location aliasContainerDirPath default name strDefaultFileName with prompt strPromptText) as «class furl»
098end tell
099
100
101##################
102#インストールチェック
103set boolExist to appFileManager's fileExistsAtPath:(ocidBinPath) isDirectory:(false)
104if boolExist is false then
105   set boolDone to doInstall()
106   if boolDone is false then
107      log "インストールに失敗しました"
108      return false
109   end if
110end if
111
112
113
114##################
115#出力パス
116set strSaveFilePath to (POSIX path of aliasFilePath) as text
117set ocidSaveFilePathStr to refMe's NSString's stringWithString:(strSaveFilePath)
118set ocidSaveFilePath to ocidSaveFilePathStr's stringByStandardizingPath()
119set ocidSaveFilePathURL to (refMe's NSURL's alloc()'s initFileURLWithPath:(ocidSaveFilePath) isDirectory:false)
120set ocidContainerDirPathURL to ocidFilePathURL's URLByDeletingLastPathComponent()
121###拡張子
122set strExtension to (ocidFilePathURL's pathExtension()) as text
123###最後のアイテムがファイル名
124set strFileName to (ocidFilePathURL's lastPathComponent()) as text
125###拡張子のつけ忘れ対策
126if strFileName does not contain strExtensionName then
127   set strFileName to (strFileName & "." & strExtensionName) as text
128   set ocidFilePathURL to ocidContainerDirPathURL's URLByAppendingPathComponent:(strFileName)
129end if
130
131##################
132#コマンド整形
133set strSaveFilePath to (ocidSaveFilePathURL's |path|()) as text
134set strFilePath to (ocidFilePathURL's |path|()) as text
135set strBinPath to (ocidBinFilePathURL's |path|()) as text
136##################
137#まずはページの回転を0とする
138set strCmd to ("'" & strBinPath & "' -upright '" & strFilePath & "' -o '" & strSaveFilePath & "'") as text
139log strCmd
140do shell script strCmd
141
142
143##################
144#1ページ目のページの回転を取得
145set strCmd to ("\"" & strBinPath & "\" -page-info -json -utf8   \"" & strSaveFilePath & "\" 1 ") as text
146log strCmd
147try
148   set strJsonResponse to (do shell script strCmd) as text
149on error
150   return "コマンドでエラーになりました"
151end try
152#戻り値のJSONから1ページ目の回転値を取得
153set ocidJsonStr to refMe's NSString's stringWithString:(strJsonResponse)
154set ocidStringData to ocidJsonStr's dataUsingEncoding:(refMe's NSUTF8StringEncoding)
155set ocidOption to (refMe's NSJSONReadingMutableContainers)
156set listJSONSerialization to (refMe's NSJSONSerialization's JSONObjectWithData:(ocidStringData) options:(ocidOption) |error|:(reference))
157set ocidPlistDict to (item 1 of listJSONSerialization)'s firstObject()
158set numPageRotation to (ocidPlistDict's valueForKey:("Rotation")) as integer
159
160#ページの回転に合わせたコマンド整形
161if numPageRotation = 0 then
162   set strCmd to ("\"" & strBinPath & "\" -chop \"2 1\" -chop-rtl \"" & strSaveFilePath & "\" -o \"" & strSaveFilePath & "\"") as text
163else if numPageRotation = 90 then
164   set strCmd to ("\"" & strBinPath & "\" -chop \"1 2\" -chop-columns \"" & strSaveFilePath & "\" -o \"" & strSaveFilePath & "\"") as text
165else if numPageRotation = 270 then
166   set strCmd to ("\"" & strBinPath & "\" -chop \"1 2\" -chop-btt \"" & strSaveFilePath & "\" -o \"" & strSaveFilePath & "\"") as text
167else if numPageRotation = 180 then
168   set strCmd to ("\"" & strBinPath & "\" -chop \"2 1\" \"" & strSaveFilePath & "\" -o \"" & strSaveFilePath & "\"") as text
169end if
170log strCmd
171try
172   do shell script strCmd
173on error
174   return "コマンドでエラーになりました"
175end try
176
177set strCmd to ("'" & strBinPath & "' -upright '" & strSaveFilePath & "' -o '" & strSaveFilePath & "'") as text
178log strCmd
179do shell script strCmd
180
181return 0
182
183
184##############################
185#すでにインストール済みで
186#ユーザーがRC設定している場合は
187#パスが通るのでこれがエラーにならない
188to 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
196end doChkInstall
197
198##############################
199to 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
215end doQuitSelf
216
217##############################
218#インストールサプ
219to 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   
323end doInstall
AppleScriptで生成しました