20260320

【cpdf】PDFページの原寸集4UP(PrintPos4_4in1)


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

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

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

PrintPos4_4in1.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#入力ファイルパス
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 strExtensionName to (ocidFilePathURL's pathExtension()) as text
082set ocidContainerDirPathURL to ocidFilePathURL's URLByDeletingLastPathComponent()
083set aliasContainerDirPath to (ocidContainerDirPathURL's absoluteURL()) as alias
084#
085set strDefaultFileName to (strBaseFileName & "-4in1.pdf") as text
086#
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#インストールチェック
096set boolExist to appFileManager's fileExistsAtPath:(ocidBinPath) isDirectory:(false)
097if boolExist is false then
098   set boolDone to doInstall()
099   if boolDone is false then
100      log "インストールに失敗しました"
101      return false
102   end if
103end if
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#
126set strCommandText to ("\"" & strBinPath & "\" -impose-xy \"2 2\" -impose-rtl -impose-columns \"" & strFilePath & "\" -o \"" & strSaveFilePath & "\"") as text
127log strCommandText
128do shell script strCommandText
129##############################
130#すでにインストール済みで
131#ユーザーがRC設定している場合は
132#パスが通るのでこれがエラーにならない
133to doChkInstall(argBinName)
134   try
135      set strCmd to ("/bin/zsh -c 'where " & argBinName & "'")
136      set strResponse to (do shell script strCmd) as text
137      return strResponse
138   on error strErrMsg number numErrNo
139      return false
140   end try
141end doChkInstall
142##############################
143to doQuitSelf()
144   set strOutputString to (missing value)
145   set listAliasFilePath to (missing value)
146   set listBundleID to {"com.apple.automator.xpc.runner", "com.apple.automator.xpc.workflowServiceRunner"} as list
147   repeat with itemBundleID in listBundleID
148      set ocidAppArray to (refMe's NSRunningApplication's runningApplicationsWithBundleIdentifier:(itemBundleID))
149      repeat with itemApp in ocidAppArray
150         try
151            set boolDone to itemApp's terminate()
152         end try
153      end repeat
154   end repeat
155   refMe's NSRunningApplication's terminateAutomaticallyTerminableApplications()
156   try
157      tell application "System Events" to quit
158   end try
159   return true
160end doQuitSelf
161##############################
162#インストールサプ
163to doInstall()
164   ##############################
165   #設定項目
166   #ダウンロードURL
167   set strURL to ("https://codeload.github.com/coherentgraphics/cpdf-binaries/zip/refs/heads/master") as text
168   #インストール先
169   set strSaveDir to ("~/Library/Application Support/bin/cpdf") as text
170   ##############################
171   #メタ文字
172   set strLF to ("" & linefeed & "") as text
173   set strCR to ("" & return & "") as text
174   set strCRLF to ("" & return & linefeed & "") as text
175   set strSpace to ("" & space & "") as text
176   set strTab to ("" & tab & "") as text
177   ##############################
178   #ダウンロード先(起動時に削除される項目)
179   set appFileManager to refMe's NSFileManager's defaultManager()
180   set ocidTempDirURL to appFileManager's temporaryDirectory()
181   set ocidUUID to refMe's NSUUID's alloc()'s init()
182   set ocidUUIDString to ocidUUID's UUIDString
183   set ocidDownloadDirPathURL to ocidTempDirURL's URLByAppendingPathComponent:(ocidUUIDString) isDirectory:true
184   set ocidAttrDict to refMe's NSMutableDictionary's alloc()'s initWithCapacity:0
185   ocidAttrDict's setValue:(511) forKey:(refMe's NSFilePosixPermissions)
186   set listDone to appFileManager's createDirectoryAtURL:(ocidDownloadDirPathURL) withIntermediateDirectories:true attributes:(ocidAttrDict) |error|:(reference)
187   set ocidDownloadFilePathURL to ocidDownloadDirPathURL's URLByAppendingPathComponent:("master.zip") isDirectory:(false)
188   set strDownloadFilePath to (ocidDownloadFilePathURL's |path|()) as text
189   set strExpandDirPath to (ocidDownloadDirPathURL's |path|()) as text
190   ##############################
191   #保存先 最終的なインストール先
192   set ocidSaveDirPathStr to refMe's NSString's stringWithString:(strSaveDir)
193   set ocidSaveDirPath to ocidSaveDirPathStr's stringByStandardizingPath()
194   set ocidSaveDirPathURL to (refMe's NSURL's alloc()'s initFileURLWithPath:(ocidSaveDirPath) isDirectory:false)
195   
196   ##############################
197   #今あるものをゴミ箱へ
198   set boolDirExists to appFileManager's fileExistsAtPath:(ocidSaveDirPath) isDirectory:(true)
199   if (boolDirExists as boolean) is true then
200      set appFileManager to refMe's NSFileManager's defaultManager()
201      set listDone to (appFileManager's trashItemAtURL:(ocidSaveDirPathURL) resultingItemURL:(ocidDownloadDirPathURL) |error|:(reference))
202   end if
203   
204   ##############################
205   #新たにフォルダを作り直す
206   ocidAttrDict's setValue:(448) forKey:(refMe's NSFilePosixPermissions)
207   set listDone to appFileManager's createDirectoryAtURL:(ocidSaveDirPathURL) withIntermediateDirectories:true attributes:(ocidAttrDict) |error|:(reference)
208   ##############################
209   #リダイレクト先
210   set strCmd to ("/usr/bin/curl -s -L -I -o /dev/null -w '%{url_effective}' \"" & strURL & "\"") as text
211   try
212      set strRedirectURL to (do shell script strCmd) as text
213   on error strErrMsg number numErrNo
214      log "No: " & numErrNo & linefeed & "Error:" & strErrMsg
215      return false
216   end try
217   ##############################
218   #ダウンロード
219   set strCmd to ("/usr/bin/curl -L -o \"" & strDownloadFilePath & "\" \"" & strRedirectURL & "\" --connect-timeout 20") as text
220   try
221      set strResponse to (do shell script strCmd) as text
222   on error strErrMsg number numErrNo
223      log "No: " & numErrNo & linefeed & "Error:" & strErrMsg
224      return false
225   end try
226   ##############################
227   #解凍
228   set strCmd to ("/usr/bin/bsdtar -xzf \"" & strDownloadFilePath & "\" -C \"" & strExpandDirPath & "\"") as text
229   try
230      set strResponse to (do shell script strCmd) as text
231   on error strErrMsg number numErrNo
232      log "No: " & numErrNo & linefeed & "Error:" & strErrMsg
233      return false
234   end try
235   ##############################
236   #解凍結果
237   set reocdSystemInfot to (system info) as record
238   set strCPU to (CPU type of reocdSystemInfot) as text
239   #CPUで分岐
240   if strCPU contains "ARM" then
241      #ARM
242      set ocidExpandBinFilePathURL to ocidDownloadDirPathURL's URLByAppendingPathComponent:("cpdf-binaries-master/OSX-ARM/cpdf") isDirectory:(false)
243      
244   else
245      #INTEL
246      set ocidExpandBinFilePathURL to ocidDownloadDirPathURL's URLByAppendingPathComponent:("cpdf-binaries-master/OSX-Intel/cpdf") isDirectory:(false)
247      
248   end if
249   
250   #最終的なバイナリーの移動先
251   set ocidDistBinFilePathURL to ocidSaveDirPathURL's URLByAppendingPathComponent:("cpdf") isDirectory:(false)
252   #移動
253   set listDone to (appFileManager's moveItemAtURL:(ocidExpandBinFilePathURL) toURL:(ocidDistBinFilePathURL) |error|:(reference))
254   ##############################
255   #解凍前のファイルをゴミ箱へ
256   set appFileManager to refMe's NSFileManager's defaultManager()
257   set listDone to (appFileManager's trashItemAtURL:(ocidDownloadDirPathURL) resultingItemURL:(ocidDownloadDirPathURL) |error|:(reference))
258   
259   return true
260   
261end doInstall
AppleScriptで生成しました