For each selected email in apple mail

I want to run a macro in apple mail across several selected emails. How can I achieve this? I am looking for a "for each selected email" but can't find it. This is a general question. Here is an example for a macro what I want to "loop". It searches for all urls in an email and then displays them :

mail links display.kmmacros (4.5 KB)

You can use a javascript for automation:

(() => {

const Mail = Application("Mail");
const urlRegex = /https:\/\/\S+/g  // (simplified regex)

const urls = Mail.selection().flatMap(
    email => [...(email.content().match(urlRegex) ?? [])]

return urls.join("\n");

1 Like

Thank you very much for your quick reply. I have tried it and it works.