20260323

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


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

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