20260406

[mutool]コマンドライン実行用へルパ bake注釈統合


[mutool]コマンドライン実行用へルパ bake注釈統合

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

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

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