I am new to Applescript and cobbled the attached together after trolling the web. The code bombs in two places and I have no idea what I’ve got wrong and why. Any and all assistance and/or insight is greatly appreciated! The two offending lines are:
1.) set subFolder to (name of theRule) as text
2.) repeat with eachMessage in theMessages
using terms from application “Mail”
–
– Attempt to save mail attachment(s) from message(s) into Folders named by the Rule (text)
–
on perform mail action with messages theMessages for rule theRule
-- set up the attachment folder path
--
tell application "Finder"
set folderName to "Documents:Newsletters" -- Main respository for all newsletters
set homePath to (path to home folder as text) as text
set attachmentsFolder to (homePath & folderName) as text
--
-- Now use (or create) a subfolder within the Newsletters directory
--
-- set the sub folder for the attachments to the name of the Mail Rule
-- All future attachments using this rule will the be put here.
--
set subFolder to (name of theRule) as text
-- set subFolder to "Poten Newsletters"
if (do shell script "/bin/test -e " & quoted form of ((POSIX path of attachmentsFolder) & "/" & subFolder) & " ; echo $?") is "1" then
-- 1 is false, so create the subfolder
do shell script "/bin/mkdir -p " & quoted form of ((POSIX path of attachmentsFolder) & "/" & subFolder)
end if
end tell
tell application "Mail"
repeat with eachMessage in theMessages
-- repeat with a from 1 to count theMessages
set attachCount to count of (mail attachments of eachMessage)
-- if attachCount is not equal to 0 then
display notification ((count of mail attachments of eachMessage) as string) & " attachments will be processed."
delay 5 --> allow time for the notification to trigger
try
-- Save the attachment
repeat with theAttachment in eachMessage's mail attachments
set originalName to name of theAttachment as rich text
display notification ("Trying to save " & originalName)
delay 5 --> allow time for the notification to trigger
set savePath to attachmentsFolder & ":" & subFolder & ":" & originalName
try
save theAttachment in file (savePath)
end try
end repeat
--on error msg
--display dialog msg
end try
-- end if
end repeat
end tell
end perform mail action with messages
end using terms from
Model: Mac Air
AppleScript: Script Debugger 5
Browser: Safari 537.74.9
Operating System: Mac OS X (10.8)
Hi, and welcome to the forum. I only skimmed your code, but I believe the first issue is that you’re trying to get the rule’s name from within a Finder block; you need to variabilize that below the on perform mail action handler. The second issue may be harder to identify without the specific error’s text, but I think that “file” is the culprit, as you are making a file, rather than saving to an existing one”strike that specifier.
It may be helpful for you to test the basic functionality of this without the mail handlers and try blocks”just use a selection; once you’ve ironed out the kinks, then make it into a rule.
I’m going to break this into pieces to see if I can resolve. This does not run… Any idea WHY?
using terms from application “Mail”
–
– Save mail attachment(s) Folders named by the Rule (text)
–
on perform mail action with messages theMessages for rule theRule
–
– Fetch the parameters passed to this handler
–
– The following line blows up with a script debugger message of:
– Script Debugger got an error: Can’t get rule “Argus Newsletters”.
–
set subFolder to name of rule theRule
display dialog ("Attachment folder: " & subFolder)
end perform mail action with messages
end using terms from
Since my upgrade to Mountain Lion (from Snow Leopard), as far as I’m concerned, I gave up AppleScripts attached to Mail rules (when attachments are involved) at least as automically run script at receiving messages. For some reason the attachments were not reachable to the script. So up to now I need to hit Option-Cmd-L on a selected message as to performing the script attached to a rule. Don’t know if this has any connexion with your concerns.
I would edit the code creating the target folder.
Triggering OSAX features like path to and do shell script in a tell application block is issuing non fatal errors since the delivery of 10.6.
Would be better with :
tell application "Mail"
set subFolder to name of theRule # it's text, no need to coerce
end tell # Finder
set folderName to "Newsletters" -- Main respository for all newsletters
set homePath to path to documents folder as text
set attachmentsFolder to (homePath & folderName) as text
--
-- Now use (or create) a subfolder within the Newsletters directory
--
-- set the sub folder for the attachments to the name of the Mail Rule
-- All future attachments using this rule will the be put here.
--
-- set subFolder to "Poten Newsletters"
if (do shell script "/bin/test -e " & quoted form of (POSIX path of attachmentsFolder & ":" & subFolder) & " ; echo $?") is "1" then
-- 1 is false, so create the subfolder
do shell script "/bin/mkdir -p " & quoted form of (POSIX path of attachmentsFolder & ":" & subFolder)
end if
Yvan KOENIG (VALLAURIS, France) mardi 25 mars 2014 15:56:34