6

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?

bmike
  • 235,889
Alan Munn
  • 486

2 Answers2

7

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.

Daniel
  • 34,803
1

Enable the Script menu bar icon via Script Editor > Settings. Note that you can also set application scripts to appear at either the top or bottom of the script menu. Script Editor Settings dialog with "Show Script menu in menu bar" option highlighted Save your script as a Script (.scpt extension) in ~/Library/Scripts/Applications/<Application name>/, replacing <Application Name> with the name of your application – in this case, Mail. You might have to first create the Scripts folder and/or required subfolders.

Alternatively, save your script in the iCloud storage Script Editor folder and link to it from the above folder with a Finder alias or symbolic link. This is handy if you use more than one Mac as you only have to maintain one version of each script file, and may also help you avoid losing scripts when you switch to a new Mac.

The script menu bar icon looks like a papyrus scroll Script menu bar icon and my script menu looks as below when accessed from Mail and set to show application scripts at the top.

Script menu with "Contact group from email" script highlighted

This solution differs from the Automator method in that scripts are accessed from a different part of the UI (the script menu) and only appear there when the relevant application is frontmost. However, they can easily be made to appear at any time by saving them in ~/Libary/Scripts (for the user) or ~/Library/Scripts (for all users).

Louie Louie
  • 1,435