20260406

[mutool]コマンドライン実行用へルパ extract書き出し


[mutool]コマンドライン実行用へルパ [mutool]extract書き出し

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

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

[mutool]extract書き出し.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   
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   #ドロップ(OPEN)されたエイリアスリストを順番に処理
065   repeat with itemAliasFilePath in listAliasFilePath
066      set aliasItemFilePath to itemAliasFilePath as alias
067      set recordInfoFor to (info for aliasItemFilePath) as record
068      set strItemUTI to (type identifier of recordInfoFor) as text
069      #PDFファイルのみ次工程に回す
070      if strItemUTI is "com.adobe.pdf" then
071         set boolDone to doExecCmd(ocidBinFilePath, aliasItemFilePath)
072         
073      end if
074   end repeat
075   return true
076end open
077
078########################
079#コマンド整形実行
080to doExecCmd(ocidBinFilePath, aliasItemFilePath)
081   #エイリアス確定させてからのUNIXパス
082   set aliasFilePath to aliasItemFilePath as alias
083   set strFilePath to (POSIX path of aliasFilePath) as text
084   set ocidFilePath to doPathEscape(strFilePath)
085   #保存先
086   set aliasSaveDirPath to doSaveDir()
087   set strSaveDirPath to (POSIX path of aliasSaveDirPath) as text
088   #コマンド整形して実行
089   set strCmd to ("pushd \\\"" & strSaveDirPath & "\\\";\\\"" & ocidBinFilePath & "\\\" extract \\\"" & ocidFilePath & "\\\"") as text
090   set boolDone to doExecZsh(strCmd)
091   return true
092end doExecCmd
093
094########################
095#パスのエスケープ
096to doPathEscape(strFilePath)
097   set ocidFilePathStr to refMe's NSString's stringWithString:(strFilePath)
098   set ocidFilePath to ocidFilePathStr's stringByStandardizingPath()
099   set ocidFilePathURL to refMe's NSURL's fileURLWithPath:(ocidFilePath) isDirectory:(false)
100   set listEscChar to {"\\", "\"", "$", "`"} as list
101   repeat with itemEscChar in listEscChar
102      set ocidFilePath to (ocidFilePath's stringByReplacingOccurrencesOfString:(itemEscChar) withString:("\\\\\\" & itemEscChar & ""))
103   end repeat
104   set ocidFilePath to (ocidFilePath's stringByReplacingOccurrencesOfString:("!") withString:("\\\"'!'\\\""))
105   return ocidFilePath
106end doPathEscape
107
108########################
109#コマンド実行
110to doExecZsh(argCmdText)
111   set strCmdText to argCmdText as text
112   set strExecZsh to ("/bin/zsh -c \"" & strCmdText & "\"") as text
113   try
114      log "コマンド開始"
115      set strStdOut to (do shell script strExecZsh) as text
116      log "コマンド終了"
117   on error strErroMsg number numErrorNo
118      return false
119   end try
120   if strStdOut is not "" then
121      #      set boolDone to doDialog(strStdOut)
122   end if
123   return true
124end doExecZsh
125
126########################
127#保存先指定
128to doSaveDir()
129   set strMsg to ("保存先を指定してください" & return & "ファイル数が多くなることもありますので" & return & "新規フォルダを指定推奨") as text
130   set strPrompt to ("保存先を指定してください" & return & "ファイル数が多くなることもありますので" & return & "新規フォルダを指定推奨") as text
131   set appFileManager to refMe's NSFileManager's defaultManager()
132   set ocidURLsArray to (appFileManager's URLsForDirectory:(refMe's NSDesktopDirectory) inDomains:(refMe's NSUserDomainMask))
133   set ocidDesktopDirPathURL to ocidURLsArray's firstObject()
134   set aliasDefaultLocation to (ocidDesktopDirPathURL's absoluteURL()) as alias
135   try
136      tell application "SystemUIServer"
137         activate
138         set aliasDirPath to (choose folder strMsg with prompt strPrompt default location aliasDefaultLocation with invisibles and showing package contents without multiple selections allowed) as alias
139      end tell
140   on error strErrMsg number numErrNo
141      log strErrMsg & numErrNo
142      return false
143   end try
144   return aliasDirPath
145end doSaveDir
146
147########################
148#ダイアログ
149to doDialog(argString)
150   set strDefaultAnswer to argString as text
151   set strIconFilePath to ("/System/Library/UserNotifications/Bundles/com.apple.identityservicesd.firewall.bundle/Contents/Resources/Apple Care_Mac.icns") as text
152   set aliasIconFilePath to (POSIX file strIconFilePath) as alias
153   set strMsg to ("テキストの戻り値です" & return & "コピーできます") as text
154   set strTitle to ("処理の戻り値です") as text
155   set strOK to ("ファイルに保存する") as text
156   set strCancel to ("キャンセル") as text
157   set strCopyToClipboard to ("クリップボードにコピー") as text
158   set listBotton to {strCopyToClipboard, strCancel, strOK} as list
159   try
160      tell application "System Events"
161         activate
162         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
163      end tell
164   on error strErrMsg number numErrNo
165      log strErrMsg & numErrNo
166      return false
167   end try
168   set boolGaveUp to (gave up of recordResult) as boolean
169   if boolGaveUp is true then
170      log "時間切れ"
171      return false
172   end if
173   #戻り値
174   set strResponseButton to (button returned of recordResult) as text
175   set strResponseText to (text returned of recordResult) as text
176   #ファイルに保存の場合
177   if strResponseButton is strOK then
178      set boolDone to doSaveFile(argString)
179   end if
180   #クリップボードに格納の場合
181   if strResponseButton is strCopyToClipboard then
182      set boolDone to doCopyPasteboard(argString)
183   end if
184   return boolDone
185end doDialog
186
187########################
188#ファイルに保存
189to doSaveFile(argString)
190   set strResponseText to argString as text
191   #保存先はデスクトップ
192   set appFileManager to refMe's NSFileManager's defaultManager()
193   set ocidURLsArray to (appFileManager's URLsForDirectory:(refMe's NSDesktopDirectory) inDomains:(refMe's NSUserDomainMask))
194   set ocidDesktopDirPathURL to ocidURLsArray's firstObject()
195   set aliasDesktopDirPath to (ocidDesktopDirPathURL's absoluteURL()) as alias
196   #
197   set strDateNo to doGetDateNo("yyyyMMdd_hhmm") as text
198   set strBaseFileName to ("コマンド戻り値-" & strDateNo & "") as text
199   set strSaveExtension to ("txt") as text
200   set strDefaultFileName to ("" & strBaseFileName & "." & strSaveExtension & "") as text
201   set strMsg to ("名前を決めてください" & return & "拡張子は" & strSaveExtension & "です") as text
202   set strPromptText to "名前を決めてください" as text
203   #ファイル名 ダイアログ
204   tell application "SystemUIServer"
205      activate
206      set aliasSaveFilePath to (choose file name strMsg default location aliasDesktopDirPath default name strDefaultFileName with prompt strPromptText) as «class furl»
207   end tell
208   #出力パス
209   set strSaveFilePath to (POSIX path of aliasSaveFilePath) as text
210   set ocidSaveFilePathStr to refMe's NSString's stringWithString:(strSaveFilePath)
211   set ocidSaveFilePath to ocidSaveFilePathStr's stringByStandardizingPath()
212   set ocidSaveFilePathURL to (refMe's NSURL's alloc()'s initFileURLWithPath:(ocidSaveFilePath) isDirectory:false)
213   set ocidContainerDirPathURL to ocidSaveFilePathURL's URLByDeletingLastPathComponent()
214   #拡張子
215   set strExtension to (ocidSaveFilePathURL's pathExtension()) as text
216   #最後のアイテムがファイル名
217   set strFileName to (ocidSaveFilePathURL's lastPathComponent()) as text
218   #拡張子のつけ忘れ対策
219   if strFileName does not contain strSaveExtension then
220      set strFileName to (strFileName & "." & strSaveExtension) as text
221      set ocidSaveFilePathURL to ocidContainerDirPathURL's URLByAppendingPathComponent:(strFileName)
222   end if
223   #保存処理 改行をUNIXに強制
224   set ocidSaveString to refMe's NSMutableString's stringWithString:(strResponseText)
225   set ocidSaveString to (ocidSaveString's stringByReplacingOccurrencesOfString:(return & linefeed) withString:(linefeed))
226   set ocidSaveString to (ocidSaveString's stringByReplacingOccurrencesOfString:(return) withString:(linefeed))
227   set ocidSaveString to (ocidSaveString's stringByReplacingOccurrencesOfString:(linefeed & linefeed) withString:(linefeed))
228   set listDone to ocidSaveString's writeToURL:(ocidSaveFilePathURL) atomically:(true) encoding:(refMe's NSUTF8StringEncoding) |error|:(reference)
229   return (first item of listDone) as boolean
230end doSaveFile
231
232########################
233#クリップボードに格納
234to doCopyPasteboard(argString)
235   set strString to argString as text
236   #ペーストボードに格納する
237   set appPasteboard to refMe's NSPasteboard's generalPasteboard()
238   set ocidText to (refMe's NSString's stringWithString:(strString))
239   appPasteboard's clearContents()
240   set boolDone to appPasteboard's setString:(ocidText) forType:(refMe's NSPasteboardTypeString)
241   return boolDone
242end doCopyPasteboard
243
244
245########################
246#日付情報の取得
247to doGetDateNo(argFormatStrings)
248   set ocidDate to refMe's NSDate's |date|()
249   set ocidNSDateFormatter to refMe's NSDateFormatter's alloc()'s init()
250   ocidNSDateFormatter's setLocale:(refMe's NSLocale's localeWithLocaleIdentifier:"ja_JP_POSIX")
251   ocidNSDateFormatter's setDateFormat:(argFormatStrings)
252   set ocidDateAndTime to ocidNSDateFormatter's stringFromDate:(ocidDate)
253   set strDateAndTime to ocidDateAndTime as text
254   return strDateAndTime
255end doGetDateNo
AppleScriptで生成しました