如何在忽略特定字符时使用Applescript文本项分隔符
我想在Applescript应用程序中用逗号分隔文本列表中的每个URL。输入的一个例子是: alloresto.fr eat.ch eatnow.com.au just-eat.ca just-eat.co.uk 使用Applescript的文本项分隔符,我获得了部分成功:如何在忽略特定字符时使用Applescript文本项分隔符,applescript,Applescript,我想在Applescript应用程序中用逗号分隔文本列表中的每个URL。输入的一个例子是: alloresto.fr eat.ch eatnow.com.au just-eat.ca just-eat.co.uk 使用Applescript的文本项分隔符,我获得了部分成功: set enterDomains to myTextField's stringValue() as text -- gets text from text field in my xib window set Apple
set enterDomains to myTextField's stringValue() as text -- gets text from text field in my xib window
set AppleScript's text item delimiters to ", "
set theResults to every word of enterDomains as string
set AppleScript's text item delimiters to ""
alloresto.fr,eat.ch,eatnow.com.au,just,eat.ca,just,eat.co.uk
但这会在每个域中都包含一个-
,因为我将它设置为每个单词。在使用Applescript的文本项分隔符时,是否可以忽略-
字符
我知道我的问题在于enterDomains的每个单词
,因为连字符域包含多个单词,但当我将此行更改为类似enterDomains的文本
时,它会返回相同的域列表,结果没有添加任何逗号
想法或建议?考虑以下示例:
set s to "alloresto.fr eat.ch eatnow.com.au just-eat.ca just-eat.co.uk"
set AppleScript's text item delimiters to " "
set theResults to every text item of s
-- {"alloresto.fr", "eat.ch", "eatnow.com.au", "just-eat.ca", "just-eat.co.uk"}
set AppleScript's text item delimiters to ""
你也去做同样的事。单词的…
与文本的…
的功能独立于文本项分隔符,因此将始终以相同的方式拆分字符串
text item delimiters
允许您指定一个或多个短语,用于将字符串分隔为文本项列表。它还确定了文本项列表
是如何连接在一起的,因此在列表
对象强制成为文本
(或字符串
)对象的任何情况下都非常重要
要在每次出现空格字符时拆分字符串,并且仅拆分空格字符,请执行以下操作:
set enterDomains to "alloresto.fr eat.ch eatnow.com.au just-eat.ca just-eat.co.uk"
set my text item delimiters to space
set theResults to text items in enterDomains
--> {"alloresto.fr", "eat.ch", "eatnow.com.au", "just-eat.ca", "just-eat.co.uk"}
然后,要将此文本项列表
合并到一个字符串中,并用逗号分隔:
# ...Cont'd from the previous code block
set my text item delimiters to ", "
return theResults as text
--> "alloresto.fr, eat.ch, eatnow.com.au, just-eat.ca, just-eat.co.uk"
my TextField's stringValue()'s stringByReplacingOccurrencesOfString:space withString:", "
return the result as text
文本项分隔符
更一般
如上所述,文本项分隔符实际上可以设置为多个项的列表,而不仅仅是单个字符或短语。这会导致在指定的文本项分隔符列表中的每个项出现时拆分字符串,例如
set input to "The quick brown fox jumps over the lazy dog."
set my text item delimiters to {"i", space, "fox"}
get the text items of the input
--> {"The", "qu", "ck", "brown", "", "", "jumps", "over", "the", "lazy", "dog."}
分隔符的顺序在这里并不重要,因为字符串将使用每个分隔符同时进行拆分
但是,当将列表
备份到文本
对象中时,仅使用第一个分隔符将文本项
粘合在一起。在这种情况下,它将是“i”
:
请注意,拆分字符串时,分隔符的每次出现都会从字符串中删除;连接时,第一个分隔符仅插入到每个文本块之间。这实际上是用其他内容替换文本的一部分
使用文本项分隔符替换文本(简介)
在您的特定情况下,您的任务可以概括为需要将字符串中的空格替换为逗号空格。因此,我们可以通过设置文本项分隔符
,删除空格字符,并在连接过程中插入“,”
:
set enterDomains to "alloresto.fr eat.ch eatnow.com.au just-eat.ca just-eat.co.uk"
set my text item delimiters to {", ", space}
return the text items of enterDomains as text
--> "alloresto.fr eat.ch, eatnow.com.au just-eat.ca, just-eat.co.uk"
text item delimiter
还有其他一些特性和特性,我在关于堆栈溢出的其他回答中也提到了这些特性和特性,您可以自由搜索它们。但是,对于绝大多数用例来说,上面的信息是最相关的
AppleScript ObjC
…因为我看到了您对stringValue()
的使用和对.xib
窗口的引用,所以我将很快为您提供上述一些场景的AppleScriptObjC等价物
我相信您会知道,所有示例只有在其出现的脚本具有以下初始代码行时才能工作:
use framework "Foundation"
# use scripting additions -- if Standard Additions are needed
property this : a reference to the current application
property NSArray : a reference to NSArray of this
property NSString : a reference to NSString of this
我将在代码中使用enterDomains
的赋值声明作为起点,但不是将其强制为text
,而是将其保留为stringValue()
返回的cocoa引用对象(--
注释了强制):
因此,enterDomains
现在包含一个NSString
类值对象的实例。因此:
拆分字符串:
set theResults to enterDomains's componentsSeparatedByString:space
加入字符串列表:
theResults's componentsJoinedByString:", "
将每个空格替换为逗号空格:
# ...Cont'd from the previous code block
set my text item delimiters to ", "
return theResults as text
--> "alloresto.fr, eat.ch, eatnow.com.au, just-eat.ca, just-eat.co.uk"
my TextField's stringValue()'s stringByReplacingOccurrencesOfString:space withString:", "
return the result as text
更多信息,请参阅苹果公司关于该类的文档。您的代码毫无意义。文本项分隔符是用于获取某个内容的文本项的。您没有这样做,因此首先没有必要使用文本项分隔符。而且这是不一致的:myTextField的stringValue()
中是否有逗号?如果没有,添加它们似乎没有什么意义。