0

这是我的脚本,基于在 SO上挑选的一个:

set theFile to (path to desktop folder as text) & "unsubscribe.csv"
set theRecord to ""
set counter to 1
set theAddresses to "Address, Name, Counter" & return
try
    set theFileID to open for access file theFile with write permission

    tell application "Mail"
        set emailSelection to selection
        repeat with eachMessage in emailSelection
            --  log sender of the_message
            set theSender to extract address from sender of eachMessage
            set theName to extract name from sender of eachMessage
            if theAddresses contains theSender then
                log "***     Double:    " & theSender & "    ***"
                theSender & "***********" & return
            else
                copy theSender to the end of theAddresses
                set theAddresses to theAddresses & theSender & "," & theName & "," & counter & "," & return
                log theAddresses

            end if

        end repeat
    end tell

    try
        write theAddresses to theFileID as «class utf8»
    on error theError
        log theError
    end try

on error theError
    log theError
    try
        close access file theFile
    end try
end try

运行时出现此错误:

"(*Can’t set end of "Address, Name, Counter " to "f.l@place.vic.gov.au".*)"

当我创建范围证明脚本时,向自己展示告诉应用程序“邮件”块范围不是问题,它可以工作:

try
    set theFile to (path to desktop folder as text) & "test3.csv"
    set theFileID to open for access file theFile with write permission
    set theAddresses to "Address, Name, Counter" & return
    tell application "Mail"
        set emailSelection to selection
        set theMsg to the first item in emailSelection
        set theSender to extract address from sender of theMsg
    end tell

    set theAddresses to theAddresses & theSender & return

    try
        write theAddresses to theFileID as «class utf8»
    on error theError
        log theError
    end try
    close access file theFile

on error theError
    close access file theFile
end try

来自 Scope 上的Applescript 文档(问题的先前迭代的残余):

将 x 设置为 3

在没有全局变量声明的情况下,使用 copy 或 set 命令声明的变量的范围通常仅限于脚本的运行处理程序,使其隐式地位于该运行处理程序的本地。但是,处理程序或嵌套脚本对象可以使用全局声明来声明相同的变量以访问它。

4

2 回答 2

1

看起来您留下了一些测试代码,或者没有正确编辑您复制的行之一。问题在这里:

copy theSender to the end of theAddresses
set theAddresses to theAddresses & theSender & "," & theName & "," & counter & "," & return
log theAddresses

在第一行中,您使用了将发件人添加到列表 ( end of) 的语法。由于theAddresses是字符串,而不是列表,因此您会收到错误消息。

您在第二个版本中没有收到该错误,因为您使用了正确的语法来附加到字符串:

set theAddresses to theAddresses & theSender & return
于 2013-12-11T21:25:29.537 回答
1

我做了几个假设:

set theFile to POSIX path of ((path to desktop folder as text) & "unsubscribe.csv")
set counter to 0
set theAddrresses to {"Address, Name, Counter" & return}
set uniqueAddresses to {}

tell application "Mail"
    set emailSelection to (get selection)

    repeat with eachMessage in emailSelection
        --  log sender of the_message
        set theSender to extract address from sender of eachMessage
        set theName to extract name from sender of eachMessage

        if uniqueAddresses contains theSender then
            log "***     Double:    " & theSender & "    ***"
            theSender & "***********" & return
        else
            -- I think you want to increment your counter ?
            set counter to counter + 1

            copy theSender to the end of uniqueAddresses
            set theAddrresses to theAddrresses & theSender & "," & theName & "," & counter & "," & return
            log theAddrresses
        end if
    end repeat
end tell

do shell script "echo " & quoted form of (theAddrresses as text) & " > " & quoted form of theFile
delay 1
do shell script "open " & quoted form of theFile
于 2013-12-11T05:12:07.307 回答