任何方式使Applescript作为一个单词计算“&”?

问题描述:

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

这里有一个解决方法:

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

在这个例子中,它返回:3

此脚本使用Satimage.osax scripting addition.把Satimage.osax文件在文件夹/Library/ScriptingAdditions/Satimage.osax后,您应该能够通过转到菜单项“Window/Library”在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