2013-05-07 30 views
0

我一直在研究腳本一段時間,並且提出了很多問題。根據一些人的建議我也重寫了很多代碼。然而,我仍然有這個問題沒有答案。以下是iMessage PA的「Siri」代碼。基本上,當我通過iMe​​ssage向它發送命令時,它會響應。Applescript:未知的錯誤

我一直在拼命地讓它有更多的智慧,讓它在句子中回覆。你可以在代碼中看到,當它問我「......你還好嗎?」它應該尋找「是」或「否」,但只有當第一個問題被問到。但目前它只是響應最初的「我很好,先生,你還好嗎?」然後當我說出是或否時,什麼都不做。

只是爲了確保它不是主代碼,我添加了一個簡單的「hello world」來查看它是否會正常響應。以下是代碼:

using terms from application "Messages" 
    on message received theMessage from theBuddy for theChat 

     if theMessage is "hello" then 
      helloresponse() 
     end if 

     if theMessage contains "Are you ok" then 
      areyouok() 
      if theMessage is "yes" then 
       happyResponse() 
      else 
       if theMessage is "no" then 
        unhappyResponse() 
       end if 
      end if 
     end if 

     if theMessage contains "hello world" then 
      helloworldresponse() 
     end if 

    end message received 

end using terms from 

on helloresponse() 
    tell application "Messages" 
     send "Hey sir!" to buddy "[email protected]" of service "E:[email protected]" 
    end tell 
end helloresponse 

on happyResponse() 
    tell application "Messages" 
     send "you said yes, yey!" to buddy "[email protected]" of service "E:[email protected]" 
    end tell 
end happyResponse 

on unhappyResponce() 
    tell application "Messages" 
     send "You said no, thats a shame!" to buddy "[email protected]" of service "E:[email protected]" 
    end tell 
end unhappyResponce 

on areyouok() 
    tell application "Messages" 
     send "I am fine sir, are you ok?" to buddy "[email protected]" of service "E:[email protected]" 
    end tell 
end areyouok 

on helloworldresponse() 
    tell application "Messages" 
     send "World replies: Hello." to buddy "[email protected]" of service "E:[email protected]" 
    end tell 
end helloworldresponse 

感謝任何幫助的人可以給我!

+0

我已經更新我的答案給你如何Ÿ一個想法響應你可以測試你的邏輯,也可以看到獲得你想要的答案的方法 – markhunte 2013-05-07 23:39:46

回答

1

如果我正在閱讀此權利。

你會得到一個消息,「你還好嗎」

劇本的反應是「我很好,先生,你還好嗎?」

你得到一個消息發回「是」或「否」

但這種情況正在等待「是」或「否」的消息被埋沒了,如果塊中的「我很好腳本的一部分先生,你還好嗎?「所以它永遠不會被觸發。

**更新

這是一種測試您的邏輯的方法。

創建兩個腳本。

第一個將是邏輯的一個。你將在Applescript編輯器中運行它。

property lastMessage : "" 

set theMessage to load script "/PATH/TO/SECOND/SCRIPT/secondScript.scpt" 
run theMessage 

if theMessage is "hello" then 
    helloresponse() 
end if 

if theMessage contains "Are you ok" then 

    areyouok() 

end if 

if theMessage is "yes" and lastMessage is "I am fine sir, are you ok?" then 
    happyResponse() 
else if theMessage is "no" and lastMessage is "I am fine sir, are you ok?" then 
    unhappyResponse() 

end if 


if theMessage contains "hello world" then 
    helloworldresponse() 
end if 




on helloresponse() 
    say "Hey sir!" 
end helloresponse 

on happyResponse() 
    say "you said yes, yey!" 
    set lastMessage to "" 
end happyResponse 

on unhappyResponce() 
    say "You said no, thats a shame!" 
    set lastMessage to "" 
end unhappyResponce 

on areyouok() 
    say "I am fine sir, are you ok?" 
    set lastMessage to "I am fine sir, are you ok?" 
end areyouok 

on helloworldresponse() 
    say "World replies: Hello." 
end helloworldresponse 

。注意到財產我已經給它的。傳遞給屬性的任何變量都存儲在那裏。並且可以在腳本稍後再次運行時訪問。

在這種情況下,我已經將它設置爲存儲最後的回覆/消息,如果回覆是「我很好,先生,你還好嗎?」

我也拆你的如果塊,這樣的happyResponse和unhappyResponse響應將只用「是」或「否」的消息被觸發,只要存儲在屬性lastMessage最後的反應是「我好的先生,你還好嗎?「

在第一個腳本中是一行加載和運行第二個腳本。

第二個腳本用於給出消息。該腳本被編輯並保存。

例如第二個腳本將只包含;

set theMessage to "Are you ok" 

第二個腳本被保存。 然後你運行第一個腳本來獲取它的消息並作出迴應。 這應該是「我很好,先生,你還好嗎

然後就可以編輯和保存第二個腳本;

set theMessage to "yes" 

,並再次運行第一個腳本的反應應該是。 「你說的沒錯,yey!」

你會得到你期待