20260406

[mutool]コマンドライン実行用へルパ traceレンダリング情報


[mutool]コマンドライン実行用へルパ traceレンダリング情報

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

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

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