-1

我做了这个快速的 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
4

2 回答 2

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 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
于 2013-08-24T22:21:10.537 回答
0

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

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"
于 2013-08-24T20:59:24.870 回答