20260406

[mutool]コマンドライン実行用へルパ poster分割


[mutool]コマンドライン実行用へルパ poster分割

ダウンロードはこちら
NOTE記事一覧ですnote.com

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

[mutool]poster分割.scpt
ソース
001#!/usr/bin/env osascript
002#coding: utf-8
003----+----1----+----2----+-----3----+----4----+----5----+----6----+----7--
004(*
005
006mutoolのコマンドラインぺルパ
007
008mutoolのmake方法はこちら
009https://note.com/quicktimer/n/nbec6b6e72677
010
011
012v1初回作成
013
014com.cocolog-nifty.quicktimer.icefloe *)
015----+----1----+----2----+-----3----+----4----+----5----+----6----+----7--
016use AppleScript version "2.8"
017use framework "Foundation"
018use framework "AppKit"
019use framework "UniformTypeIdentifiers"
020use scripting additions
021
022property refMe : a reference to current application
023
024########################
025#設定項目 mutoolへのパス
026property strBinPath : ("~/Library/Application Support/bin/mupdf/mutool") as text
027
028########################
029# RUN
030#   on run {listAliasFilePath}
031
032on run
033   
034   ########################
035   #ダイアログ
036   set strMsg to ("PDFファイルを 選択") as text
037   set strPrompt to ("PDF を選択してください" & return & "複数選択可") as text
038   set aliasDesktopDirPath to (path to desktop from user domain) as alias
039   set listUTI to {"com.adobe.pdf"} as list
040   try
041      tell application "SystemUIServer"
042         activate
043         set listAliasFilePath to (choose file strMsg with prompt strPrompt default location aliasDesktopDirPath of type listUTI with invisibles and multiple selections allowed without showing package contents) as list
044      end tell
045   on error strErrMes number numErrNo
046      log strErrMes & numErrNo
047      return false
048   end try
049   if listAliasFilePath is {} then
050      return false
051   end if
052   
053   set boolDone to (open listAliasFilePath)
054   return boolDone
055end run
056
057########################
058# OPEN
059on open listAliasFilePath
060   #バイナリーのフルパス
061   set ocidBinFilePathStr to refMe's NSString's stringWithString:(strBinPath)
062   set ocidBinFilePath to ocidBinFilePathStr's stringByStandardizingPath()
063   #書き出し種選択
064   set strSaveExtension to doChooseList()
065   
066   #保存先
067   set aliasSaveDirPath to doSaveDir()
068   set strSaveDirPath to (POSIX path of aliasSaveDirPath) as text
069   
070   #ドロップ(OPEN)されたエイリアスリストを順番に処理
071   repeat with itemAliasFilePath in listAliasFilePath
072      set aliasItemFilePath to itemAliasFilePath as alias
073      set recordInfoFor to (info for aliasItemFilePath) as record
074      set strItemUTI to (type identifier of recordInfoFor) as text
075      #PDFファイルのみ次工程に回す
076      if strItemUTI is "com.adobe.pdf" then
077         set boolDone to doExecCmd(ocidBinFilePath, aliasItemFilePath, strSaveDirPath, strSaveExtension)
078         
079      end if
080   end repeat
081   return true
082end open
083
084########################
085#コマンド整形実行
086to doExecCmd(ocidBinFilePath, aliasItemFilePath, strSaveDirPath, strSaveExtension)
087   #エイリアス確定させてからのUNIXパス
088   set aliasFilePath to aliasItemFilePath as alias
089   set strFilePath to (POSIX path of aliasFilePath) as text
090   set ocidFilePath to doPathEscape(strFilePath)
091   set ocidFileName to ocidFilePath's lastPathComponent()
092   set ocidFileName to ocidFileName's decomposedStringWithCanonicalMapping()
093   set ocidBaseFileName to ocidFileName's stringByDeletingPathExtension()
094   #
095   set strSaveDirPath to strSaveDirPath as text
096   set ocidSaveDirPath to doPathEscape(strSaveDirPath)
097   
098   #コマンド整形して実行
099   
100   set strCmd to ("pushd \\\"" & ocidSaveDirPath & "\\\";\\\"" & ocidBinFilePath & "\\\" poster " & strSaveExtension & " -m 0 \\\"" & ocidFilePath & "\\\" \\\"" & ocidFileName & "\\\"") as text
101   
102   set boolDone to doExecZsh(strCmd)
103   return true
104end doExecCmd
105
106########################
107#パスのエスケープ
108to doPathEscape(strFilePath)
109   set ocidFilePathStr to refMe's NSString's stringWithString:(strFilePath)
110   set ocidFilePath to ocidFilePathStr's stringByStandardizingPath()
111   set ocidFilePathURL to refMe's NSURL's fileURLWithPath:(ocidFilePath) isDirectory:(false)
112   set listEscChar to {"\\", "\"", "$", "`"} as list
113   repeat with itemEscChar in listEscChar
114      set ocidFilePath to (ocidFilePath's stringByReplacingOccurrencesOfString:(itemEscChar) withString:("\\\\\\" & itemEscChar & ""))
115   end repeat
116   set ocidFilePath to (ocidFilePath's stringByReplacingOccurrencesOfString:("!") withString:("\\\"'!'\\\""))
117   return ocidFilePath
118end doPathEscape
119
120########################
121#コマンド実行
122to doExecZsh(argCmdText)
123   set strCmdText to argCmdText as text
124   set strExecZsh to ("/bin/zsh -c \"" & strCmdText & "\"") as text
125   try
126      log "コマンド開始"
127      set strStdOut to (do shell script strExecZsh) as text
128      log "コマンド終了"
129   on error strErroMsg number numErrorNo
130      return false
131   end try
132   if strStdOut is not "" then
133      #      set boolDone to doDialog(strStdOut)
134   end if
135   return true
136end doExecZsh
137
138
139to doChooseList()
140   
141   set listExtension to {"-x 1 -y 2", "-x 1 -y 2 -r", "-x 2 -y 1", "-x 2 -y 1 -r", "-x 2 -y 2", "-x 2 -y 2 -r", "-x 1 -y 4", "-x 1 -y 4 -r", "-x 2 -y 4", "-x 2 -y 4 -r", "-x 4 -y 4", "-x 4 -y 4 -r"} as list
142   set strTitle to ("選んでください") as text
143   set strPrompt to ("出力フォーマットをひとつ選んでください" & return & "-rはR2L 右から左です") as text
144   set strOK to ("OK") as text
145   set strCancel to ("キャンセル") as text
146   try
147      tell application "System Events"
148         activate
149         set valueResponse to (choose from list listExtension with title strTitle with prompt strPrompt default items (first item of listExtension) OK button name strOK cancel button name strCancel with empty selection allowed without multiple selections allowed)
150      end tell
151   on error strErrMes number numErrNo
152      log strErrMes & numErrNo
153      return false
154   end try
155   if (class of valueResponse) is boolean then
156      error "ユーザによってキャンセルされました。" number -128
157   else if (class of valueResponse) is list then
158      if valueResponse is {} then
159         log "Error 何も選んでいません"
160         return false
161      else
162         set strResponse to (first item of valueResponse) as text
163      end if
164   end if
165   
166   return strResponse
167end doChooseList
168
169########################
170#保存先指定
171to doSaveDir()
172   set strMsg to ("保存先を指定してください" & return & "ファイル数が多くなることもありますので" & return & "新規フォルダを指定推奨") as text
173   set strPrompt to ("保存先を指定してください" & return & "ファイル数が多くなることもありますので" & return & "新規フォルダを指定推奨") as text
174   set appFileManager to refMe's NSFileManager's defaultManager()
175   set ocidURLsArray to (appFileManager's URLsForDirectory:(refMe's NSDesktopDirectory) inDomains:(refMe's NSUserDomainMask))
176   set ocidDesktopDirPathURL to ocidURLsArray's firstObject()
177   set aliasDefaultLocation to (ocidDesktopDirPathURL's absoluteURL()) as alias
178   try
179      tell application "SystemUIServer"
180         activate
181         set aliasDirPath to (choose folder strMsg with prompt strPrompt default location aliasDefaultLocation with invisibles and showing package contents without multiple selections allowed) as alias
182      end tell
183   on error strErrMsg number numErrNo
184      log strErrMsg & numErrNo
185      return false
186   end try
187   return aliasDirPath
188end doSaveDir
189
190########################
191#ダイアログ
192to doDialog(argString)
193   set strDefaultAnswer to argString as text
194   set strIconFilePath to ("/System/Library/UserNotifications/Bundles/com.apple.identityservicesd.firewall.bundle/Contents/Resources/Apple Care_Mac.icns") as text
195   set aliasIconFilePath to (POSIX file strIconFilePath) as alias
196   set strMsg to ("テキストの戻り値です" & return & "コピーできます") as text
197   set strTitle to ("処理の戻り値です") as text
198   set strOK to ("ファイルに保存する") as text
199   set strCancel to ("キャンセル") as text
200   set strCopyToClipboard to ("クリップボードにコピー") as text
201   set listBotton to {strCopyToClipboard, strCancel, strOK} as list
202   try
203      tell application "System Events"
204         activate
205         set recordResult to (display dialog strMsg with title strTitle default answer strDefaultAnswer buttons listBotton default button strCopyToClipboard cancel button strCancel with icon aliasIconFilePath giving up after 20 without hidden answer) as record
206      end tell
207   on error strErrMsg number numErrNo
208      log strErrMsg & numErrNo
209      return false
210   end try
211   set boolGaveUp to (gave up of recordResult) as boolean
212   if boolGaveUp is true then
213      log "時間切れ"
214      return false
215   end if
216   #戻り値
217   set strResponseButton to (button returned of recordResult) as text
218   set strResponseText to (text returned of recordResult) as text
219   #ファイルに保存の場合
220   if strResponseButton is strOK then
221      set boolDone to doSaveFile(argString)
222   end if
223   #クリップボードに格納の場合
224   if strResponseButton is strCopyToClipboard then
225      set boolDone to doCopyPasteboard(argString)
226   end if
227   return boolDone
228end doDialog
229
230########################
231#ファイルに保存
232to doSaveFile(argString)
233   set strResponseText to argString as text
234   #保存先はデスクトップ
235   set appFileManager to refMe's NSFileManager's defaultManager()
236   set ocidURLsArray to (appFileManager's URLsForDirectory:(refMe's NSDesktopDirectory) inDomains:(refMe's NSUserDomainMask))
237   set ocidDesktopDirPathURL to ocidURLsArray's firstObject()
238   set aliasDesktopDirPath to (ocidDesktopDirPathURL's absoluteURL()) as alias
239   #
240   set strDateNo to doGetDateNo("yyyyMMdd_hhmm") as text
241   set strBaseFileName to ("コマンド戻り値-" & strDateNo & "") as text
242   set strSaveExtension to ("txt") as text
243   set strDefaultFileName to ("" & strBaseFileName & "." & strSaveExtension & "") as text
244   set strMsg to ("名前を決めてください" & return & "拡張子は" & strSaveExtension & "です") as text
245   set strPromptText to "名前を決めてください" as text
246   #ファイル名 ダイアログ
247   tell application "SystemUIServer"
248      activate
249      set aliasSaveFilePath to (choose file name strMsg default location aliasDesktopDirPath default name strDefaultFileName with prompt strPromptText) as «class furl»
250   end tell
251   #出力パス
252   set strSaveFilePath to (POSIX path of aliasSaveFilePath) as text
253   set ocidSaveFilePathStr to refMe's NSString's stringWithString:(strSaveFilePath)
254   set ocidSaveFilePath to ocidSaveFilePathStr's stringByStandardizingPath()
255   set ocidSaveFilePathURL to (refMe's NSURL's alloc()'s initFileURLWithPath:(ocidSaveFilePath) isDirectory:false)
256   set ocidContainerDirPathURL to ocidSaveFilePathURL's URLByDeletingLastPathComponent()
257   #拡張子
258   set strExtension to (ocidSaveFilePathURL's pathExtension()) as text
259   #最後のアイテムがファイル名
260   set strFileName to (ocidSaveFilePathURL's lastPathComponent()) as text
261   #拡張子のつけ忘れ対策
262   if strFileName does not contain strSaveExtension then
263      set strFileName to (strFileName & "." & strSaveExtension) as text
264      set ocidSaveFilePathURL to ocidContainerDirPathURL's URLByAppendingPathComponent:(strFileName)
265   end if
266   #保存処理 改行をUNIXに強制
267   set ocidSaveString to refMe's NSMutableString's stringWithString:(strResponseText)
268   set ocidSaveString to (ocidSaveString's stringByReplacingOccurrencesOfString:(return & linefeed) withString:(linefeed))
269   set ocidSaveString to (ocidSaveString's stringByReplacingOccurrencesOfString:(return) withString:(linefeed))
270   set ocidSaveString to (ocidSaveString's stringByReplacingOccurrencesOfString:(linefeed & linefeed) withString:(linefeed))
271   set listDone to ocidSaveString's writeToURL:(ocidSaveFilePathURL) atomically:(true) encoding:(refMe's NSUTF8StringEncoding) |error|:(reference)
272   return (first item of listDone) as boolean
273end doSaveFile
274
275########################
276#クリップボードに格納
277to doCopyPasteboard(argString)
278   set strString to argString as text
279   #ペーストボードに格納する
280   set appPasteboard to refMe's NSPasteboard's generalPasteboard()
281   set ocidText to (refMe's NSString's stringWithString:(strString))
282   appPasteboard's clearContents()
283   set boolDone to appPasteboard's setString:(ocidText) forType:(refMe's NSPasteboardTypeString)
284   return boolDone
285end doCopyPasteboard
286
287
288########################
289#日付情報の取得
290to doGetDateNo(argFormatStrings)
291   set ocidDate to refMe's NSDate's |date|()
292   set ocidNSDateFormatter to refMe's NSDateFormatter's alloc()'s init()
293   ocidNSDateFormatter's setLocale:(refMe's NSLocale's localeWithLocaleIdentifier:"ja_JP_POSIX")
294   ocidNSDateFormatter's setDateFormat:(argFormatStrings)
295   set ocidDateAndTime to ocidNSDateFormatter's stringFromDate:(ocidDate)
296   set strDateAndTime to ocidDateAndTime as text
297   return strDateAndTime
298end doGetDateNo
AppleScriptで生成しました