2014-07-07 4 views
1

AppleScript 배우기 실습을하고 있는데 .xhtml 파일에 클래스 수를 얻을 수 있는지보고 싶었습니다.AppleScript로 클래스 수를 계산하는 방법은 무엇입니까?

set this_project to file of project document 1 

과 함께 모든 .xhtml 파일을 대상으로 확인했다 : 내 BBEdit에서 프로젝트에서

내가 가진 프로젝트에 대한 변수를 설정

set total_xhtml to {search mode:grep, case sensitive:false, match words:false, extend selection:false, returning results:true, filter:{ID:"111232452", filter_mode:and_mode, filter_terms:{{operand:"xhtml", field:«constant ****FnSf», operator:op_is_equal}}}} 

하지만 난의 클래스를 계산하려고 할 때 각 파일은 내가 우연히 만났어.

나는 시도했다 :

set varCount to count class=\"foobar\"" of (this_project in total_xhtml) 

set varCount to count class=\"foobar\""을 시도하면 AppleScript 편집기에서 숫자가 반환되지만 프로젝트의 각 파일에 대한 전체 개수는 어떻게 얻을 수 있습니까?

답변

0
내 질문에 대한 답변을 받아 결코 나는이 (가) 제공 answer text item delimiters 때문에 파일에서 잘못된 카운트를 발사하고 선택하지 않은이 Q & A.을 닫고 싶어하고 내가 원한 그냥 실현

접근 방식은 do shell에 전화하지 않았습니다.

애플 스크립트 및 BBEdit에서에서

당신이 사전 FIND가 참조하는 경우 :

## beginning of file 
select insertion point before first character of text document text document 1 

set countClass to 0 

repeat 
    ## find pattern 
    set findClass to find "class=\"foobar\"" searching in text 1 of text document text document 1 options {options} with selecting match 

    ## exit after no more patterns 
    if not found of findClass then exit repeat 

    ## increment for every occurrence 
    set countClass to countClass + 1 
end repeat 

## return total count of pattern 
return countClass 

으로 :

enter image description here

내가 class="foobar"의 각 발생 단계별 할 수 있었다 반복 루프

위의 반복을 통해 각 파일을 단계별로 실행하고 클래스 foobar의 모든 발생을 합산 할 수있었습니다. 희망은 다음 사람을 돕는다.

0

내가 묻는 바를 이해하면 다른 .xhtml 파일에 클래스가 나열된 횟수를 알아야합니다.

이것이 성취하려는 경우, 아래 내용은 당신이 찾고있는 것을 수행해야하는 실제적인 예입니다.

on run 
    try 
     set foundItems to do shell script "grep -ri 'yourClassName' " & quoted form of "/Path/to/Your/directory" 
    on error 
     set foundItems to "" 
    end try 

    set oldDelims to AppleScript's text item delimiters -- save their current state 
    set AppleScript's text item delimiters to {return} -- declare new delimiters 
    set tempList to every text item of foundItems 
    set AppleScript's text item delimiters to oldDelims -- restore them 

    set foundCount to count of tempList 
end run