Lo que estoy tratando de hacer: tengo que escanear algunos escritos personales que hago para que mi mentor los revise, tengo 3 tipos de escritura J, F, W. Uso mi escáner para enviarlos por correo electrónico (es algo de trabajo, así que no puedo cambiarlo). Esta mañana tuve la idea de poner el código en la línea de asunto del escáner y luego tener una regla para procesar los correos electrónicos y archivarlos en la carpeta correcta para mí.
Encontré y reutilicé el script en la publicación Guardar archivos adjuntos de Mail.app según el asunto , respuesta de markhunte
Estoy usando Mail en MacOSX 10.13.6
Para hacer pruebas, comenté la función de regla de correo.
Mi prueba es solo mía para seleccionar manualmente los mensajes en la aplicación de correo (se convertirá en regla una vez que funcione).
Una cosa que no puedo entender es que la primera vez que guarda el archivo no cambia el nombre del archivo. Verá que he usado registros para ayudarme a resolver esto. En la primera ejecución del script, la declaración IF cree que la carpeta existe y, por lo tanto, no cambia el nombre del archivo, vuelve a ejecutar el script y lo cambia de nombre correctamente.
Me siento un poco confundido, la ayuda sería muy apreciada.
Mi correo electrónico tiene líneas de asunto con una letra en ellas (lo hago cuando escaneo documentos para que sea más fácil). El nombre del archivo adjunto siempre es scan.pdf
Parece que también hay un problema con CodeList, no encuentra el primer elemento en la Lista (por eso tengo J dos veces), no es un gran problema actualmente.
(*using terms from application "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*)
Lo encontré. La primera configuración de savePath no tiene el código adicional para cambiar el nombre del archivo (por el motivo que sea). También obtuve un código de inicio de semana incluido para crear carpetas de semana dinámicas.
Era:set savePath to attachmentsFolder & ":" & subFolder & ":" & originalName
Ahora:set savePath to attachmentsFolder & ":" & subFolder & ":" & theDate & "_" & originalName
Código 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*)
Monomeeth
mgguine
mgguine