20260320

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


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

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

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

2in1_2原稿縦_L2R.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 strSaveFilePath to (POSIX path of aliasFilePath) as text
104set ocidSaveFilePathStr to refMe's NSString's stringWithString:(strSaveFilePath)
105set ocidSaveFilePath to ocidSaveFilePathStr's stringByStandardizingPath()
106set ocidSaveFilePathURL to (refMe's NSURL's alloc()'s initFileURLWithPath:(ocidSaveFilePath) isDirectory:false)
107set ocidContainerDirPathURL to ocidFilePathURL's URLByDeletingLastPathComponent()
108###拡張子
109set strExtension to (ocidFilePathURL's pathExtension()) as text
110###最後のアイテムがファイル名
111set strFileName to (ocidFilePathURL's lastPathComponent()) as text
112###拡張子のつけ忘れ対策
113if strFileName does not contain strExtensionName then
114   set strFileName to (strFileName & "." & strExtensionName) as text
115   set ocidFilePathURL to ocidContainerDirPathURL's URLByAppendingPathComponent:(strFileName)
116end if
117##################
118#コマンド整形
119set strSaveFilePath to (ocidSaveFilePathURL's |path|()) as text
120set strFilePath to (ocidFilePathURL's |path|()) as text
121set strBinPath to (ocidBinFilePathURL's |path|()) as text
122#
123set strCommandText to ("\"" & strBinPath & "\" -impose-xy \"2 1\" \"" & strFilePath & "\" -o \"" & strSaveFilePath & "\"") as text
124log strCommandText
125do shell script strCommandText
126##############################
127#すでにインストール済みで
128#ユーザーがRC設定している場合は
129#パスが通るのでこれがエラーにならない
130to doChkInstall(argBinName)
131   try
132      set strCmd to ("/bin/zsh -c 'where " & argBinName & "'")
133      set strResponse to (do shell script strCmd) as text
134      return strResponse
135   on error strErrMsg number numErrNo
136      return false
137   end try
138end doChkInstall
139##############################
140to doQuitSelf()
141   try
142      tell application "System Events" to quit
143   end try
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   return true
156end doQuitSelf
157##############################
158#インストールサプ
159to doInstall()
160   ##############################
161   #設定項目
162   #ダウンロードURL
163   set strURL to ("https://codeload.github.com/coherentgraphics/cpdf-binaries/zip/refs/heads/master") as text
164   #インストール先
165   set strSaveDir to ("~/Library/Application Support/bin/cpdf") as text
166   ##############################
167   #メタ文字
168   set strLF to ("" & linefeed & "") as text
169   set strCR to ("" & return & "") as text
170   set strCRLF to ("" & return & linefeed & "") as text
171   set strSpace to ("" & space & "") as text
172   set strTab to ("" & tab & "") as text
173   ##############################
174   #ダウンロード先(起動時に削除される項目)
175   set appFileManager to refMe's NSFileManager's defaultManager()
176   set ocidTempDirURL to appFileManager's temporaryDirectory()
177   set ocidUUID to refMe's NSUUID's alloc()'s init()
178   set ocidUUIDString to ocidUUID's UUIDString
179   set ocidDownloadDirPathURL to ocidTempDirURL's URLByAppendingPathComponent:(ocidUUIDString) isDirectory:true
180   set ocidAttrDict to refMe's NSMutableDictionary's alloc()'s initWithCapacity:0
181   ocidAttrDict's setValue:(511) forKey:(refMe's NSFilePosixPermissions)
182   set listDone to appFileManager's createDirectoryAtURL:(ocidDownloadDirPathURL) withIntermediateDirectories:true attributes:(ocidAttrDict) |error|:(reference)
183   set ocidDownloadFilePathURL to ocidDownloadDirPathURL's URLByAppendingPathComponent:("master.zip") isDirectory:(false)
184   set strDownloadFilePath to (ocidDownloadFilePathURL's |path|()) as text
185   set strExpandDirPath to (ocidDownloadDirPathURL's |path|()) as text
186   ##############################
187   #保存先 最終的なインストール先
188   set ocidSaveDirPathStr to refMe's NSString's stringWithString:(strSaveDir)
189   set ocidSaveDirPath to ocidSaveDirPathStr's stringByStandardizingPath()
190   set ocidSaveDirPathURL to (refMe's NSURL's alloc()'s initFileURLWithPath:(ocidSaveDirPath) isDirectory:false)
191   
192   ##############################
193   #今あるものをゴミ箱へ
194   set boolDirExists to appFileManager's fileExistsAtPath:(ocidSaveDirPath) isDirectory:(true)
195   if (boolDirExists as boolean) is true then
196      set appFileManager to refMe's NSFileManager's defaultManager()
197      set listDone to (appFileManager's trashItemAtURL:(ocidSaveDirPathURL) resultingItemURL:(ocidDownloadDirPathURL) |error|:(reference))
198   end if
199   
200   ##############################
201   #新たにフォルダを作り直す
202   ocidAttrDict's setValue:(448) forKey:(refMe's NSFilePosixPermissions)
203   set listDone to appFileManager's createDirectoryAtURL:(ocidSaveDirPathURL) withIntermediateDirectories:true attributes:(ocidAttrDict) |error|:(reference)
204   ##############################
205   #リダイレクト先
206   set strCmd to ("/usr/bin/curl -s -L -I -o /dev/null -w '%{url_effective}' \"" & strURL & "\"") as text
207   try
208      set strRedirectURL to (do shell script strCmd) as text
209   on error strErrMsg number numErrNo
210      log "No: " & numErrNo & linefeed & "Error:" & strErrMsg
211      return false
212   end try
213   ##############################
214   #ダウンロード
215   set strCmd to ("/usr/bin/curl -L -o \"" & strDownloadFilePath & "\" \"" & strRedirectURL & "\" --connect-timeout 20") as text
216   try
217      set strResponse 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   set strCmd to ("/usr/bin/bsdtar -xzf \"" & strDownloadFilePath & "\" -C \"" & strExpandDirPath & "\"") as text
225   try
226      set strResponse to (do shell script strCmd) as text
227   on error strErrMsg number numErrNo
228      log "No: " & numErrNo & linefeed & "Error:" & strErrMsg
229      return false
230   end try
231   ##############################
232   #解凍結果
233   set reocdSystemInfot to (system info) as record
234   set strCPU to (CPU type of reocdSystemInfot) as text
235   #CPUで分岐
236   if strCPU contains "ARM" then
237      #ARM
238      set ocidExpandBinFilePathURL to ocidDownloadDirPathURL's URLByAppendingPathComponent:("cpdf-binaries-master/OSX-ARM/cpdf") isDirectory:(false)
239      
240   else
241      #INTEL
242      set ocidExpandBinFilePathURL to ocidDownloadDirPathURL's URLByAppendingPathComponent:("cpdf-binaries-master/OSX-Intel/cpdf") isDirectory:(false)
243      
244   end if
245   
246   #最終的なバイナリーの移動先
247   set ocidDistBinFilePathURL to ocidSaveDirPathURL's URLByAppendingPathComponent:("cpdf") isDirectory:(false)
248   #移動
249   set listDone to (appFileManager's moveItemAtURL:(ocidExpandBinFilePathURL) toURL:(ocidDistBinFilePathURL) |error|:(reference))
250   ##############################
251   #解凍前のファイルをゴミ箱へ
252   set appFileManager to refMe's NSFileManager's defaultManager()
253   set listDone to (appFileManager's trashItemAtURL:(ocidDownloadDirPathURL) resultingItemURL:(ocidDownloadDirPathURL) |error|:(reference))
254   
255   return true
256   
257end doInstall
AppleScriptで生成しました