2017-10-05 2 views
0

내 AppleScript 기능을 향상시키고 싶고 내 repeat의 사용을 늘리고 싶습니다. 어떻게 콘텐츠를 조작하기 위해 텍스트 파일을 단계적으로 실행할 수 있는지 궁금했지만 다른 이름을 돌릴 때 루프를 깨뜨리기 위해 무엇을 전달해야할지 막혔습니다. .별도의 조건이 충족 될 때 어떻게 반복을 중단 할 수 있습니까?

다음 텍스트 파일의 내용을 감안할 때 :

Bob 
- bacon 
- eggs 
- waffle 
Bacon ipsum dolor amet bresaola andouille flank, meatloaf salami biltong brisket. 
James 
- sausage 
- omelet 
Sausage short ribs sirloin landjaeger, biltong swine kielbasa prosciutto. 
Bresaola landjaeger jowl alcatra. 
Michael 
- scrambled eggs 
- cereal 
- toast 
Sam 
- sausage 
- pancakes 
내가 닮은 내 출력을 원하는

:

Bob 
    1. Bob likes bacon 
    2. Bob likes eggs 
    3. Bob likes waffle 
Bacon ipsum dolor amet bresaola andouille flank, meatloaf salami biltong brisket. 
James 
    1. James likes sausage 
    2. James likes omelet 
Sausage short ribs sirloin landjaeger, biltong swine kielbasa prosciutto. 
Bresaola landjaeger jowl alcatra. 
Michael 
    1. Michael likes scrambled eggs 
    2. Michael likes cereal 
    3. Michael likes toast 
Sam 
    1. Sam likes sausage 
    2. Sam likes pancakes 

을하지만 때이 이름을 찾을 수있어 텍스트를 통해 내가 스크립트와 점선 항목이 있지만 다른 이름이있을 때 깨는 방법을 알아 내려고 문제가 있습니다.

코드 :

tell application "BBEdit" 
    activate 
    set testCase to "repeatBreakfast.txt" 
    select insertion point before first character of text document testCase 
    set foodCount to 1 
    repeat 
     set theCustomer to find "^([A-Za-z]*)$" searching in text 1 of text document testCase options {search mode:grep, wrap around:false} with selecting match 
     if theCustomer is not found then 
      exit repeat 
     else 
      set theName to (grep substitution of "\\1") as string 
      repeat 
       set breakfastItem to find "^- (?=\\D)" searching in text 1 of text document testCase options {search mode:grep, wrap around:false} with selecting match 
       if breakfastItem is not found then 
        exit repeat 
       else 
        replace selection using ("\\t" & foodCount & ". " & theName & " likes ") 
        set foodCount to foodCount + 1 
       end if 
      end repeat 
     end if 
    end repeat 
end tell 

내가 얻을 :

Bob 
    1. Bob likes Bob likes bacon 
    2. Bob likes Bob likes eggs 
    3. Bob likes Bob likes waffle 
Bacon ipsum dolor amet bresaola andouille flank, meatloaf salami biltong brisket. 
James 
    4. Bob likes James likes sausage 
    5. Bob likes James likes omelet 
Sausage short ribs sirloin landjaeger, biltong swine kielbasa prosciutto. 
Bresaola landjaeger jowl alcatra. 
Michael 
    6. Bob likes Michael scrambled eggs 
    7. Bob likes Michael cereal 
    8. Bob likes Michael toast 
Sam 
    9. Bob likes sausage 
    10. Bob likes pancakes 

하지만 다음 이름에 대한 조건을 삽입하려고하면

if theCustomer is found then 
    set foodCount to 1 
    exit repeat 
end if 

를 내가 얻을 :

애플 스크립트에서
Bob 
    1. Bob likes Bob likes bacon 
- Bob likes eggs 
- Bob likes waffle 
Bacon ipsum dolor amet bresaola andouille flank, meatloaf salami biltong brisket. 
James 
    1. James likes James likes sausage 
- James likes omelet 
Sausage short ribs sirloin landjaeger, biltong swine kielbasa prosciutto. 
Bresaola landjaeger jowl alcatra. 
Michael 
    1. Michael likes Michael scrambled eggs 
- Michael cereal 
- Michael toast 
Sam 
    1. Sam likes sausage 
- pancakes 

및 BBEdit 내 리피터에서 텍스트의 이름을 올바르게 지정하고 증분하는 방법 루프?

답변

0

솔루션을 사용하면 스크립트는 한 번에 하나씩 모든 고객을 확보해야합니다.

그런 다음 두 고객 간의 회선을 사용하여 아침 식사 품목을 찾습니다.

tell application "BBEdit" 
    activate 
    set testCase to "repeatBreakfast.txt" 
    set r to find "^([A-Za-z]*)$" searching in text document testCase options {search mode:grep, wrap around:false, starting at top:true, returning results:true, showing results:false} 
    if r is found then 
     set allCustomers to found matches of r 
     set theCount to count allCustomers 
     repeat with i from 1 to theCount 
      set theCustomer to item i of allCustomers 
      set theName to match_string of theCustomer 
      set lineOfThisCustomer to result_line of theCustomer 
      if i < theCount then 
       set lastLineOfThisCustomer to (result_line of item (i + 1) of allCustomers) - 1 -- the line before the next customer 
      else 
       set lastLineOfThisCustomer to -1 -- it's the last customer, so use the last line of the document 
      end if 

      set foodCount to 1 
      repeat 
       -- ** search between this customer and the next customer ** 
       set breakfastItem to find "^- (?=\\D)" searching in (lines lineOfThisCustomer thru lastLineOfThisCustomer of text document testCase) options {search mode:grep, wrap around:false} with selecting match 
       if breakfastItem is not found then exit repeat 
       replace selection using ("\\t" & foodCount & ". " & theName & " likes ") 
       set foodCount to foodCount + 1 
      end repeat 
     end repeat 
    end if 
end tell 

또 다른 해결책

두 고객을 얻을 명령을 찾아 사용합니다.

그런 다음이 두 고객 간의 회선을 사용하여 아침 식사 항목을 찾으십시오.

tell application "BBEdit" 
    activate 
    set testCase to "repeatBreakfast.txt" 
    select insertion point before first character of text document testCase 

    set theCustomer to find "^([A-Za-z]*)$" searching in text document testCase options {search mode:grep, wrap around:false} with selecting match 
    if theCustomer is found then 
     set theName to (grep substitution of "\\1") as string 
     repeat 
      set lineOfThisCustomer to startLine of found object of theCustomer 

      set theNextCustomer to find "^([A-Za-z]*)$" searching in text document testCase options {search mode:grep, wrap around:false} with selecting match 
      if theNextCustomer is found then 
       set lastLineOfThisCustomer to (startLine of found object of theNextCustomer) - 1 -- the line before the next customer 
       set theName2 to (grep substitution of "\\1") as string 
      else 
       set lastLineOfThisCustomer to -1 -- no other customer, so use the last line of the document 
      end if 

      set foodCount to 1 
      repeat 
       -- ** search between this customer and the next customer ** 
       set breakfastItem to find "^- (?=\\D)" searching in (lines lineOfThisCustomer thru lastLineOfThisCustomer of text document testCase) options {search mode:grep, wrap around:false} with selecting match 
       if breakfastItem is not found then exit repeat 
       replace selection using ("\\t" & foodCount & ". " & theName & " likes ") 
       set foodCount to foodCount + 1 
      end repeat 
      if theNextCustomer is not found then exit repeat 
      set theCustomer to theNextCustomer 
      set theName to theName2 
     end repeat 
    end if 
end tell 
관련 문제