Apple Script für Mail

Hallo zusammen, ich möchte gerne ein Script erstellen da die Mailregeln nicht ausreichend sind.


Ich habe eine Regel erstellt die sobald eine Mail von einem Bestimmten Absender kommt das folgende Script ausgeführt werden soll.


Nun habe ich noch folgendes Problem mit dem Script.


  • Die Dateien sollen alle in dem folgenden Ordner gespeichert werden: /Users/dominikschneider/Library/Mobile Documents/com~apple~CloudDocs/eBons
  • Die Dateien sollen immer mit dem aktuellen Datum im Format und Absender gespeichert werden. yy.mm.tt-"Absender"
  • Es soll kein neuer Ordner erstellt werden, sondern alles in dem oben stehenden Ordner. Perfekt wäre es noch wenn jedes Jahr automatisch ein neuer Unterordner mit der aktuellen Jahreszahl erstellt wird. (Das kann ich aber auch jedes Jahr manuell ändern.


Den Code hab eich im internet gefunden, kenne mich mit Code leider nicht aus.


Vielen Dank schon mal für die Unterstützung.


--set main folder of attachments
set attachmentsPath to "/Users/dominikschneider/Library/Mobile Documents/com~apple~CloudDocs/eBons"
try
set attachmentsFolder to (attachmentsPath as POSIX file) as alias
on error errMsg number errNr
display dialog "Please select a folder for saving the attachments! Error: " & errMsg & " Nr.: " & errNr
return
end try
if not my isdirectory(attachmentsPath) then
display dialog "Please select a folder for saving the attachments!"
return
end if

--set how the sub folders for the attachments should be named
set UseSubject to "false"
set UseFrom to "true"
if not UseSubject = "true" and not UseFrom = "true" then
display dialog "Please set the name of the attachment folders either to the subject or the from of the email!"
return
end if

tell application "Mail"

--get the selected emails
set selectedMessages to selection

--loop over the messages to get and save the attachments
try
repeat with theMessage in selectedMessages

--do only something if the message has attachments
set theAttachments to theMessage's mail attachments
if (count of theAttachments) > 0 then

--make folder in Finder with the selected name option
set theSubject to subject of theMessage
set Originator to sender of theMessage
tell application "Finder"
activate
if UseSubject = "true" and not UseFrom = "true" then
set FolderName to theSubject
else if UseFrom = "true" and not UseSubject = "true" then
set FolderName to Originator
else if UseFrom = "true" and UseSubject = "true" then
set FolderName to Originator & " " & theSubject
end if
try
set newFolder to (make new folder at attachmentsFolder with properties {name:FolderName})
on error errMsg number errNr
if errNr = -48 then
--folder already exists
set newFolder to get item 1 of (every folder in attachmentsFolder whose name contains FolderName)
else
display dialog "Error: " & errMsg & " Nr.: " & errNr
return
end if
end try
open newFolder
end tell

--save the attachments
repeat with theAttachment in theAttachments
set originalName to name of theAttachment
set savePath to (newFolder as rich text) & originalName
save theAttachment in file savePath
end repeat
end if
end repeat
on error errMsg number errNr
display dialog "Error: " & errMsg & " Nr.: " & errNr
end try

end tell

--check if the path is a directory and is actually there
on isdirectory(someItem) -- someItem is a file reference
set filePosixPath to quoted form of (POSIX path of someItem)
set fileType to (do shell script "file -b " & filePosixPath)
if fileType ends with "directory" then return true
return false
end isdirectory


MacBook Pro 13″, macOS 15.3

Gepostet am 13. Feb. 2025 02:17

Antworten
2 Antworten

Dieser Thread wurde vom System oder dem Community-Team geschlossen. Du kannst alle Beiträge positiv bewerten, die du hilfreich findest, oder in der Community nach weiteren Antworten suchen.

Apple Script für Mail

Willkommen in der Apple Support Community
Ein Forum, in dem Apple-Kunden sich gegenseitig mit ihren Produkten helfen. Melde dich mit deinem Apple Account an, um Mitglied zu werden.