20260320

【cpdf】PDFページの原寸集約2UP(2in1_1原稿縦_R2L)


【cpdf】PDFページの原寸集約2UP

ソースのダウンロードはこちら

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

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