Salva gli allegati da Mail.app in base ai problemi dell'oggetto

2

Quello che sto cercando di fare: devo scannerizzare alcuni scritti personali che faccio per il mio mentore da recensire, ho 3 tipi di scrittura J, F, W. Io uso il mio scanner per inviarli via e-mail (è una cosa da lavoro quindi non posso cambiarlo). Ho avuto questa idea stamattina che avrei potuto inserire il codice nella riga dell'oggetto sullo scanner e quindi avere una regola per elaborare le email e archiviarle nella cartella corretta per me.

Ho trovato e riutilizzato lo script nel post Salva allegati da Mail.app in base all'argomento , risposta da markhunte

Sto usando Mail in MacOSX 10.13.6

Per eseguire test, ho commentato la funzione della regola della posta.

Il mio test proviene solo da me per selezionare manualmente i messaggi nell'app di posta (convertirai in regola una volta funzionante).

Una cosa che non riesco a capire è che la prima volta che salva il file non rinomina il file. Vedrai che ho usato i log per aiutarmi a capirlo. Alla prima esecuzione dello script, l'istruzione IF pensa che la cartella esista e quindi non rinomini il file, riesegua lo script e lo rinomina correttamente.

Sentendoti un po 'confuso, l'aiuto sarebbe molto apprezzato.

La mia e-mail ha delle righe dell'oggetto con una sola lettera (lo faccio quando eseguo la scansione dei documenti per renderlo più semplice). Il nome dell'allegato è sempre scan.pdf

SembrachecisiaunproblemaconlacodeList,nontrovailprimoelementonellalista(quindiperchéhoJduevolte),nonèungrossoproblemaalmomento.

(*usingtermsfromapplication"Mail"
    on perform mail action with messages theMessages for rule theRule*)

set codeList to {"J", "F", "W", "O", "J"}
set subFolder to ""
set theDate to do shell script "date +%Y_%m_%d_%H%M%S"
--set ruleName to name of theRule


-- The folder to save the attachments in (must already exist)
tell application "Finder" to set attachmentsFolder to ((path to home folder as text) & "Desktop") as text

tell application "Mail"
    set theMessages to the selected messages of the front message viewer
    --set theMessage to first item of theMessages

    repeat with eachMessage in theMessages
        set theSubject to subject of eachMessage
        repeat with i from 1 to number of items in codeList
            set this_item to item i of codeList
            if theSubject contains this_item then

                set subFolder to this_item
                log "Found subject match " & this_item

            else
                -- display dialog "no subject " & this_item
                log "no subject " & this_item

            end if

        end repeat
        if (count of (eachMessage's mail attachments)) > 0 then
            log "Number of attachments is " & (count of (eachMessage's mail attachments))
            try

                tell application "Finder"

                    if not (exists folder subFolder of folder attachmentsFolder) then
                        make new folder at attachmentsFolder with properties {name:subFolder}
                    end if
                end tell
                -- Save the attachment
                repeat with theAttachment in eachMessage's mail attachments

                    set originalName to name of theAttachment
                    set savePath to attachmentsFolder & ":" & subFolder & ":" & originalName

                    tell application "Finder"
                        if (exists file originalName of folder subFolder of folder attachmentsFolder) then
                            set savePath to attachmentsFolder & ":" & subFolder & ":" & theDate & "_" & subFolder & "_" & originalName
                            log "File will be saved to path: " & attachmentsFolder & ":" & subFolder & ":" & theDate & "_" & subFolder & "_" & originalName
                        else
                            log "Found that file path already exits: " & attachmentsFolder & ":" & subFolder & ":" & theDate & "_" & subFolder & "_" & originalName
                        end if
                    end tell
                    try
                        save theAttachment in file (savePath)
                        log "saved file to " & savePath
                    end try
                end repeat
            on error error_message number error_number

            end try
        else
            log "Number of attachments is " & (count of (eachMessage's mail attachments))
        end if
    end repeat

end tell
(*end perform mail action with messages
end using terms from*)
    
posta mgguinne 12.09.2018 - 03:01
fonte

1 risposta

0

L'ho trovato. La prima impostazione di savePath non ha il codice aggiuntivo per rinominare il file (per qualsiasi motivo). Ho anche ricevuto l'inizio del codice settimanale incluso per creare le cartelle della settimana dinamica.

Era: set savePath to attachmentsFolder & ":" & subFolder & ":" & originalName

Ora: set savePath to attachmentsFolder & ":" & subFolder & ":" & theDate & "_" & originalName

Codice completo:

 # https://apple.stackexchange.com/a/82085/301868
-- (markhunte)


(*using terms from application "Mail"
    on perform mail action with messages theMessages for rule theRule*)

set codeList to {"J", "F", "W", "O", "J"}
set MainFolder to "Desktop" -- After the current users home dir
set subFolder to ""
set theDate to do shell script "date +%Y_%m_%d_%H%M%S"
--set ruleName to name of theRule

-- Sean Korzdorfer
-- 2013-01-21
-- Fork of benwaldie Current Week Range TextExpander snippet: https://gist.github.com/4583398

set theDateW to (current date)
set theStartDate to theDateW
repeat until weekday of theStartDate = Wednesday
    set theStartDate to theStartDate - 1 * days
end repeat


-- Original Script Date Formatting:
-- set theDate to (short date string of theStartDate) & " - " & (short date string of theEndDate)
set theDateW to (year of theStartDate as string) & "-" & (my add_zero(month of theStartDate as integer)) & "-" & (my add_zero(day of theStartDate as integer))

on add_zero(theNumber)
    if theNumber < 10 then
        return "0" & (theNumber as string)
    else
        return theNumber as string
    end if
end add_zero

-- The folder to save the attachments in (must already exist)
tell application "Finder" to set attachmentsFolder to ((path to home folder as text) & MainFolder) as text

tell application "Mail"
    set theMessages to the selected messages of the front message viewer
    --set theMessage to first item of theMessages

    repeat with eachMessage in theMessages
        set theSubject to subject of eachMessage
        repeat with i from 1 to number of items in codeList
            set this_item to item i of codeList
            if theSubject contains this_item then
                set subFolder to theDateW
                log "Found subject match " & this_item

            else
                log "no subject " & this_item
            end if

        end repeat
        if (count of (eachMessage's mail attachments)) > 0 then
            log "Number of attachments is " & (count of (eachMessage's mail attachments))
            try

                tell application "Finder"

                    if not (exists folder subFolder of folder attachmentsFolder) then
                        make new folder at attachmentsFolder with properties {name:subFolder}
                    end if
                end tell
                -- Save the attachment
                repeat with theAttachment in eachMessage's mail attachments

                    set originalName to name of theAttachment
                    set savePath to attachmentsFolder & ":" & subFolder & ":" & theDate & "_" & originalName

                    tell application "Finder"
                        if (exists file originalName of folder subFolder of folder attachmentsFolder) then
                            set savePath to attachmentsFolder & ":" & subFolder & ":" & theDate & "_" & originalName
                            log "File will be saved to path: " & attachmentsFolder & ":" & subFolder & ":" & theDate & "_" & originalName
                        else
                            log "Found that file path already exits: " & attachmentsFolder & ":" & subFolder & ":" & theDate & "_" & originalName
                        end if
                    end tell
                    try
                        save theAttachment in file (savePath)
                        log "saved file to " & savePath
                    end try
                end repeat
            on error error_message number error_number

            end try
        else
            log "Number of attachments is " & (count of (eachMessage's mail attachments))
        end if
    end repeat

end tell
(*end perform mail action with messages
end using terms from*)
    
risposta data 12.09.2018 - 06:39
fonte

Leggi altre domande sui tag