0

我有以下命令行在终端中运行良好

重命名 's/\d+/sprintf("%04d",$&)/e' ~/Downloads/test/*.pdf

但我无法在applescript中转义引号(脚本中有单引号和反斜杠),这是我尝试过的,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 回答