Script that creates a mailbox (folder) based on the email subject I adopt the inbox zero approach, which means that I want to keep my inbox as close to empty as possible.

To achieve this, I need to archive emails quickly. The best method I found was to write a script that, when I right-click on an email, the script creates a new folder based on the email subject and then move this message to this folder.

This worked perfectly on Microsoft Outlook on Windows.

Now I switched jobs and I am using a Mac. Does anyone know how to achieve a similar result using Mail?

Thank you very much in advance.

For your reference, here is the script I used on Windows:

Sub folderCreation()

    Dim o_App As Object
    Dim obj_NameSpace As NameSpace
    Dim obj_folder As Folder
    Dim obj_NewFolder As Folder
    Dim obj_mail As MailItem
    Dim s_Subject As String
    Dim s_Date As Date

    Set o_App = Application
    If Application.ActiveExplorer.Selection.Count = 0 Then
        MsgBox "No item was selected!"
        Exit Sub
    End If

    Set obj_NameSpace = o_App.GetNamespace("MAPI")
    Set obj_folder = obj_NameSpace.PickFolder    

    If obj_folder Is Nothing Then
        MsgBox "You did not make a selection"
        Exit Sub
    End If

    For Each obj_mail In Application.ActiveExplorer.Selection
        s_Subject = obj_mail.Subject
        s_Date = obj_mail.ReceivedTime
        Set obj_NewFolder = obj_folder.Folders.Add(Format(s_Date, "yyyy.mm.dd / ") &amp; s_Subject)
        obj_mail.Move obj_NewFolder
    Next obj_mail   
End Sub
I was able to get help from the official Apple Discussions forum, thanks to Barney15-E. Here's the reply:

In order to trigger it with a shortcut, you would need to create an Automator Quick Action.
Within that Quick Action you can run this script:

tell application "Mail"
    set selectedMessages to selection
    set mailboxName to subject of item 1 of selectedMessages
    set messageAccount to account of (mailbox of item 1 of selectedMessages)
    set newMailbox to make new mailbox at (end of mailboxes of messageAccount) with properties {name:mailboxName}
    repeat with eachMessage in selectedMessages
        set mailbox of eachMessage to newMailbox
    end repeat
end tell


To create the Quick Action, open Automator.
Select Quick Action from the document type selection.
Set the Quick Action to accept No Input in Mail.
Set the Image and Color to anything you want.
Drag a "Run AppleScript" action into the workflow area.
Replace everything between the on run/end run statements with the script above.
Save the Quick Action with a suitable name.

To give the Quick Action a shortcut, open Keyboard System Preferences.
Click on Shortcuts tab.
Select Services from the list.
Find your Quick Action (probably under General).
Add the desired shortcut.

If you want to file each selected message into its own folder, use this script instead:

tell application "Mail"
    set selectedMessages to selection
    repeat with eachMessage in selectedMessages
        set mailboxName to subject of eachMessage
        set messageAccount to account of (mailbox of eachMessage)
        set newMailbox to make new mailbox at (end of mailboxes of messageAccount) with properties {name:mailboxName}
        set mailbox of eachMessage to newMailbox
    end repeat
end tell


If you want a version that checks for at least one message selected, use this:

tell application "Mail"
    set selectedMessages to selection
    if selectedMessages is not {} then
        set mailboxName to subject of item 1 of selectedMessages
        set messageAccount to account of (mailbox of item 1 of selectedMessages)
        set newMailbox to make new mailbox at (end of mailboxes of messageAccount) with properties {name:mailboxName}
        repeat with eachMessage in selectedMessages
            set mailbox of eachMessage to newMailbox
        end repeat
    else 
        display dialog "You did not select a message."
    end if
end tell