2017-09-26 92 views
1

我试图检查一个准确的计数值,但我遇到了步进过程的问题。鉴于提取:在AppleScript中通过虚线递增?

1-1 
2-1 
3-1 
4-1 
5-1 
6-1 
7-1 
7-2 
8-1 
9-1 
9-2 
9-3 
9-4 
10-1 
11-1 
12-2 ## intentionally left out 12-1 to throw error 
13-1 

如何正确增加通过列表和标志,如果有人失踪。当我运行我的脚本时,它运行通过7-2,但当涉及到8-1它失败:

子标记似乎缺少。

代码:

tell application "BBEdit" 
    activate 
    set parentCount to 1 
    set childCount to 1 
    set theDoc to text document 1 
    select insertion point before first character of theDoc 
    set searchOpt to {search mode:grep, wrap around:false} 
    repeat 
     set theNumbers to find "^(\\d{1,4})-(\\d{1,4})" searching in text 1 of theDoc options searchOpt with selecting match 
     if not found of theNumbers then exit repeat 
     set parentNum to (grep substitution of "\\1") as number 
     set childNum to (grep substitution of "\\2") as number 
     if parentNum is equal to parentCount and childNum is equal to childCount then 
      set parentCount to parentCount + 1 
     else if parentNum is equal to parentCount and (childNum + 1) is equal to childCount then 
      set parentCount to parentCount + 1 
      set childCount to 1 
     else 
      display dialog "missing marker" 
     end if 
    end repeat 
    display dialog "completed check" 
end tell 

在AppleScript的我怎样才能正确地通过一系列数字递增?

回答

0

在你的条件语句,您可以使用相同的条件if parentNum is equal to parentCount,第二个条件语句不起作用,因为脚本增加parentCount在这两个条件语句,和你的脚本始终不会增加childCount。

使用该脚本:

tell application "BBEdit" 
    activate 
    set parentCount to 0 
    set childCount to 1 
    set theDoc to text document 1 
    select insertion point before first character of theDoc 
    set searchOpt to {search mode:grep, wrap around:false} 
    repeat 
     set theNumbers to find "^(\\d{1,4})-(\\d{1,4})" searching in text 1 of theDoc options searchOpt with selecting match 
     if not found of theNumbers then exit repeat 
     set parentNum to (grep substitution of "\\1") as number 
     set childNum to (grep substitution of "\\2") as number 
     if parentNum = (parentCount + 1) and childNum = 1 then -- if the parentNum increase of 1, the childNum must be 1 
      set parentCount to parentCount + 1 
      set childCount to 1 -- so, reset the childCount to 1 
     else if parentNum = parentCount and childNum = (childCount + 1) then 
      set childCount to childNum 
     else 
      display dialog "missing marker" 
      set parentCount to parentNum -- start at this value for the next sequence 
      set childCount to childNum -- start at this value for the next sequence 
     end if 
    end repeat 
    display dialog "completed check" 
end tell