无法将handler参数传递给代码。新手

时间:2013-04-20 21:42:12

标签: applescript

背景:我正在尝试使用其AllShare功能将来自电话应用程序(Phone Amego)的来电者信息传递给我的三星电视。为此,我必须发送一条带有来电者信息的肥皂信息。电话Amego提供了一种简单的方法来分配AppleScript来调用事件。但是,我没有任何编程经验!

Sofar我成功制作了一个读取肥皂信息的脚本,更新了所需的字段并将其发送到我的电视。完美,至少结果,代码可能不完美,但它的工作原理。这是代码。

set callerID_string to "John Doe : 1-917-123-4567"
set AppleScript's text item delimiters to {":"}
set pieces to text items of callerID_string
set callerID_name to item 1 of pieces
set callerID_number to item 2 of pieces
set AppleScript's text item delimiters to {""}

set myDate to do shell script "date '+%d.%m.%Y'"
set myTime to do shell script "date '+%T'"
set total_Length to (length of callerID_name) + (length of callerID_number) + 780
set search_strings to {"Content-Length: 796", "2013-01-01", "00:00:00", "Mike", "777-777-7777"}
set replace_strings to {"Content-Length:" & total_Length, myDate, myTime, callerID_name, callerID_number}

tell application "Finder"
set theFile to alias "Macintosh HD:Users:Marc:Desktop:IncCallMBsTemplate.txt"
open for access theFile
set fileRef to open for access (theFile as alias)
set fileContents to (read fileRef)
close access theFile
end tell

set the clipboard to fileContents

repeat with i from 1 to (count search_strings)
set the_string to the clipboard
set the_string to my snr(the_string, item i of search_strings, item i of replace_strings)
end repeat

on snr(the_string, search_string, replace_string)
tell (a reference to my text item delimiters)
    set {old_atid, contents} to {contents, search_string}
    set {the_string, contents} to {the_string's text items, replace_string}
    set {the_string, contents} to {"" & the_string, old_atid}
end tell
set the clipboard to the_string


-- Create a file handler for the file to write to.

set myFile to (open for access alias "Macintosh HD:Users:Marc:Desktop:Test.txt" with write permission)
try
    -- Delete current contents of the file
    set eof myFile to 0
    -- Write to file
    write the_string to myFile as «class utf8»
end try
-- Close the file
close access myFile
end snr

set cmd to "/Usr/bin/nc 10.0.1.7 52235 < /Users/Marc/Desktop/Test.txt"
do shell script cmd

问题:在上面的脚本中,我为变量callerID_string设置了一个值,但我应该通过处理程序call_from(callerID_string)从Phone Amego获取它。但无论我尝试什么,我都无法将callerID_string传递给我的代码。它由两部分组成,即来电者的姓名和号码。代码应该像:

on call_from(callerID_string)

任何帮助都将受到高度赞赏。

1 个答案:

答案 0 :(得分:0)

我假设您的脚本同时与另一个应用程序一起运行,它需要该处理程序,因此需要稍微重新设计,以便我们可以将(前主要)代码称为子例程。

我不确定我是否理解这种情况,但无论如何都是这样:

我添加了on call_from...处理程序,该处理程序将其余代码调用为子例程(myAction)。

代码中的第一个注释行可以取消注释,并用于使用AppleScript Editor对其进行测试运行。不再需要时删除该行。

testFilePath是一个属性,全局可见。此外,我更改了硬编码的文件路径,以便找到桌面的路径。

property testFilePath : ""


-- my myAction("John Doe : 1-917-123-4567")


on call_from(callerID_string)

    my myAction(callerID_string)

end call_from


on myAction(CIS)

    if CIS is "" then
        tell me to activate
        display dialog "Caller ID is empty." buttons {"Quit"} default button 1 with icon 0
        return
    end if

    set pathToDesktop to (path to desktop) as text
    set testFilePath to pathToDesktop & "Test.txt"

    set callerID_string to CIS -- "John Doe : 1-917-123-4567"

    set lastTextItemDelimeter to AppleScript's text item delimiters
    set AppleScript's text item delimiters to {" : "}

    set pieces to text items of callerID_string
    set callerID_name to item 1 of pieces
    set callerID_number to item 2 of pieces

    set AppleScript's text item delimiters to {""} -- or lastTextItemDelimeter if you want to change it back to last

    set myDate to do shell script "date '+%d.%m.%Y'"
    set myTime to do shell script "date '+%T'"
    set total_Length to (length of callerID_name) + (length of callerID_number) + 780
    set search_strings to {"Content-Length: 796", "2013-01-01", "00:00:00", "Mike", "777-777-7777"}
    set replace_strings to {"Content-Length:" & total_Length, myDate, myTime, callerID_name, callerID_number}

    set templateFile to pathToDesktop & "IncCallMBsTemplate.txt"

    tell application "Finder"
        set theFile to templateFile as alias -- Macintosh HD:Users:Marc:Desktop:IncCallMBsTemplate.txt"
        open for access theFile
        set fileRef to open for access (theFile as alias)
        set fileContents to (read fileRef)
        close access theFile
    end tell

    set the clipboard to fileContents

    repeat with i from 1 to (count search_strings)
        set the_string to the clipboard
        set the_string to my snr(the_string, item i of search_strings, item i of replace_strings)
    end repeat

    set cmd to "/usr/bin/nc 10.0.1.7 52235 < " & quoted form of (POSIX path of testFilePath)
    do shell script cmd

end myAction

on snr(the_string, search_string, replace_string)

    tell (a reference to my text item delimiters)
        set {old_atid, contents} to {contents, search_string}
        set {the_string, contents} to {the_string's text items, replace_string}
        set {the_string, contents} to {"" & the_string, old_atid}
    end tell


    set the clipboard to the_string

    -- Create a file handler for the file to write to.
    set myFile to (open for access (testFilePath as alias) with write permission)

    try
        -- Delete current contents of the file
        set eof myFile to 0
        -- Write to file
        write the_string to myFile as «class utf8»
    on error the error_message number the error_number
        -- display dialog "Error: " & the error_number & ". " & the error_message buttons {"Cancel"} default button 1
        log "Error: " & the error_number & ". " & the error_message
    end try

    -- Close the file
    close access myFile
end snr