20260320

【cpdf】PDFページの原寸集約2UP(2in1_4原稿横_B2T)


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

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

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

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