20260320

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


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

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

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

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