Text 为什么赢了';这一小段代码不管用吗?
只是一个简单的问题:下面的AppleScript代码有什么问题?它应该做的是获取文本项(由用户提供的分隔符分隔)在字符串中的位置。但到目前为止,它还不起作用。脚本调试器只是简单地说,“无法继续返回字符串位置”,而没有任何特定错误。有什么问题吗Text 为什么赢了';这一小段代码不管用吗?,text,applescript,Text,Applescript,只是一个简单的问题:下面的AppleScript代码有什么问题?它应该做的是获取文本项(由用户提供的分隔符分隔)在字符串中的位置。但到目前为止,它还不起作用。脚本调试器只是简单地说,“无法继续返回字符串位置”,而没有任何特定错误。有什么问题吗 tell application "System Events" set the_text to "The quick brown fox jumps over the lazy dog" set word_index to return_
tell application "System Events"
set the_text to "The quick brown fox jumps over the lazy dog"
set word_index to return_string_position("jumps", the_text, " ")
end tell
on return_string_position(this_item, this_str, delims)
set old_delims to AppleScript's text item delimiters
set AppleScript's text item delimiters to delim
set this_list to this_str as list
repeat with i from 1 to the count of this_list
if item i of this_list is equal to this_item then return i
end repeat
set AppleScript's text item delimiters to old_delims
end return_string_position
告诉系统事件命令不正确,应排除该命令。此外,您不需要使用“”的文本项分隔符来创建单词列表,只需使用“的每个单词”。最后,您的代码将只返回所传递参数的最后一个匹配项,这将返回每个匹配项
on return_string_position(this_item, this_str)
set theWords to every word of this_str
set matchedWords to {}
repeat with i from 1 to count of theWords
set aWord to item i of theWords
if item i of theWords = this_item then set end of matchedWords to i
end repeat
return matchedWords
end return_string_position
return_string_position("very", "The coffee was very very very very very ... very hot.")
您的问题是,系统事件认为函数
return\u string\u position
是它自己的(如果您查看字典,您会发现它不是)。这很容易解决,;只需在调用return\u string\u position
之前添加my
您的新代码:
tell application "System Events"
set the_text to "The quick brown fox jumps over the lazy dog"
set word_index to my return_string_position("jumps", the_text, " ")
end tell
...
或者你也可以用阿达兹敦的溶液。在这种情况下,他/她的解决方案非常适合这项工作,因为在处理简单的文本内容时,实际上不需要以系统事件为目标