Error handling 邮件中的Applescript错误

Error handling 邮件中的Applescript错误,error-handling,applescript,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

我制作了这个快速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

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

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"

我没有检查您的代码是否正常工作,但这里有一种编写代码的方法来避免问题。您可以使用子例程。当您从“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 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

你需要提供更多的信息。错误是什么?它应该做什么?而不是
characters 33 thru…
尝试
text 33 thru…
,这应该可以解决您的错误当我这样做时,我得到的错误是:无法将“”转换为整型,它会突出显示每个击键消息框@syontank you,@Josh但当我这样做时,它仍然给我错误。当我编译它时,它将文本更改为富文本是正常的吗?是的。Mail知道“富文本”命令,因此由于您的代码位于“告诉应用程序邮件”代码块中,因此它正在使用Mail的applescript字典。因此,解决方案是将系统事件代码移到邮件代码块之外。如果可以避免的话,不应该将“告诉”代码块嵌入彼此。你的错误只是分离代码块的原因之一。嗯,我从来没有听说过它们被称为
子例程
,它们有什么不利于调用
方法
函数
?实际上在applescript中它们被称为处理程序。。。下面是维基百科关于子程序的内容。。。哈,我猜你每天都会学到一些东西+1对于后续行动的彻底性:)