0

次のコマンドラインがターミナルでうまく機能します

's/\d+/sprintf("%04d",$&)/e' の名前を変更 ~/Downloads/test/*.pdf

しかし、applescript で引用符 (スクリプトに一重引用符とバックスラッシュを含める) をエスケープできませんでした。

 set renamer_command to "'s/\\d+/sprintf(\"%04d\",$&)/e'"
 do shell script "/opt/local/bin/rename " & quoted form of renamer_command & " ~/Downloads/test/*.pdf"

これは私がApplescriptから得た出力です

 tell current application
do shell script "/opt/local/bin/rename ''\\''s/\\d+/sprintf(\"%04d\",$&)/e'\\''' ~/Downloads/test/*.pdf"
    --> ""
 end tell
 Result:
""

これは、出力example.txtに見られるように、私が望むものを正確に提供します

 set inString to "'s/\\d+/sprintf(\"%04d\",$&)/e'"
 do shell script "echo " & quoted form of inString & " > ~/Desktop/example.txt"

結果

 's/\d+/sprintf("%04d",$&)/e'

繰り返しますが、スクリプトに出力をドロップして、applescript で結果を確認すると、結果にバックスラッシュが表示されます。

 set inString to "'s/\\d+/sprintf(\"%04d\",$&)/e'"
 do shell script "echo " & quoted form of inString

結果

 "'s/\\d+/sprintf(\"%04d\",$&)/e'"

私は多くのことを回避しましたが、動作するスクリプトを取得できませんでした。解決策を提案してください、ありがとう

4

1 に答える 1

1

引用符が多すぎます。これを試して:

set inString to "s/\\d+/sprintf(\"%04d\",$&)/e"
do shell script "/opt/local/bin/rename " & quoted form of inString & " ~/Downloads/test/*.pdf"
于 2013-09-16T14:32:26.237 に答える