How can I use an Applescript from within a program?

This question Is there a way to add all of the recipients of an email to a group/folder in contacts from Mail? seems to have an answer that would be useful for me, but I don't actually know how to use the script from within Mail.

Alternatively, it seems that Automator could be used too, but I'm also very unfamiliar with Automator. I'm looking to extend the tools and link them to solve general problems, with the maintenance of group membership being my current challenge.

How can I run an AppleScript from within a program?


Solution 1:

To use this (or any) script in a program like Apple Mail, you could create a service in Automator.

Launch the Automator program.

When it says Choose a type for your document, select Service and click Choose

Choose a type for your document

In the top dialog, select Service receives no input in Mail.app (or, the name of your program, or any application if that's what you want).

You will insert one action: From the Utilities group, double-click Run AppleScript.

Run AppleScript

Select the text that says

(* Your script goes here *)

and paste in the script you want to run. In your case, the script you want to paste is

  tell application "Mail"
    set theSelection to selection
    set theMessage to item 1 of theSelection
    set theSubject to subject of theMessage
    tell application "Address Book"
        set theGroup to make new group with properties {name:theSubject}
    end tell
    set theRecipients to to recipients of item 1 of theMessage
    repeat with a from 1 to count theRecipients
        set theRecipient to item a of theRecipients
        tell application "Address Book"
            set theName to name of theRecipient
            tell application "Mail" to set theAddress to address of theRecipient
            set thePerson to make new person with properties {first name:name of theRecipient}
            make new email at end of emails of thePerson with properties {value:theAddress}
            add thePerson to theGroup
        end tell
    end repeat
    set theRecipients to cc recipients of item 1 of theMessage
    repeat with a from 1 to count theRecipients
        set theRecipient to item a of theRecipients
        tell application "Address Book"
            set theName to name of theRecipient
            tell application "Mail" to set theAddress to address of theRecipient
            set thePerson to make new person with properties {first name:name of theRecipient}
            make new email at end of emails of thePerson with properties {value:theAddress}
            add thePerson to theGroup
        end tell
    end repeat
    tell application "Address Book" to save
  end tell

Once you have done that, go to the File menu and click Save.

Give the service a name you will remember, like "Add Recipients to Group".

Then when you are in Mail, you can select a message or messages and go to the Mail menu in the menu bar, then the Services menu in the Mail menu, and select the service Add Recipients to Group.