20260406

[mutool]コマンドライン実行用へルパ  info情報表示


[mutool]コマンドライン実行用へルパ [mutool]info情報表示

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

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

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