【发布时间】:2018-04-26 12:18:17
【问题描述】:
对 Applescript 非常陌生,正在寻求帮助。我已经有一个脚本,可以将此文件名 RM5_CA_0_4C_288.tif 末尾的数字加一,而且效果很好。但有时我需要将该字符串增加 1,但它在最后一组数字之前或之后都有字母(即 _A288.tif 或 _288A.tif)。如何在数字之前或之后将数字加 1?
我的脚本将文件名复制到剪贴板,将其加一,然后将结果粘贴回剪贴板。
感谢您的帮助!
原脚本加一:
try
set oldName to the clipboard
set AppleScript's text item delimiters to {"_"}
set oldNumber to the last text item of oldName
set AppleScript's text item delimiters to {"."}
set oldNumber to the first text item of oldNumber
set newNumber to oldNumber + 1
set newNumber to newNumber & "tif" as string
set xVar to 1
set AppleScript's text item delimiters to {"_"}
set newName to ""
repeat while xVar is less than 5
set newName to newName & text item xVar of oldName & "_"
set xVar to xVar + 1
end repeat
set newName to newName & newNumber
set the clipboard to newName as string
on error tell application "Firefox" to display dialog "Error! Check the 'set AppleScript's text item delimiters to' section. Change the delimeter?" buttons ["OK"] default button 1
return
end try
正在进行的脚本以包含数字之前/之后的字母:
set oldName to the clipboard
set AppleScript's text item delimiters to {"_"}
set oldNumber to the last text item of oldName
get result
set returnedString to do shell script "echo " & quoted form of result & " | tr -d '[:upper:]'"
get result
set returnedString to do shell script "echo " & quoted form of result & " | tr -dc '[:digit:]'"
set result to result as number
set newNumber to result + 1
set newNumber to newNumber & ".tif" as string
get result
set theName to do shell script "echo " & quoted form of result & " | tr -d '\"_'"
【问题讨论】:
-
显示你的代码。并指定是否要将文件“_289A.tif”转到“_290A.tif”,或者甚至“_299A.tif”转到“_300A.tif”,是否需要“_999A.tif”添加另一个数字?
-
在您想加 1 的最后一位数字之前是否总是有一个下划线
_?另外,请显示使用您当前正在使用的代码。 -
是的。在最后一组数字之前总是有一个 _,但有时在数字之前或之后有一个字母。
-
感谢您查看此@jweaks!太有帮助了!如果我使用“UM5_CA_0_4C_A1.tif”运行这个脚本,我会得到我想要的结果,“UM5_CA_0_4C_A2.tif”。同样,如果 oldName 是“UM5_CA_0_4C_1A.tif”,我也会得到预期的结果。但是,如果 oldName 是“UM5_CA_0_4C_A1A.tif”,我会得到一个不起作用的结果。想法?
-
那是因为我根据您的要求进行了结构化。您说“它在最后一组数字之前或之后都有字母”。你没有说它可以两者兼得。您需要在此实例中取消嵌套 Try 块。 (并且,您应该评论答案,而不是问题)
标签: applescript