什么是Applescript错误“(*不能将”地址,名称,计数器“的结尾设置为”f.l@place.vic.gov.au“。*)”告诉我?

时间:2013-12-11 04:27:14

标签: applescript scope apple-mail

这是我的脚本基于一个here on 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".*)"

当我创建范围脚本证明时,请告诉我自己告诉应用程序" Mail"阻止范围不是问题,它起作用:

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

来自范围的Applescript docs(问题的前一次迭代的残余):

  

将x设为3

     

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

2 个答案:

答案 0 :(得分: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

答案 1 :(得分: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