| 001 | #!/usr/bin/env osascript |
| 002 | #coding: utf-8 |
| 003 | ----+----1----+----2----+-----3----+----4----+----5----+----6----+----7-- |
| 004 | (* |
| 005 |
|
| 006 | mutoolのコマンドラインぺルパ |
| 007 |
|
| 008 | mutoolのmake方法はこちら |
| 009 | https://note.com/quicktimer/n/nbec6b6e72677 |
| 010 |
|
| 011 |
|
| 012 | v1初回作成 |
| 013 |
|
| 014 |
|
| 015 | com.cocolog-nifty.quicktimer.icefloe *) |
| 016 | ----+----1----+----2----+-----3----+----4----+----5----+----6----+----7-- |
| 017 | use AppleScript version "2.8" |
| 018 | use framework "Foundation" |
| 019 | use framework "AppKit" |
| 020 | use framework "UniformTypeIdentifiers" |
| 021 | use scripting additions |
| 022 |
|
| 023 | property refMe : a reference to current application |
| 024 |
|
| 025 | ######################## |
| 026 | #設定項目 mutoolへのパス |
| 027 | property strBinPath : ("~/Library/Application Support/bin/mupdf/mutool") as text |
| 028 |
|
| 029 | ######################## |
| 030 | # RUN |
| 031 | # on run {listAliasFilePath} |
| 032 |
|
| 033 | on 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 |
| 057 | end run |
| 058 |
|
| 059 | ######################## |
| 060 | # OPEN |
| 061 | on 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 |
| 077 | end open |
| 078 |
|
| 079 | ######################## |
| 080 | #コマンド整形実行 |
| 081 | to doExecCmd(ocidBinFilePath, aliasItemFilePath) |
| 082 | #エイリアス確定させてからのUNIXパス |
| 083 | set aliasFilePath to aliasItemFilePath as alias |
| 084 | set strFilePath to (POSIX path of aliasFilePath) as text |
| 085 | set ocidFilePathStr to refMe's NSString's stringWithString:(strFilePath) |
| 086 | set ocidFilePath to ocidFilePathStr's stringByStandardizingPath() |
| 087 | # |
| 088 | set ocidBaseFilePath to ocidFilePath's stringByDeletingPathExtension() |
| 089 | set ocidSaveFilePath to ocidBaseFilePath's stringByAppendingPathExtension:("圧縮済.pdf") |
| 090 | # |
| 091 | set ocidFilePath to doPathEscape(ocidFilePath) |
| 092 | set ocidSaveFilePath to doPathEscape(ocidSaveFilePath) |
| 093 | |
| 094 | #コマンド整形して実行 |
| 095 | set strCmd to ("\\\"" & ocidBinFilePath & "\\\" clean -gggg -z -f -i -Z -e 100 -t \\\"" & ocidFilePath & "\\\" \\\"" & ocidSaveFilePath & "\\\"") as text |
| 096 | set boolDone to doExecZsh(strCmd) |
| 097 | return true |
| 098 | end doExecCmd |
| 099 |
|
| 100 | ######################## |
| 101 | #パスのエスケープ |
| 102 | to 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 |
| 112 | end doPathEscape |
| 113 |
|
| 114 | ######################## |
| 115 | #コマンド実行 |
| 116 | to 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 |
| 130 | end doExecZsh |
| 131 |
|
| 132 | ######################## |
| 133 | #保存先指定 |
| 134 | to 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 |
| 151 | end doSaveDir |
| 152 |
|
| 153 | ######################## |
| 154 | #ダイアログ |
| 155 | to 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 |
| 191 | end doDialog |
| 192 |
|
| 193 | ######################## |
| 194 | #ファイルに保存 |
| 195 | to 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 |
| 236 | end doSaveFile |
| 237 |
|
| 238 | ######################## |
| 239 | #クリップボードに格納 |
| 240 | to 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 |
| 248 | end doCopyPasteboard |
| 249 |
|
| 250 |
|
| 251 | ######################## |
| 252 | #日付情報の取得 |
| 253 | to 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 |
| 261 | end doGetDateNo |