I want to set up a Mail.app rule, that probably requires a script, which means I have no idea how to do it. I have seen the beginner stuff in here, but I honestly don’t have time to learn everything, just for one little script. Anybody want to give me a helping hand learning just what i need?
What I want is for email that meets my Junk rule conditions to:
here is a PARTIAL answer:
First, go to the “Junk” section of Mail’s peferences, click “Advanced” and add “Run Applescript” to the “Perform the following actions:” portion, with the name of this applescript, and “Forward message” with the email address you want it forwarded to. (Note that the applescript is not complete)
tell application "Mail"
set junkMail to contents of "Junk"
--I'm not sure what you mean by "Show All Headers"
bounce junkMail
delete junkMail --this will delete the junk from EVERY mailbox
end tell
You need someone else, someone who knows what “Show all headers” means and how to script it.
How do I do it using the regular “rules” of the Mail Prefs?
then, I assume this changes the “set…” line? can you explain “set” to me simply?
show all headers is: Mail>View>Message>Long Headers a.k.a. Shift-Command-H.
Here’s my guess, so far:
-Mail, added mailbox folder “JunkReport”
-Mail>Prefs>Rules>Add Rule: “JunkReport”
-JunkReport rule: If any of the following conditions are met: [all my conditions], Perform the following actions:
[1] “Move Message” to mailbox “JunkReport”
[2] “Run Applescript” : “Junk.scpt”
tell application "Mail"
set junkMail to contents of "JunkReport"
--[view long headers]
--[send to spam@mac.com]
bounce junkMail
delete junkMail
end tell
Yay, you’re learning! I felt that way too, believe you me. And look at me now- I can help people! Cool!
Sorry.
So, for your questions:
You got your guess pretty much right, except instead of adding the forward action to the applescript part, set it as one of the “Perform the following actions” in Mail. Also, this is my problem, there’s a far better way to get every piece of spam that you or Mail has marked. Your version would take all the items in the folder “JunkReport” and put them in the folder “JunkReport”. Edit: No, I was wrong. Moral: always read posts carefully before you reply. Or something. With these changes, the script becomes:
tell application "Mail"
set junkMail to messages whose junk mail status is yes
--[view long headers]
--[send to spam@mac.com] <--No, you already did that with the Mail action.
bounce junkMail
delete junkMail
end tell
Thank you for an easy question! Before I explain that, you need to know that whatever comes after the “set” command (or others, but for now all you need to know is “set”) is a variable- kind of like the ones you learned (hopefully) in algebra, except in AppleScript a variable can stand for pretty much anything- I haven’t tested it’s limits. Notice that in Script Editor, all variables show up in green after you compile your script. Also, rather than being one letter, they are usually a word or two, both so you know what you wanted when you wrote it and in case you need more than 26 variables in a long script. Variables can consist of alphanumeric characters and an underscore (“_”), although I like to capitalize the first letter of the second word instead of separating them with underscores. Lastly, some variables are reserved by the system and certain applications, so if your script seems to not be working or a variable is mistaken for a command (shown in blue instead of green), just change your variable. I think I ran into that with “junk” and changed it into “junkMail”. And sorry for the long-winded explanation. A set command simply assigns a value, usually text or a number, to a variable.
Oh, that’s what you meant. I don’t think there’s a specific command for that, so we’ll have to delve into the realms of GUI Scripting. (Here’s what Apple has to say about it.) What that means is essentially telling an applescript to click a button or type a key, and it needs to have access for assistive devices enabled. To do this, go to System Preferences > Universal Access and make sure “Enable access for assistive devices” is checked. Now that you’ve done that run this script:
tell application "Mail"
--set the variable
set junkMail to messages whose junk mail status is yes
--view long headers
try
tell application "Mail" to activate
tell application "System Events"
tell process "Mail"
tell menu bar 1
tell menu bar item "View"
tell menu "View"
tell menu item "Message"
tell menu "Message"
click menu item "Long Headers"
end tell
end tell
end tell
end tell
end tell
end tell
end tell
end try
--bounce
bounce junkMail
--delete
delete junkMail
end tell
The GUI Scripting part is in a try block so if it fails, the rest of the script will continue. If you don’t want it to continue without long headers, just delete “try” and “end try”.
And I berate myself for having to ask, but if there’s anyone out there who knows the command to press “shift-command-H”, please tell me/us. Thanks, and legioss, I hope I helped.
Edit again: I found out how to push a button. Here’s how the script looks now. As a bonus, it should remember the application you were working in before it activated and go back to that afterwards.
tell application "Mail"
--set the variable
set junkMail to messages whose junk mail status is yes
end tell
try
tell application "System Events"
--remember what you were doing
set this_was_frontmost to name of some process whose frontmost is true
--view long headers
tell application "Mail" to activate
tell process "Mail"
keystroke "H" using {command down, shift down}
end tell
end tell
end try
tell application "Mail"
--bounce
bounce junkMail
--delete
delete junkMail
end tell
--go back to what you were doing
try
tell application "System Events"
tell application this_was_frontmost to activate
end tell
end try
So my script would look like this with Pidge’s tip.
tell application "Mail"
--set the variable
set junkMail to messages whose junk mail status is yes
--view long headers
set header detail to all
--bounce
bounce junkMail
--delete
delete junkMail
end tell
Still, I’m happy that I learned how to go back to your app when you’re done.
4a. So “set whatever…” is just the way of naming what a variable is that will/may be used in the rest of the script? Or at least in ‘simple’ scripts for my sake…?
4b. And I can use any variable I want, like say… “Assquatch” (within naming limitations, and not already reserved)?
4c. So “header detail” is apparently one of Mail’s reserved variables? blue = reserved?
5a. The reason I didn’t want to use the “Junk Mail” portion of the preferences (as opposed the “rules”), is that “Junk mail” isn’t customized like my rules. My “Rules” that I have worked on for the last 5 years are near perfected. Because, back when I started using Mail there was a bug in “Junk Mail”, where “perform custom actions”>“conditions” were limited visually. If you make more than can fit on your screen, there’s no scrollbar to go down and see the rest. It looks like they finally fixed it (Leopard?). HOWEVER, if you ever toggle between “perform custom actions” and the other two settings, it looks like you STILL lose all the custom actions you may have set up. I don’t want to lose all the hard work due to some weird app or system freeze/crash, or maybe weird update/install. You just can’t ‘manage’ them the way you can in “rules”.
5b. If I learn how to do it in “rules”, theoretically I can learn to make other scripts I can apply in other rules.
How do I find out what different variables and commands are available in different apps? i.e. “header detail” and “bounce”, “delete”? (I had tried “header” early on, but it didn’t compile. I didn’t know “header detail”…
You need to be checking out the applescript dictionary for whatever app you are trying
to script.
the “header detail” is a property of the mail application class
in the dictionary it tells you what you can change the “header detail” to and what it defaults to.
in script editor menu go to window then library, you can add other apps in there as well, most of the main
default apple applications are listed already just double click to view them.
one word of warning sometimes there not the clearest! on how to use them but there’s everything in there
that you can script the app to do.
hope this helps.
4a. Yes, and not only in simple scripts. set will always set a variable, however Script Editor will detect a reserved variable (such as Move) as an action (that’s not actually the official term, as far as I know) rather than a variable. For example, this script:
set move to "Something"
will not compile. The somewhat confusing thing is that the error it returns is “An end of script can’t go after this “”.”, and I know the quotations are kind of confusing, so everything inside the outermost two is in the error message. This script, however:
set moveThat to "Something" as text
works perfectly, although it is pretty pointless. Notice that “move” is in blue (when you use it in a way that it will compile) and “moveThis” is green. blue=keyword, green=variable.
These are the guidelines I’ve come across: anything other than
Language keywords- the blue bold text
Application keywords- the regular blue text
Comments- the grey text, which you prefaced with – or enclosed with (* *)
Operators- such as () , & = + etc.
Values- numbers, strings, lists, etc.
Will be considered variables after compiling. (I got that list straight from Script Editor preferences, so it’s probably correct in essentials)
What I meant by “(or others, but for now all you need to know is “set”)” is that there are other commands that will interact with a variable (e.g. “to”, “on”).
4b. Yes again, although I recommend that your variables make sense, so you know what you’re talking about when you look at your script two months later.
4c. Well, that’s what I said, but I was wrong. Look above for clarification.
5a. Oh. That makes sense.
5b. See 5a.
Pidge explained that very nicely.I would like to add that another (easier?) way to view dictionaries is to drag the application (in Finder) to the Script Editor icon in the Dock. I personally would also like a list of the generic “language keywords”. Does anyone know where that can be found?