0

纯粹作为一个实验,我试图用 AppleScript 做一些相当复杂的事情,主要是作为一个学术练习,而不是任何事情,但我遇到了麻烦。这是正在发生的事情。

首先,我有一个名为“ascr_code_library.scpt”的代码库,其中只包含一个方法:

on return_string_position(this_item, this_str, delim)
    set old_delims to AppleScript's text item delimiters
    set AppleScript's text item delimiters to delim
    set this_list to text items of this_str
    set found_pos to 0
    repeat with i from 1 to the count of this_list
            if item i of this_list is equal to this_item then set found_pos to i
    end repeat
    set AppleScript's text item delimiters to old_delims
    return found_pos
end return_string_position

然后,我有这个脚本,“test-2.scpt”。它的作用非常简单,而且不言自明:

set scr_lib to load script (choose file with prompt "Please pick a library")

tell scr_lib
    return_string_position("Who", "Who am I?", " ")
end tell

但是当我运行脚本并选择文件时得到的是以下错误:

*“«data scpt4D617259332E303000000000000000000000000000000000000000000000 000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 . . . . . . . (等等,共四页)。. . 1000101010CFADEDEAD» 不理解 return_string_position 消息。”*

那么我哪里错了?据我所知,它正在正确加载脚本。但是在这样一个简单的脚本中,我还能在哪里出错呢?我尝试在方法调用前加上“my”,但这也不起作用。有任何想法吗?

4

1 回答 1

0

As mentioned in your previous question, I like this script better because it avoids delimiters and returns each instance.

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

You can call the script like this:

run script file ((path to desktop as text) & "test 1.scpt") with parameters {"very", "The coffee was very very very very very ... very hot."}
于 2012-09-05T20:14:59.517 回答