2013-04-20 99 views
1

背景:我試圖使用其AllShare功能將來自電話應用程序(電話Amego)的來電者信息傳遞給我的三星電視。爲了做到這一點,我必須發送帶有來電者信息的肥皂訊息。電話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,但我應該從電話Amego通過處理程序call_from(callerID_string)獲得它。但無論我嘗試什麼,我都無法將該callerID_string傳遞給我的代碼。它由2部分組成,即呼叫者的姓名和號碼。代碼應該像這樣開始:

on call_from(callerID_string) 

任何幫助將不勝感激。

回答

0

我假設你的腳本與在同一時間的其他應用程序運行,它需要一個處理程序,因此一點點重新設計,所以我們可以調用(前主)代碼的子程序。

我不知道如果我理解正確的情況,但在這裏它是無論如何:

我增加了對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 
+0

歡迎您!感謝您的反饋。請注意,如果解決了問題,通常會接受答案。 – 2013-04-21 17:14:02

+0

對不起,在此期間不知道。我在這裏也是一個新手。親切的問候。渣子 – user2303065 2013-04-21 23:13:32

相關問題