2013-05-06 86 views
1

我已经在Applescript编写了我自己的Jarvis版本几天了,我一直使用iMessages作为我的输入和输出,因为我喜欢它的便携性,但我现在想要更高级一些,并开始制作它觉得它有用处。如何以不同的方式让applescript控制iMessage回复?

目前我只使用简单的代码行来识别我什么时候发送了一些内容,然后运行相应的脚本来发送回复。比如,如果我说“你好”,它说“你好,先生”回到我身边。

一个例子是:

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

     if theMessage is "hello" then 
      run script ("/Users/Alex/Desktop/JarvisScripts/hello.scpt" as POSIX file) 
     end if 

    end message received 
end using terms from 

都好,但是我前面说过我想走得更远,我需要一种方式,它可以问我一个问题,如“你好吗”,然后取决于我说的“好”,它会运行正确的脚本来回应“好”。 我现在尝试沿着线走:

if theMessage contains "Are you ok" then 
    run script ("/Users/Alex/Desktop/JarvisScripts/areyouok.scpt" as POSIX file) 

    if theMessage is "I'm fine" then 
     run script ("/Users/Alex/Desktop/JarvisScripts/happy.scpt" as POSIX file) 
    else 
     if theMessage is "No I'm not" then 
      run script ("/Users/Alex/Desktop/JarvisScripts/unhappy.scpt" as POSIX file) 
     end if 
    end if 
end if 

但我知道这是远离正确的。那么,有人可以帮我一些代码吗?谢谢

回答

0

而不是将您的'响应'存储在单独的脚本文件中。尝试使用处理程序。

例如:

if theMessage contains "Are you ok" then 
    run script ("/Users/Alex/Desktop/JarvisScripts/areyouok.scpt" as POSIX file) 

    if theMessage is "I'm fine" then 
     happyResponse() 
    else 
     if theMessage is "No I'm not" then 
      unhappyResponse() 
     end if 
    end if 
end if 


--HANDLERS-- 
on unhappyResponse() 
    --<<whatever you want to happen (the code inside your .scpt file)>> 
end unhappyResponse 

on happyResponse() 
    --<<put the contents of happy.scpt here>> 
end happyResponse 

正如你所看到的,“处理”是在你的代码的底部列出,每当功能之一是所谓的,代码为他们进行评估。现在我不知道你的.scpt文件里面有什么,但是这应该在大多数用途中起作用。

+0

我完全理解,并且我已经尝试了您的代码,但我刚收到一条错误消息,说“事件:收到消息 文件:commands.scpt 错误:无法将某些数据转换为预期类型。你能想到为什么? – Aloogy 2013-05-06 21:18:11

+0

没有完整的上下文或看到完整的脚本,我不能告诉。如果可以,请编辑您的原始问题以包含完整的代码,或者如果代码太长,请提供代码链接。 – 2013-05-07 15:21:28