【问题标题】:Increase number in a string by one将字符串中的数字加一
【发布时间】: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


【解决方案1】:

这里有一个解决方案。使用文本项分隔符是获取最后一个字符串部分的好方法,但由于我建议使用 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"

【讨论】:

  • 我已经更新了这个脚本的一部分。我遇到了包含小数点的文件名 (2229_CA_0_79.1_111.tif)。当我运行上面写的脚本时,它删除了我需要保持完整的部分文件名(2229_CA_0_79.1_111.tif > 2229_CA_0_1_112.tif)。为了解决这个问题,我更改了 grep 命令。我也将原始文件名复制到剪贴板。将 oldName 设置为剪贴板 设置 firstPart 以执行 shell 脚本 "echo " & oldName & " | grep -o '\\w.*_'" 的引用形式
【解决方案2】:

原脚本加一:

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

【讨论】:

    猜你喜欢
    • 2013-08-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-17
    • 2016-03-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多