20260323

【cpdf】PDFページの分割(上から下への2分割TTB)


【cpdf】PDFページの分割(上から下への2分割TTB)

NOTE記事一覧ですnote.com

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

【2】ChopL2B左→右分割.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
016マニュアル
017https://www.coherentpdf.com/cpdfmanual/cpdfmanualch9.html#x12-970009.4
018
019
020
021com.cocolog-nifty.quicktimer.icefloe *)
022----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
023use AppleScript version "2.8"
024use framework "Foundation"
025use framework "UniformTypeIdentifiers"
026use framework "AppKit"
027use scripting additions
028property refMe : a reference to current application
029
030
031##################
032#cpdfバイナリーのパス
033set strResponse to doChkInstall("cpdf")
034if strResponse is false then
035   set strBinPath to ("~/Library/Application Support/bin/cpdf/cpdf") as text
036else
037   set strBinPath to strResponse as text
038end if
039set ocidBinPathStr to refMe's NSString's stringWithString:(strBinPath)
040set ocidBinPath to ocidBinPathStr's stringByStandardizingPath()
041set ocidBinFilePathURL to refMe's NSURL's alloc()'s initFileURLWithPath:(ocidBinPath) isDirectory:(false)
042#メタ文字
043set strLF to ("" & linefeed & "") as text
044set strCR to ("" & return & "") as text
045set strCRLF to ("" & return & linefeed & "") as text
046set strSpace to ("" & space & "") as text
047set strTab to ("" & tab & "") as text
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#入力ファイルパス
077set strFilePath to (POSIX path of aliasFilePath) as text
078set ocidFilePathStr to refMe's NSString's stringWithString:(strFilePath)
079set ocidFilePath to ocidFilePathStr's stringByStandardizingPath()
080set ocidFilePathURL to (refMe's NSURL's alloc()'s initFileURLWithPath:(ocidFilePath) isDirectory:false)
081##################
082#出力ファイル
083set ocidFileName to ocidFilePathURL's lastPathComponent()
084set strBaseFileName to (ocidFileName's stringByDeletingPathExtension()) as text
085set strDefaultFileName to (strBaseFileName & "-左右分割済.pdf") as text
086set strExtensionName to (ocidFilePathURL's pathExtension()) as text
087set ocidContainerDirPathURL to ocidFilePathURL's URLByDeletingLastPathComponent()
088set aliasContainerDirPath to (ocidContainerDirPathURL's absoluteURL()) as alias
089
090set strPromptText to "名前を決めてください" as text
091set strMesText to "名前を決めてください" as text
092###ファイル名 ダイアログ
093tell application "SystemUIServer"
094   activate
095   set aliasFilePath to (choose file name strMesText default location aliasContainerDirPath default name strDefaultFileName with prompt strPromptText) as «class furl»
096end tell
097##################
098#インストールチェック
099set boolExist to appFileManager's fileExistsAtPath:(ocidBinPath) isDirectory:(false)
100if boolExist is false then
101   set boolDone to doInstall()
102   if boolDone is false then
103      log "インストールに失敗しました"
104      return false
105   end if
106end if
107
108##################
109#出力パス
110set strSaveFilePath to (POSIX path of aliasFilePath) as text
111set ocidSaveFilePathStr to refMe's NSString's stringWithString:(strSaveFilePath)
112set ocidSaveFilePath to ocidSaveFilePathStr's stringByStandardizingPath()
113set ocidSaveFilePathURL to (refMe's NSURL's alloc()'s initFileURLWithPath:(ocidSaveFilePath) isDirectory:false)
114set ocidContainerDirPathURL to ocidFilePathURL's URLByDeletingLastPathComponent()
115###拡張子
116set strExtension to (ocidFilePathURL's pathExtension()) as text
117###最後のアイテムがファイル名
118set strFileName to (ocidFilePathURL's lastPathComponent()) as text
119###拡張子のつけ忘れ対策
120if strFileName does not contain strExtensionName then
121   set strFileName to (strFileName & "." & strExtensionName) as text
122   set ocidFilePathURL to ocidContainerDirPathURL's URLByAppendingPathComponent:(strFileName)
123end if
124##################
125#コマンド整形
126set strSaveFilePath to (ocidSaveFilePathURL's |path|()) as text
127set strFilePath to (ocidFilePathURL's |path|()) as text
128set strBinPath to (ocidBinFilePathURL's |path|()) as text
129##################
130#まずはページの回転を0とする
131set strCmd to ("'" & strBinPath & "' -upright '" & strFilePath & "' -o '" & strSaveFilePath & "'") as text
132log strCmd
133do shell script strCmd
134
135
136##################
137#1ページ目のページの回転を取得
138set strCmd to ("\"" & strBinPath & "\" -page-info -json -utf8   \"" & strSaveFilePath & "\" 1 ") as text
139log strCmd
140try
141   set strJsonResponse to (do shell script strCmd) as text
142on error
143   return "コマンドでエラーになりました"
144end try
145#戻り値のJSONから1ページ目の回転値を取得
146set ocidJsonStr to refMe's NSString's stringWithString:(strJsonResponse)
147set ocidStringData to ocidJsonStr's dataUsingEncoding:(refMe's NSUTF8StringEncoding)
148set ocidOption to (refMe's NSJSONReadingMutableContainers)
149set listJSONSerialization to (refMe's NSJSONSerialization's JSONObjectWithData:(ocidStringData) options:(ocidOption) |error|:(reference))
150set ocidPlistDict to (item 1 of listJSONSerialization)'s firstObject()
151set numPageRotation to (ocidPlistDict's valueForKey:("Rotation")) as integer
152
153#ページの回転に合わせたコマンド整形
154if numPageRotation = 0 then
155   set strCmd to ("\"" & strBinPath & "\" -chop \"2 1\"  \"" & strSaveFilePath & "\" -o \"" & strSaveFilePath & "\"") as text
156else if numPageRotation = 90 then
157   set strCmd to ("\"" & strBinPath & "\" -chop \"1 2\" -chop-btt \"" & strSaveFilePath & "\" -o \"" & strSaveFilePath & "\"") as text
158else if numPageRotation = 270 then
159   set strCmd to ("\"" & strBinPath & "\" -chop \"1 2\" \"" & strSaveFilePath & "\" -o \"" & strSaveFilePath & "\"") as text
160else if numPageRotation = 180 then
161   set strCmd to ("\"" & strBinPath & "\" -chop \"2 1\"  -chop-rtl   \"" & strSaveFilePath & "\" -o \"" & strSaveFilePath & "\"") as text
162end if
163log strCmd
164try
165   do shell script strCmd
166on error strErrMsg number numErrNo
167   log "No: " & numErrNo & strLF & "Error:" & strErrMsg
168   return false
169end try
170
171set strCmd to ("'" & strBinPath & "' -upright '" & strSaveFilePath & "' -o '" & strSaveFilePath & "'") as text
172log strCmd
173do shell script strCmd
174
175return 0
176
177
178##############################
179#すでにインストール済みで
180#ユーザーがRC設定している場合は
181#パスが通るのでこれがエラーにならない
182to 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
190end doChkInstall
191
192##############################
193to 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
209end doQuitSelf
210
211##############################
212#インストールサプ
213to 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   
317end doInstall
AppleScriptで生成しました