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

问题描述

只是一个快速的问题:什么是错用下面的AppleScript code?什么它应该做的就是一个文本项的字符串中的位置(由用户提供的分隔符分隔)。但迄今为止,这是行不通的。脚本调试程序只是说,不能继续return_string_position没有任何具体的错误。任何想法有什么不对?

 告诉应用程序系统事件
    the_text设置为快速的棕色狐狸跳过懒狗
    设置word_index到return_string_position(跳跃,the_text,)
告诉结束在return_string_position(this_item,this_str,delims)
    设置old_delims到的AppleScript的文本项分隔符
    设置的AppleScript的文本项分隔符DELIM
    设置this_list为this_str的列表
    与我重复1到this_list的计数
         如果我的this_list产品等于this_item然后返回我
    重复结束
    设置的AppleScript的文本项分隔符old_delims
结束retu​​rn_string_position


解决方案

该告诉系统事件的命令是不正确的,应该被排除在外。此外,您不需要使用文字分隔符项,使单词列表,只需使用的每一个字。最后,你的code只会返回传递的参数的最后一场比赛,这将返回每场比赛。

 上return_string_position(this_item,this_str)
    设置theWords以this_str每一个字
    集matchedWords到{}
    与我再说一遍,从1数theWords的
        设置AWORD项我theWords的
        如果项目i theWords的= this_item然后设置matchedWords的结束我
    重复结束
    返回matchedWords
结束retu​​rn_string_positionreturn_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
解决方案

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

这篇关于为什么不会code工作的这一短位?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-03 07:06