
【Applescripts】『do shell scrip』スクリプト内でのコマンド実行とパスのエスケープ(ZSH呼び出し編)
【Safari・FireFox用Script Editorで開く】 |
| ソース | |
|---|---|
| 001 | #!/usr/bin/env osascript |
| 002 | #coding: utf-8 |
| 003 | ----+----1----+----2----+----3----+----4----+----5----+----6----+----7-- |
| 004 | (* |
| 005 | do shell script でzsh呼び出し時(ダブルクオト)の場合の |
| 006 | パスのエスケープ処理 |
| 007 | 見本 |
| 008 | |
| 009 | |
| 010 | |
| 011 | com.cocolog-nifty.quicktimer.icefloe *) |
| 012 | ----+----1----+----2----+----3----+----4----+----5----+----6----+----7-- |
| 013 | use AppleScript version "2.8" |
| 014 | use scripting additions |
| 015 | |
| 016 | |
| 017 | #入力画像ファイルパス UNIX形式 |
| 018 | #テンポラリー(起動時に自動削除される)フォルダに |
| 019 | #ファイルパス中のエスケープで考慮が必要な文字全部入れのフォルダ名をつけたパス |
| 020 | set strFilePath to ("/tmp/A!B\"C#D$E&F'G(H)I*J;K<L>M?N[O]P\\Q^R`S{T|U}V~W") as text |
| 021 | |
| 022 | |
| 023 | #エスケープ処理をして |
| 024 | set strFilePath to doPathEscape(strFilePath) |
| 025 | |
| 026 | #コマンドを実行する |
| 027 | set strSTDOUT to (do shell script "/bin/zsh -c \"/bin/mkdir -p \\\"" & strFilePath & "\\\"\"") as text |
| 028 | |
| 029 | |
| 030 | return strFilePath |
| 031 | |
| 032 | ######################## |
| 033 | #【ZSH用】パスのエスケープ |
| 034 | to doPathEscape(strFilePath) |
| 035 | |
| 036 | set listEscChar to {"\\", "\"", "$", "`"} as list |
| 037 | repeat with itemEscChar in listEscChar |
| 038 | set strSearchText to (itemEscChar) as text |
| 039 | set strReplaceText to ("\\\\\\" & itemEscChar & "") as text |
| 040 | set strFilePath to doReplace(strFilePath, strSearchText, strReplaceText) |
| 041 | end repeat |
| 042 | |
| 043 | set strSearchText to ("!") as text |
| 044 | set strReplaceText to ("\\\"'!'\\\"") as text |
| 045 | set strFilePath to doReplace(strFilePath, strSearchText, strReplaceText) |
| 046 | |
| 047 | return strFilePath |
| 048 | end doPathEscape |
| 049 | ######################## |
| 050 | #置換 |
| 051 | to doReplace(argOrignalText, argSearchText, argReplaceText) |
| 052 | set strDelim to AppleScript's text item delimiters |
| 053 | set AppleScript's text item delimiters to argSearchText |
| 054 | set listDelim to every text item of argOrignalText |
| 055 | set AppleScript's text item delimiters to argReplaceText |
| 056 | set strReturn to listDelim as text |
| 057 | set AppleScript's text item delimiters to strDelim |
| 058 | return strReturn |
| 059 | end doReplace |
| AppleScriptで生成しました | |
