Applescript非常新,并寻求一些帮助。我已经有了一个脚本,可以将此文件名末尾的数字RM5_CA_0_4C_288.tif增加一个,效果很好。但有时我需要将该字符串增加1,但它在最后一组数字之前或之后有字母(即_A288.tif或_288A.tif)。如何在字母之前或之后将数字增加1?
我的脚本将文件名复制到剪贴板,将其增加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 '\"_'"
答案 0 :(得分:0)
这是一个解决方案。使用文本项分隔符是抓取最后一个字符串部分的好方法,但由于我建议使用do shell脚本来调用正则表达式来匹配字符串部分,因此对于每个部分也可以这样做。
set oldName to "RM5_CA_0_4C_288.tif"
-- get everything up through the last _
set firstPart to do shell script "echo " & quoted form of oldName & " | grep -o '\\w*_'"
-- get everything from the last _
set lastPart to do shell script "echo " & quoted form of oldName & " | grep -o '_[a-zA-Z]*[0-9]*[a-zA-Z]*\\.tif'"
-- get the simplified string in the lastPart
set lastPart to text 2 thru -5 of lastPart
try -- check for leading letters
set preLetters to do shell script "echo " & quoted form of lastPart & " | grep -o '^[a-zA-Z]'"
set theNumber to do shell script "echo " & quoted form of lastPart & " | grep -o '[0-9]*$'"
set postLetters to ""
on error
try -- check for trailing letters
set postLetters to do shell script "echo " & quoted form of lastPart & " | grep -o '[a-zA-Z]$'"
set theNumber to do shell script "echo " & quoted form of lastPart & " | grep -o '^[0-9]*'"
set preLetters to ""
on error -- has no letters
set theNumber to lastPart
set preLetters to ""
set postLetters to ""
end try
end try
-- increment the number string
set newNumber to theNumber + 1
-- put humpty dumpty back together again
set newName to firstPart & preLetters & newNumber & postLetters & ".tif"
答案 1 :(得分:-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