20260621

【Applescripts】『do shell scrip』スクリプト内でのコマンド実行とパスのエスケープ(ZSH呼び出し編)

【Applescripts】『do shell scrip』スクリプト内でのコマンド実行とパスのエスケープ(ZSH呼び出し編)

NOTE記事一覧ですnote.com
 

【Safari・FireFox用Script Editorで開く】 |

Zsh用パスのエスケープAS.scpt.scpt
ソース
001#!/usr/bin/env osascript
002#coding: utf-8
003----+----1----+----2----+----3----+----4----+----5----+----6----+----7--
004(*
005do shell script でzsh呼び出し時(ダブルクオト)の場合の
006パスのエスケープ処理
007見本
008
009
010
011com.cocolog-nifty.quicktimer.icefloe *)
012----+----1----+----2----+----3----+----4----+----5----+----6----+----7--
013use AppleScript version "2.8"
014use scripting additions
015
016
017#入力画像ファイルパス UNIX形式
018#テンポラリー(起動時に自動削除される)フォルダに
019#ファイルパス中のエスケープで考慮が必要な文字全部入れのフォルダ名をつけたパス
020set 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#エスケープ処理をして
024set strFilePath to doPathEscape(strFilePath)
025
026#コマンドを実行する
027set strSTDOUT to (do shell script "/bin/zsh -c \"/bin/mkdir -p  \\\"" & strFilePath & "\\\"\"") as text
028
029
030return strFilePath
031
032########################
033#【ZSH用】パスのエスケープ
034to 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
048end doPathEscape
049########################
050#置換
051to 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
059end doReplace
AppleScriptで生成しました