1

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

到目前为止,我已经成功地制作了一个脚本,它可以读取肥皂消息,更新必填字段并将其发送到我的电视。完美,至少结果,代码可能不完美,但它可以工作。这是代码。

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)

任何帮助将不胜感激。

4

1 回答 1

0

我假设您的脚本同时与其他应用程序一起运行,并且它需要该处理程序,因此需要进行一些重新设计,以便我们可以将(前主)代码作为子例程调用。

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

我添加了 oncall_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
于 2013-04-21T15:50:06.590 回答