Applescript for IMAP

Get help. Get answers. Let others lend you a hand.

Moderator: Mr_Noodle

Applescript for IMAP Fri Apr 23, 2010 9:20 am • by eN0ch
Hi folks,

Below is a script I've been using within a Hazel rule for a few years. It's purpose is to give me the option of keeping or discarding an e-mail I've just sent. It worked beautifully with a POP account. I've recently switched to using IMAP for the same account, and since then the script hasn't worked.

The Hazel rule (applied to the folder ~/Library/Mail/IMAP-accountname@servername/Sent.imapbox/Messages):
• any file
• date added is in the last 1 minute

• Run Applescript ...

The script:
Code: Select all
on hazelProcessFile(infile)
   tell application "Mail"
       activate
       set theMessage to message 1 of mailbox "Sent" of account "Mail.com" of application "Mail"
       set theSubject to subject of theMessage
       display dialog theSubject & "... Save copy?" buttons {"No", "Yes"} default button 1
       if (button returned of the result) = "No" then
           set mailbox of theMessage to mailbox "Trash" of account "Mail.com" of application "Mail"
       end if
       set selected mailboxes of front message viewer to {sent mailbox}
       set selected mailboxes of front message viewer to {trash mailbox}
       set selected mailboxes of front message viewer to {inbox}
       display dialog "Continue with Mail?" buttons {"No", "Yes"} default button 1
       if (button returned of the result) = "No" then
           close front window
       end if
   end tell
end hazelProcessFile


Should I be using some different terms for IMAP? Or should I set a time delay somewhere? Or is there something else to try? Any help appreciated. (NB: I'm not much of geek with scripting etc, so please minimise the geekspeak in replying ... )

Thanks.
Lance <º))><

iMac 24" 2021 M1 | macOS 14.4.1 | Hazel 5.2.1
eN0ch
 
Posts: 63
Joined: Tue Jul 01, 2008 7:41 pm
Location: Crookwell, NSW, Australia

Re: Applescript for IMAP Sat Apr 24, 2010 3:01 pm • by Mr_Noodle
I know this post will not be helpful but I would not recommend having Hazel (or any other program) mess with Mail's files except Mail itself. Albeit, your AppleScript does do its thing via Mail but as a general warning you should use any actions that manipulate the files themselves.

Also, if you use the "any file" condition, no other condition makes sense with it. It's a catch-all condition where you are basically saying, do this all the time for every file that comes through. Just nix that part and use "date added is in the last 1 minute". This probably isn't the problem you are seeing but it's something I noticed. Someone else more familiar with AppleScripting Mail will have to chime in on your main problem.
Mr_Noodle
Site Admin
 
Posts: 11865
Joined: Sun Sep 03, 2006 1:30 am
Location: New York City

Re: Applescript for IMAP Sun Apr 25, 2010 6:04 am • by eN0ch
Thanks, Paul. I've deleted the "any file" bit, which on reflection was probably superfluous.

And thanks for the warning about tinkering with Mail's files. I'm sure you're right - although I've had no apparent problems resulting from this script over the few years I've been running it (with a POP account).

Hoping some applescript guru sees this ...
Lance <º))><

iMac 24" 2021 M1 | macOS 14.4.1 | Hazel 5.2.1
eN0ch
 
Posts: 63
Joined: Tue Jul 01, 2008 7:41 pm
Location: Crookwell, NSW, Australia

Re: Applescript for IMAP Mon Apr 26, 2010 9:41 am • by alastor933
Hi Lance,

As often is the case with AppleScript, this is not trivial - until you've done it. And I don't use IMAP, so have not done it.
Anyway, let's give it a try.

First, does the folder ~/Library/Mail/IMAP-accountname@servername/Sent.imapbox/Messages actually contain files? I thought all messages of an IMAP account would be on the IMAP server, not on your Mac.

The script talks to an account named "Mail.com". That should be your IMAP account. To find out if it is, run this little script from Script Editor:
Code: Select all
tell application "Mail"
   account type of account "Mail.com"
end tell
The reply, in the bottom panel of the editor, would be one of "imap/pop/smtp/Mac".

Come to think of it, it might be a simple matter of changes in AppleScript. Did this work in Snow Leopard before you switched to IMAP?

[And it remains to be seen if I'm in the "guru" class...]
alastor933
 
Posts: 53
Joined: Wed Mar 05, 2008 3:52 pm
Location: Utrecht, Netherlands

Re: Applescript for IMAP Mon Apr 26, 2010 7:12 pm • by eN0ch
Hi Alastor (?),

alastor933 wrote:Hi Lance,

First, does the folder ~/Library/Mail/IMAP-accountname@servername/Sent.imapbox/Messages actually contain files? I thought all messages of an IMAP account would be on the IMAP server, not on your Mac.

I'm still learning how IMAP actually works. But yes, the folder does contain .emlx files. I think I have the app optionally configured to store copies of the messages on my HD, kept in sync with the server. So I assume (but don't know for sure) that a sent message would land on the HD initially and then be synced to the server .. which in itself could be another possible thread in this investigation ... ?

The script talks to an account named "Mail.com". That should be your IMAP account. To find out if it is, run this little script from Script Editor:
Code: Select all
tell application "Mail"
   account type of account "Mail.com"
end tell
The reply, in the bottom panel of the editor, would be one of "imap/pop/smtp/Mac".

Well I reckon I can picture you in a saffron robe already ;) . That returned an error "Can't get account". So presumably IMAP accounts require some special string of characters in place of (or in addition to?) to the account name? (The same script worked with a POP account of same name as this IMAP account.)

Come to think of it, it might be a simple matter of changes in AppleScript. Did this work in Snow Leopard before you switched to IMAP?

Yes.

[And it remains to be seen if I'm in the "guru" class...]

Well as observed above you may be "enlightened" indeed! And "purified" too ... ;)
Lance <º))><

iMac 24" 2021 M1 | macOS 14.4.1 | Hazel 5.2.1
eN0ch
 
Posts: 63
Joined: Tue Jul 01, 2008 7:41 pm
Location: Crookwell, NSW, Australia

Re: Applescript for IMAP Tue Apr 27, 2010 2:40 am • by alastor933
Mmmm. That was too easy. Now run this from the editor:
Code: Select all
tell application "Mail"
   set theResult to {}
   set theAccounts to accounts
   repeat with anAccount in theAccounts
      set end of theResult to {name of anAccount, account type of anAccount}
   end repeat
end tell
theResult
(That last statement ensures you'll actually see it in the results panel. This works in Tiger - moving to SL ASAP.)

You should get something like this:
Code: Select all
{{"accountname1", pop}, {"acountname2", imap}}
There is, hopefully, one occurrence of "imap" in that list of lists. Or "Mac", which is a subclass of "imap".

Something else you could try:
- open script in editor
- add a return at the end
- click 'Compile'
Anything happen?

Thijs

[Saffron robes, huh? And a shaven head to boot? I'd go for azure. And a lion's head of silver.Image]
alastor933
 
Posts: 53
Joined: Wed Mar 05, 2008 3:52 pm
Location: Utrecht, Netherlands

Re: Applescript for IMAP Tue Apr 27, 2010 9:24 am • by eN0ch
Right .. um .. that's progress alright.

Turns out that imap accounts have a space as the last character at the end of the account name. Why a space ... ?

Well anyway .. So now the script gets me as far as the two dialogs. But the the fun starts - i.e. the next scripting challenge:
    takes maybe 2 minutes to pop up the first dialog
    then goes into an endless loop giving me the dialog over and over
    only escape is to force quite Mail
    relaunch Mail and it started again
    but the subsided after 2 or 3 repeats
    peace at last

Next morsel of wisdom from the holy mountain ... ?
Lance <º))><

iMac 24" 2021 M1 | macOS 14.4.1 | Hazel 5.2.1
eN0ch
 
Posts: 63
Joined: Tue Jul 01, 2008 7:41 pm
Location: Crookwell, NSW, Australia

Re: Applescript for IMAP Tue Apr 27, 2010 3:10 pm • by alastor933
Darn.
It *was* too easy.
Prepare to descend into the pits.
Not a speck of holyness in sight...
.... imap accounts have a space as the last character...
If you have only one IMAP account you might have put that space there yourself. What does it look like in Mail's prefs?
Otherwise this is worth storing as a tip.

Okay, more poking around.
First, take a peek in Hazel's log (info panel, 8 o'clock. Log window has a search box). There may be a relevant message. This is the format to look for:
Code: Select all
2010-03-26 20:36:26.530 hazelfolderwatch[1179] [Error] AppleScript failed: Error executing AppleScript <path of script> on file <path of file>
2010-03-26 20:36:26.530 hazelfolderwatch[1179] AppleScript error: {
    OSAScriptErrorBriefMessage = "<some text>";
    OSAScriptErrorMessage = "<some text>";
    OSAScriptErrorNumber = <some number>;
    OSAScriptErrorRange = <00000000 00000000 >;
Anything?

Second, I'm curious about the mailbox names the script uses. In Tiger, it's "Sent Messages" and "Deleted Messsages", not "Sent" and "Trash". Try this script:
Code: Select all
tell application "Mail"
   mailboxes of account "<imapAccountName>"
end tell
[edit] I just found out these are synonyms. That's a bit of info the Mail dictionary doesn't give you. Experimentation, as so often.[/edit]

I'm a bit confused about your description of events. You say you get to the two dialogs, but the first one goes into an endless loop. So I'd think you never see the 2nd one. Right?
And that looping: do you get to click a button, or does the dialog just go on playing now-you-see-me-now-you-don't?
Does the dialog show the expected message (theSubject & "... Save copy?")?

Next morsel of wisdom from the holy mountain ... ?
I'm 4 feet above sea level. No alpinism today Image. (Click map once for amazing detail.)
alastor933
 
Posts: 53
Joined: Wed Mar 05, 2008 3:52 pm
Location: Utrecht, Netherlands


Return to Support