AppleScript gurus, help!! :-)

Discussion in 'Mac Apps and Mac App Store' started by WM., May 31, 2004.

  1. WM. macrumors 6502

    Joined:
    Apr 18, 2003
    #1
    Hi there,

    First off, and perhaps most importantly, all this is happening under 10.3.4.

    I'm trying to write an AppleScript that I'll use as a PDF workflow option, to help automate the emailing of this newsletter my mom writes. I have very limited experience with AppleScript, and I've built on Apple's example scripts and the documentation I could find on ADC (which is quite a bit). I may try posting this on Apple's AppleScript mailing list, or I could go create an account on MacOSXHints, if I really have to, but I was hoping someone here might be able to help.

    So here's what I want to happen, in English:

    • My mom goes to the Print dialog, and uses the PDF Workflow menu thingy to select "Email Newsletter" (not the actual name of the script, but it's what I'll use to post here).
    • The script creates a new message in Mail, putting my mom's email address with a custom name ("newsletter e-mail list") in the To: field, and a group that we have set up in Address Book ("newsletter only") in the Bcc: field.
    • The script attaches the PDF of the newsletter to the email. It does not have to send the email or do anything beyond that.

    So here's what I have so far:

    Code:
    on open these_items
    	set AppleScript's text item delimiters to {">, <"}
    	tell application "Address Book"
    		set NewsletterRecipientsList to value of emails of people of group "newsletter only" as list
    		set NewsletterRecipients to "<" & (NewsletterRecipientsList as text) & ">"
    	end tell
    	set AppleScript's text item delimiters to {""}
    	tell application "Mail"
    		set NewsletterMessage to make new outgoing message with properties {visible:true, sender:"my mom's name <mymom'semail@domain.tld>"}
    		tell NewsletterMessage
    			make new to recipient at end of to recipients with properties {address:"mymom'semail@domain.tld", name:"newsletter e-mail list"}
    			make new bcc recipient at end of bcc recipients with properties {name:"newsletter only", address:NewsletterRecipients}
    			tell content to make new attachment with properties {file:these_items} at after the last paragraph
    		end tell
    		activate
    	end tell
    end open
    And here's what happens:

    Everything works great until it gets to putting the group in for the Bcc recipient. The title that it puts in for the group is "newsletter only, , , , , , , , , , " (with a lot more commas than that), and only the first email from the group is in the email field of the recipient. But if I tell Mail to make a new message with the content being NewsletterRecipients, every single email shows up properly in the content section of the message, surrounded by <> and with a comma and a space in between, which is what Mail wants (i.e.: <email #1>, <email #2>, <email #3>). So I know that my NewsletterRecipients variable (?) is getting to Mail OK.

    I'm guessing that the problem has something to do with the text item delimiters business, which I found in what seems to be the main AppleScript documentation (no linkage immediately at hand), which seems to be kind of old. But if I take out the list business and the text item delimiters (so I just have set NewsletterRecipients to value of emails of people of group "newsletter only" as text), the email addresses of the group will be all squashed together (i.e.: email#1email#2email#3), which Mail won't like AFAIK. The only method I could find for getting them separated by a comma and a space and surrounded by <these things> was the text item delimiters business. And even though I set the delimiters back to nothing (which is the default) before I do anything with the title of the recipient, I still end up with all those commas in that title.

    Of course, the easiest thing of all would be if I could just tell Mail to create new bcc recipient with properties {group:"newsletter only"}, or something like that. That would be great, but I don't think I can do that: I pored over Mail's AppleScript dictionary, and I couldn't find any hints of a way to access Address Book data and/or Mail's Address Panel (cmd-option-A) via AppleScript without scripting Address Book itself. (And I REALLY don't think I want to get into GUI Scripting.) Using Address Book is fine with me, it's just more of a PITA, and I can't get it to work. :)

    Oh, I should also note that I have not tested the attachment parts of this script. I've used an appropriately modified fragment of it to test the email creation and addressing parts, and I'm assuming (perhaps stupidly) that the attachment part should be no problem, because I've taken it straight from Apple example scripts.

    Anyway...any ideas?

    TIA
    WM
     
  2. kylos macrumors 6502a

    kylos

    Joined:
    Nov 8, 2002
    Location:
    MI
    #2
    Here's the problem: a single bcc recipient object has a single address property. You can't give it more than 1 address. And each address can only contain 1 email address. Forget about the whole delimiting business. What you need is a repeat loop.

    Code:
    on open these_items
            tell application "Address Book"
                    [B]set NewsletterRecipientsAddressList to value of emails of people of group "newsletter only" as list
                    set NewsletterRecipientsNameList to name of people of group "newsletter only" as list[/B]
            end tell
            tell application "Mail"
                    set NewsletterMessage to make new outgoing message with properties {visible:true, sender:"my mom's name <mymom'semail@domain.tld>"}
                    tell NewsletterMessage
                            make new to recipient at end of to recipients with properties {address:"mymom'semail@domain.tld", name:"newsletter e-mail list"}
                            [B]set i to 1
                            repeat length of NewsletterRecipientsNameList times                      
                                      make new bcc recipient at end of bcc recipients with properties {name:item i of NewsLetterRecipientsNameList, address:item i of NewsletterRecipientsAddressList}
                                      set i to i + 1
                            end repeat[/B]
                            tell content to make new attachment with properties {file:these_items} at after the last paragraph
                    end tell
                    activate
            end tell
    end open
    
    Actually, if you add a group using the mail interface, you'll notice that it changes the group to a list of addresses anyway. The problem was that you were trying to include all addresses in one address item. Also, I think Applescript list indexing begins with 1, but it might be 0. If so, just change the line
    Code:
    set i to 1
    to
    Code:
    set i to 0
    . Good luck.
     
  3. WM. thread starter macrumors 6502

    Joined:
    Apr 18, 2003
    #3
    Hmph, that's bogus. I should be able to create my own group recipient objects!! :)
    Ah, OK. The things a non-programmer doesn't think of...
    Excellent, thanks!! I don't have time to try this right now...I think it'll be tomorrow before I do, but I'll try it and report back!

    Oh, wait a minute, I just realized: what if I want this whole collection of addresses to appear as just one object, the group ("newsletter only")? That's what happens now, when my mom opens up the Address Panel and drags the group into the Bcc field. It shows up as one object, with the name of the group in bold. I'd like to replicate that as closely as possible, and I guess that's why I wanted to put everything into one recipient object. If this just isn't possible, I'm sure that'll be fine, but it would've been cool...

    Anyway, thanks again!!

    WM
     
  4. kylos macrumors 6502a

    kylos

    Joined:
    Nov 8, 2002
    Location:
    MI
    #4
    You could create your own group recipients object, but that would require substantial coding to accomplish.:) As you mentioned earlier, the mail script dictionary won't allow bcc group recipients. You can see that bcc recipient inherits recipient, which has a name and address field, which are single names and addresses. Apparently, Apple thought it wasn't worth the effort to create a group recipient class since the same result could be achieved with a bit of extra code on the users part. As you can see, my code is about 2 lines longer.

    Interesting. When I added a group in the bcc field in mail, it converted the group to a list of each individual in the group. So I checked the prefs, and there was a checkbox that allowed a group to be displayed as one entry on the line. That option is simply a top-level interface abstraction. Mail still keeps track of all your addressees individually. Apparently that option is simply not implemented at the lower levels, such as applescript. Oh, well, like you said, woulda been cool.

    Wait,wait,wait. She uses what thingy to select the script? And this is from the print dialog? I know about the print dialog's save as pdf option, and I've checked the script menu, but I don't see any pdf workflow menu thingy. This sounds interesting. Tell me more in more specific terms.
     
  5. WM. thread starter macrumors 6502

    Joined:
    Apr 18, 2003
    #5
    Yeah, I guess I should have really read that closely. I was so focused on the higher-level UI abstraction (that you mention below) of the group-as-one-object... :)
    Yeah. I'll be testing it momentarily (honest!!). :)
    Yeah. Maybe I should send some feedback to Apple about adding some AppleScript access to that...
    Ah, excellent, we've got some quality mutual teaching going on here. :) I'm talking about PDF Workflow Options, which were added in 10.2.4. Check out the article here for more details.

    I've known about those things for a while, but I just recently put 2 and 2 together and figured, hey, why don't I automate this process that my mom is always asking me about... :)

    WM
     
  6. WM. thread starter macrumors 6502

    Joined:
    Apr 18, 2003
    #6
    testing, testing, one two three...

    Okay, I've just gotten back from the other computer, and the script isn't working!! Mail fails to fill in the Bcc field, and Script Editor says there was an NSUnknownKeyScriptError (or is it ScriptKeyError...whichever...). Script Editor highlights the make new bcc recipient line.

    Note: after testing this with the complete script using the PDF Workflow menu, trying to do the whole attachment thing, I'm now using a fragment (i.e. without the on open bits) to test the addressing. But it should work, and it doesn't.

    I also confirmed that lists start with item 1, as you first thought. In the course of doing so, I confirmed that NewsletterRecipientsNameList is being created properly, so I don't think it's the problem (when I tried set i to 0 to check which item number lists start with, I got cannot find item 0 of {"Name #1", "Name #2", "Name #3"}, which looks like a properly formatted list to me).

    WM
     
  7. kylos macrumors 6502a

    kylos

    Joined:
    Nov 8, 2002
    Location:
    MI
    #7
    Applescripting is so much fun! :D Ok, just tested the script, what you need is as text at the end of each item i of NewsletterRecipients.... This will solve the problem. List items generally have to be typecast since a single list can contain items of different types.

    Also, I noticed a potential issue with including the name of the recipients in each recipient. If a recipient has more than one email address then the script will either fail or list the wrong name for email addys (to lazy to see which will occur:)), since your script asks for the values of the emails of a person.

    You can solve this simply by either removing the name field in the bcc recipient line, or by writing value of email 1 of people... This has the benefit of not sending multiple emails to individuals on your list. You could vary that line to specify home email addresses, if the recipients all have a home email defined. Just check the Address Book Dictionary to find out how.

    Indeed, that's very interesting. Just created the ~/Library/PDF Services folder and got the pdf workflow menu. Looks very useful. So how did you find out about it? Documentation seems only to be at the developer website. I tried the help application and it wasn't mentioned, and I've never seen it mentioned at all in any mainstream apple documentation. But that's whats great about this place, always learning new stuff.
     
  8. WM. thread starter macrumors 6502

    Joined:
    Apr 18, 2003
    #8
    OK, I figured out the problem. set NewsletterRecipientsAddressList to value of emails of people of group "newsletter only" as list generates a list of lists: one list for each person. Since everyone in this group has only one email address, this wasn't immediately obvious until I figured out how to use the Event Log (and/or it started working right...I swear it wasn't before...). It would have been nice to make each of those lists into a string--that way the script would be more flexible in case someone had multiple email addresses, although multiple emails would severely mess up the rest of it anyway--but it turns out that using value of email 1 of people of group etc. works fine too.

    The main problem now is that filling the Bcc field with all 100 or so of the recipients in the group is incredibly slow. Probably about one recipient per second. I could manually drag each recipient in from the Address Panel in about the same time. I did see references in some AppleScript documentation to a reference to stuff making it easier to work with large lists. And I didn't get all this to work with the attachment code, just with the fragment I used to test the addressing parts.

    Anyway, I have now been informed that my mom's figured out how to do this PDF business, so my script is made relatively moot. But it was a good learning experience. Big thanks to Kyle? for the help!!!

    WM
     
  9. WM. thread starter macrumors 6502

    Joined:
    Apr 18, 2003
    #9
    OK, that is just crazy. Check out the timestamps!!! We figured out the same thing at the same time! Gnarly!!

    ;)

    Anyway, I think your solution (item i...as text) works too--that's what I was trying to do before I (kind of) gave up and resorted to email 1.

    Thanks again
    WM

    edit: Did you check out that PDF Workflow Options business? It really is a cool idea, I think...
     
  10. kylos macrumors 6502a

    kylos

    Joined:
    Nov 8, 2002
    Location:
    MI
    #10
    Yeah, quite sweet! That's the thing about applescript, there always seems to be alot of debugging since the syntax is so loosely defined. Keep the value of email 1 it'll deal with multiple email addys much better.

    I think the speed issue is due to the overhead of the repeat loop and list item indexing. I'm not immediately aware of any decent way to deal with this. Keep checking your apple documentation to figure that out.
     
  11. kylos macrumors 6502a

    kylos

    Joined:
    Nov 8, 2002
    Location:
    MI
    #11
    Yeah, that is really sweet. I just set up an alias to my reciepts folder for e-commerce sites. Now I can save a few steps in the process of saving receipt pages as pdfs. I'm sure I'll be adding other items to the menu as I think of them. Where'd you come across that?
     
  12. WM. thread starter macrumors 6502

    Joined:
    Apr 18, 2003
    #12
    I think someone posted it when 10.2.4--and/or that developer documentation I linked to--first came out, and I've just held onto it as a little pearl of knowledge.

    It is kind of odd that it's not at least in the online help, although knowing Apple I wouldn't necessarily expect it to be there...I imagine it's primarily intended for AppleScripters and other power users (am I in that category? maybe I am), who will probably end up on developer.apple.com anyway. Although it is all buried in the Printing documentation; I only found it again because I knew to do a search for PDF Workflow. *shrug*

    Ooh, speaking of cool things buried on ADC (and I don't really spend that much time on it anyway), I bookmarked this a while back, mostly because my aunt recently got her master's in audiology (she went back to school), and along the way she had to do all kinds of speech sciences stuff (lots of phonemes all over the place) that that speech technology seemed somehow relevant to...anyway, it still seems kind of cool.

    WM
     
  13. WM. thread starter macrumors 6502

    Joined:
    Apr 18, 2003
    #13
    Yeah, that sounds good. One thing that's weird, although I'm not sure if it's relevant, is that all these single emails that the people in the group have are listed as work emails. I'm not sure how that would figure into the email 1 thing...if a home email was added later, would it take precedence over the work one, or the other way around?
    Sounds good. Since the need for my fancy script is less now, I won't be working on it too much, but if i get a chance I may look into a reference to some more.
    Mmm, there's a good idea, I've been saving a lot of receipts/acknowledgments as PDFs lately...
    Man, this has got to be the most confusing thread ever for anyone else to read. All kinds of edits and circular references and questions answered before they're asked... :)

    WM
     

Share This Page