Css 如何使用AppleScript获取类的计数?
学习AppleScript我正在尝试练习,我想看看是否可以在.xhtml文件中获得一个类的计数 在我的BBEdit项目中,我为项目设置了一个变量:Css 如何使用AppleScript获取类的计数?,css,html,count,applescript,bbedit,Css,Html,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, re
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)
如果我尝试将varCount设置为count class=\foobar\它会在AppleScript编辑器中返回一个数字,但如何才能获得项目中每个文件的完整计数?如果我理解您的要求,您希望了解您的类在不同的.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
我刚刚意识到我从来没有接受过我的问题的答案,我想结束这个问答。我没有选择提供的答案,因为文本项分隔符从文件中触发了一个不正确的计数,我想要一种不调用do shell的方法 在AppleScript和BBEdit中,如果您参考字典,则可以找到: 通过一个重复循环,我能够一步一步地遍历class=foobar的每一次出现: 通过上面的重复,我能够一步一步地浏览每个文件,并合计出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