任何使Applescript计数的方法"&"一句话?

时间:2017-10-08 01:07:36

标签: applescript

当我告诉Applescript计算字符串的字样时,如" A& B"将文本项分隔符设置为空格,它返回2.它不将&符号计为单词。有什么方法可以改变吗?毕竟,"&"意味着"和",它算作一个单词。

2 个答案:

答案 0 :(得分:0)

这是一种解决方法:

set theString to "A & B"
set wordCount to do shell script "wc -w <<< " & quoted form of theString & " | tr -d ' '"

在此示例中,它返回:3

答案 1 :(得分:0)

此脚本使用Satimage.osax scripting addition.将Satimage.osax文件放入文件夹/Library/ScriptingAdditions/Satimage.osax后,您应该能够在ScriptEditor.app中查看其字典及其所有命令到菜单项“窗口/库”

Download Satimage Scripting Addition

set theString to "A & B"
set toExclude to {" "} as list -- Variable For Items To Be Removed From Your List In Line 4

set theWordCount to items of theString -- Returns The Items Of Your String As A List {"A", " ", "&", " ", "B"}

set resultList to exclude items toExclude from theWordCount -- Removes Specified Items From The List
set theWordCount to count items of resultList -- Returns 3 As Your Count

get theWordCount -- This Line Is Not Necessary And Can Be Removed