20260406

[mutool]コマンドライン実行用へルパ convert変換


[mutool]コマンドライン実行用へルパ clean圧縮

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

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

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