如何使用AppleScript获取课程计数?

时间:2014-07-07 16:49:51

标签: css html5 count applescript bbedit

学习AppleScript我正在尝试练习,我想知道是否可以在.xhtml文件中获取课程数。

在我的BBEdit项目中,我为项目设置了一个变量:

set this_project to file of project document 1

确保使用以下内容定位所有.xhtml个文件:

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编辑器中返回一个数字但是如何获得项目中每个文件的完整计数?

2 个答案:

答案 0 :(得分: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

答案 1 :(得分:0)

刚才意识到我从未接受过我的问题的答案,我想结束这个Q& A。我没有选择提供的answer,因为text item delimiters从文件中触发了错误的计数,我想要一种不会调用do shell的方法。

在AppleScript和BBEdit中,如果您引用词典,则有FIND

enter image description here

并且通过重复循环,我能够逐步完成class="foobar"的每次出现:

## 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 

通过上面的重复,我能够逐步浏览每个文件并总计每次出现的foobar课程。希望这有助于下一个人。