为什么这么短的代码不起作用? [英] Why won't this short bit of code work?

查看:18
本文介绍了为什么这么短的代码不起作用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

只是一个简单的问题:以下 AppleScript 代码有什么问题?它应该做的是获取字符串中文本项的位置(由用户提供的定界符分隔).但到目前为止,它不起作用.Script Debugger 只是简单地说,不能继续 return_string_position"而没有任何特定错误.关于出了什么问题的任何想法?

Just a quick question: What's wrong with the following AppleScript code? What it's supposed to do is get the position of a text item (separated by a user-provided delimiter) within a string. But thus far, it doesn't work. Script Debugger simply says, "Can't continue return_string_position" without any specific errors. Any ideas as to what's wrong?

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

推荐答案

tell system events 命令不正确,应该排除.此外,您不需要使用"的文本项分隔符来制作单词列表,只需使用每个单词"即可.最后,您的代码将只返回传递参数的最后一个匹配项,这将返回每个匹配项.

The tell system events command is incorrect and should be excluded. Also, you don't need to use text item delimiters of " " to make a list of words, simply use "every word of". Lastly, your code will only return the last match of the passed parameter, this will return EACH match.

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.")

这篇关于为什么这么短的代码不起作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆