0

This is my script based on pickings of one 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

On run I get this error:

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

When I created a proof of scope script so show myself that the tell application "Mail" block scope isn't an issue, it works:

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

From the Applescript docs on Scope (remnant of previous iteration of the question):

set x to 3

In the absence of a global variable declaration, the scope of a variable declared with the copy or set command is normally restricted to the run handler for the script, making it implicitly local to that run handler. However, a handler or nested script object can declare the same variable with a global declaration to gain access to it.

Community
  • 1
  • 1
wide_eyed_pupil
  • 3,153
  • 7
  • 24
  • 35
  • 1
    Is it a typo? `set theAddresses` instead of `set theAddrresses`? – tptcat Dec 11 '13 at 04:30
  • Yes you're right there was a typo! . Fixed but now getting another error: (*Can’t set end of "Address, Name, Counter " to "first.last@localcouncil.vic.gov.au".*) – wide_eyed_pupil Dec 11 '13 at 04:33
  • @tptcat: I propose that I edit question title and code to reflect your solution to original problem, bc I think the resulting issue is of more benefit to future readers. Is that good protocol for SO? – wide_eyed_pupil Dec 11 '13 at 04:37

2 Answers2

1

I have made a few assumptions:

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
adayzdone
  • 11,120
  • 2
  • 20
  • 37
  • Ah thanks for the two variables theAddresses and uniqueAddresses. I wrote this script successfully yesterday (forgot to save and lost due to AS crash) and used the two but in rewriting forgot to make the distinction. I guess that explains the nature of the error msg. Why the preference for shell scripts? (I don't know much shell/bash) – wide_eyed_pupil Dec 11 '13 at 05:28
  • When writing to a text file, It is easier to use a shell script than opening and closing access. – adayzdone Dec 11 '13 at 05:42
  • This is giving me a Permissions error. What's best way to address that? From within the script or can I set folder permissions to allow AS Editor (or is it shell?) to act? – wide_eyed_pupil Dec 11 '13 at 06:21
  • Required "POSIX path of theFile" translation to shell syntax for filepaths. – wide_eyed_pupil Dec 11 '13 at 12:22
1

It looks like you left in some testing code, or didn't edit one of your copied lines correctly. The problem is here:

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

In the first line, you're using syntax for adding the sender to a list (end of). Since theAddresses is a string, not a list, you are getting an error.

You're not getting that error in the second version because you're using the proper syntax for appending to a string:

set theAddresses to theAddresses & theSender & return
Darrick Herwehe
  • 3,553
  • 1
  • 21
  • 30
  • Yes, thank-you. You are right. I was rewriting a script I'd written the day before from memory and forgot I needed two lists, one of strings to search contents of for prior hits and a second list of objects with 3 data items. I ended up doing a bit of each with the one list. I guess I could search inside the one set for prior hits, but it's easier to just say "if theSender is contained in theAddresses then…" and store the 3 data items in a separate list of objects. – wide_eyed_pupil Dec 12 '13 at 13:01