20260406

[mutool]コマンドライン実行用へルパ pagesページ情報


[mutool]コマンドライン実行用へルパ pagesページ情報

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

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

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