20260406

[mutool]コマンドライン実行用へルパ auditチェック


[mutool]コマンドライン実行用へルパ auditチェック

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

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

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