邮件的Applescript错误

时间:2013-08-24 16:18:02

标签: error-handling applescript

我制作了这个快速的Applescript应用程序并且它给了我一个错误,这是编码:

set buyannounce to "AUTOMATIC SERVER EMAIL : BUY"
set transferannounce to "AUTOMATIC SERVER EMAIL : TRANSFER"
set signupannounce to "AUTOMATIC SERVER EMAIL : SIGNUP"
set sellannounce to "AUTOMATIC SERVER EMAIL : SELL"
set nowsignup to "0"
set announce to "AUTOMATIC SERVER EMAIL"


set ¬
    currentSignups ¬
        to the text returned ¬
    of (display dialog ¬
    ¬
        "How many current signups?" default answer "")


tell application "Mail"
set unreadMessages to (get every message of mailbox "INBOX" of account "Yahoo" whose read status is true)
repeat with eachMessage in unreadMessages

    if announce is in (get content of eachMessage) then

        if buyannounce is in (get content of eachMessage) then
            --buy email 
        end if
        if transferannounce is in (get content of eachMessage) then
            --transfer email
        end if
        if signupannounce is in (get content of eachMessage) then
            set nowsignup to nowsignup + 1
            set eachMessageTrimmed to ((characters 33 thru -1 of (get content of eachMessage)) as string)
            display dialog nowsignup
            set filepath to POSIX path of "/Users/obleopold/Dropbox/Accounts.csv"
            try
                set command to "open " & quoted form of filepath
                do shell script command
            end try
            delay 10
            repeat currentSignups times
                tell "System Events" to keystroke return
            end repeat
            repeat nowsignup times



                tell "System Events"
                    keystroke eachMessageTrimmed --here is where I am getting the error
                end tell

                tell "System Events" to keystroke return
                currentSignups = currentSignups + nowsignup
                set nowsignup to "0"

            end repeat
        end if




            if sellannounce is in (get content of eachMessage) then
        --sell email
            end if

        end if
    end repeat
end tell
end
end
end
end

2 个答案:

答案 0 :(得分:1)

我没有检查你的代码是否正常工作,但这是编写代码以避免问题的一种方法。您可以使用子程序。当您从“tell”代码块中调用子例程时,需要在其前面添加单词“my”。这告诉脚本子例程属于脚本,而不是你告诉的应用程序。

请注意,您可以通过获取消息内容一次来优化代码。每当你“获得每个消息的内容”时,性能就会受到影响......所以只做一次。另请注意,我在键击命令之间添加了一些延迟。通常,当您执行此类操作时,您需要这些小延迟来防止错误。它为您的计算机提供了额外的时间来执行击键。

祝你好运。

set buyannounce to "AUTOMATIC SERVER EMAIL : BUY"
set transferannounce to "AUTOMATIC SERVER EMAIL : TRANSFER"
set signupannounce to "AUTOMATIC SERVER EMAIL : SIGNUP"
set sellannounce to "AUTOMATIC SERVER EMAIL : SELL"
set nowsignup to "0"
set announce to "AUTOMATIC SERVER EMAIL"

set currentSignups to the text returned of (display dialog "How many current signups?" default answer "")

tell application "Mail"
    set unreadMessages to (get every message of mailbox "INBOX" of account "Yahoo" whose read status is true)

    repeat with eachMessage in unreadMessages
        set messageContent to content of eachMessage
        if announce is in messageContent then

            if buyannounce is in messageContent then
                --buy email 
            end if

            if transferannounce is in messageContent then
                --transfer email
            end if

            if signupannounce is in messageContent then
                set nowsignup to nowsignup + 1

                set eachMessageTrimmed to my trimMessageText(messageContent)
                display dialog nowsignup
                my openFilePath(POSIX path of "/Users/obleopold/Dropbox/Accounts.csv")
                delay 10
                repeat currentSignups times
                    my keystrokeSomething(return)
                    delay 0.2
                end repeat
                repeat nowsignup times
                    my keystrokeSomething(eachMessageTrimmed)
                    delay 0.2
                    my keystrokeSomething(return)
                    delay 0.2
                    currentSignups = currentSignups + nowsignup
                    set nowsignup to "0"
                end repeat
            end if

            if sellannounce is in messageContent then
                --sell email
            end if
        end if
    end repeat
end tell


(************* SUBROUTINES *****************)
on trimMessageText(messageText)
    try
        return text 33 thru -1 of messageText
    on error
        return messageText
    end try
end trimMessageText

on keystrokeSomething(something)
    tell application "System Events"
        keystroke something
    end tell
end keystrokeSomething

on openFilePath(filepath)
    try
        do shell script "open " & quoted form of filepath
    end try
end openFilePath

分隔你的tell块的另一种方法就是这样。但在你的情况下,似乎子程序更容易。

tell application "Mail"
    -- do something
end tell

set eachMessageTrimmed to text 33 thru -1 of messageText

tell application "System Events"
    -- do something
end tell

do shell script "something"

tell application "Mail"
    -- do something
end tell

答案 1 :(得分:0)

系统事件是一个应用程序,因此对于你需要的tell块

tell application "System Events"

而不是

tell "System Events"

此外,为防止嵌套的tell块,您可以使用:

tell application "System events"
    tell process "Mail"
        keystroke "My Keystroke"
    end tell
end tell

或者在一行中:

tell application "System Events" to tell process "Mail" to keystroke "My Keystroke"