如何在忽略特定字符的同时使用 Applescript 文本项定界符
How to use Applescript text item delimiters while ignoring a specific character
我想在 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 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
但这会破坏每个带有 -
的域,因为我将它设置为 every word
。使用 Applescript 的文本项分隔符时是否可以忽略 -
字符?
我知道我的问题出在 every word of enterDomains
上,因为带连字符的域包含多个单词,但是当我将这一行更改为 text of enterDomains
之类的内容时,它 returns 我得到了相同的列表结果是没有添加任何逗号的域。
想法或建议?
考虑这个例子:
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 ""
你们也照做吧
words of...
与 text items of...
words of...
独立于 text item delimiters
运行,因此总是以相同的方式拆分字符串。
text item delimiters
允许您指定一个或多个短语,在这些短语处分隔要分隔成 text items
[=108= 列表的字符串]。它还决定了 text items
的列表如何连接在一起,因此在 list
对象被强制转换为 text
(或 string
)对象的任何情况下都很重要。
在每次出现 space 字符时拆分字符串,并且仅出现 space 字符:
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"}
然后,将这个text items
的列表加入到一个字符串中,用逗号分隔-space:
# ...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"
text item delimiters
更普遍
正如我上面所说,text item delimiters
实际上可以设置为多个项目的 列表 ,而不仅仅是单个字符或短语。这会导致字符串在您指定的 text item delimiters
列表中的每个项目每次出现时被拆分,例如
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."}
此处分隔符的顺序无关紧要,因为将使用同时作用的每个分隔符拆分字符串。
但是,当将 list
加入 text
对象时,只有第一个 分隔符用于粘合 text items
重新走到一起。在这种情况下,它将是 "i"
:
# ...Cont'd from the previous code block
# result: {"The", "qu", "ck", "brown", "", "", "jumps", "over", "the", "lazy", "dog."}
return the result as text
--> "Theiquickibrowniiijumpsioveritheilazyidog."
请注意,拆分字符串时,每次出现的分隔符都会从字符串中删除;连接时,第一个分隔符仅插入每个文本块之间。这实际上是 用其他内容替换 位文本。
使用 text item delimiters
进行文本替换(介绍)
在您的具体情况下,您的任务可以概括为需要用逗号-spaces 替换字符串中的 spaces。所以我们可以一步完成,通过设置 text item delimiters
删除 space 个字符,并在连接期间插入 ", "
:
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"
我在 Stack Overflow 上的其他答案中谈到了 text item delimiters
的其他特征和特点,您可以随意搜索。但是,对于绝大多数用例,上述信息是最相关的。
AppleScript-ObjC
...因为我看到了您对 stringValue()
的使用和对 .xib
window 的引用,我将很快为您提供上述某些场景的等效 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
,而是将其保留为 [=121] =] stringValue()
返回的引用对象(--
注释掉强制转换):
set enterDomains to myTextField's stringValue() -- as text
因此,enterDomains
现在包含 NSString
class 值对象的一个实例。所以:
拆分字符串:
set theResults to enterDomains's componentsSeparatedByString:space
加入字符串列表:
theResults's componentsJoinedByString:", "
用逗号替换每个 space-space:
my TextField's stringValue()'s stringByReplacingOccurrencesOfString:space withString:", "
return the result as text
有关 NSString
class 的更多信息,请参阅 Apple 文档。
我想在 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 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
但这会破坏每个带有 -
的域,因为我将它设置为 every word
。使用 Applescript 的文本项分隔符时是否可以忽略 -
字符?
我知道我的问题出在 every word of enterDomains
上,因为带连字符的域包含多个单词,但是当我将这一行更改为 text of enterDomains
之类的内容时,它 returns 我得到了相同的列表结果是没有添加任何逗号的域。
想法或建议?
考虑这个例子:
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 ""
你们也照做吧
words of...
与 text items of...
words of...
独立于 text item delimiters
运行,因此总是以相同的方式拆分字符串。
text item delimiters
允许您指定一个或多个短语,在这些短语处分隔要分隔成 text items
[=108= 列表的字符串]。它还决定了 text items
的列表如何连接在一起,因此在 list
对象被强制转换为 text
(或 string
)对象的任何情况下都很重要。
在每次出现 space 字符时拆分字符串,并且仅出现 space 字符:
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"}
然后,将这个text items
的列表加入到一个字符串中,用逗号分隔-space:
# ...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"
text item delimiters
更普遍
正如我上面所说,text item delimiters
实际上可以设置为多个项目的 列表 ,而不仅仅是单个字符或短语。这会导致字符串在您指定的 text item delimiters
列表中的每个项目每次出现时被拆分,例如
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."}
此处分隔符的顺序无关紧要,因为将使用同时作用的每个分隔符拆分字符串。
但是,当将 list
加入 text
对象时,只有第一个 分隔符用于粘合 text items
重新走到一起。在这种情况下,它将是 "i"
:
# ...Cont'd from the previous code block
# result: {"The", "qu", "ck", "brown", "", "", "jumps", "over", "the", "lazy", "dog."}
return the result as text
--> "Theiquickibrowniiijumpsioveritheilazyidog."
请注意,拆分字符串时,每次出现的分隔符都会从字符串中删除;连接时,第一个分隔符仅插入每个文本块之间。这实际上是 用其他内容替换 位文本。
使用 text item delimiters
进行文本替换(介绍)
在您的具体情况下,您的任务可以概括为需要用逗号-spaces 替换字符串中的 spaces。所以我们可以一步完成,通过设置 text item delimiters
删除 space 个字符,并在连接期间插入 ", "
:
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"
我在 Stack Overflow 上的其他答案中谈到了 text item delimiters
的其他特征和特点,您可以随意搜索。但是,对于绝大多数用例,上述信息是最相关的。
AppleScript-ObjC
...因为我看到了您对 stringValue()
的使用和对 .xib
window 的引用,我将很快为您提供上述某些场景的等效 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
,而是将其保留为 [=121] =] stringValue()
返回的引用对象(--
注释掉强制转换):
set enterDomains to myTextField's stringValue() -- as text
因此,enterDomains
现在包含 NSString
class 值对象的一个实例。所以:
拆分字符串:
set theResults to enterDomains's componentsSeparatedByString:space
加入字符串列表:
theResults's componentsJoinedByString:", "
用逗号替换每个 space-space:
my TextField's stringValue()'s stringByReplacingOccurrencesOfString:space withString:", " return the result as text
有关 NSString
class 的更多信息,请参阅 Apple 文档。