Insert Clipboard Into AppleScript


I am trying to copy some email addresses from Airtable and send them emails via the script below. When I copy emails from Airtable they copy on different lines like this:

I suspect I might need to do a little regex to get them into shape to insert them with inverted commas etc as per the script, but my main issue is how to insert them at all. I believe I need to declare them as KM Variables, and them send them to AppleScript? I've seen examples, but don't get it!

Could anyone help please?

property SUBJ : "This is the email subject"
property SENDR : {""}
property RECEIPIENTS : {""}
property CC : {""}
property BCC : {"", "", ""}
property TESNIP : ""

tell application "Keyboard Maestro Engine"
	-- set SUBJ to getvariable "Subject" --
	-- set RECEIPIENTS to getvariable "To" --
end tell

tell application "Airmail"
	set newMail to make new outgoing message with properties {subject:SUBJ, sender:SENDR}
	make new to recipient at end of newMail's to recipients with properties {name:"", address:RECEIPIENTS}
	repeat with C in CC
		make new cc recipient at end of newMail's cc recipients with properties {name:"", address:C}
	end repeat
	repeat with B in BCC
		make new bcc recipient at end of newMail's bcc recipients with properties {name:"", address:B}
	end repeat
	compose newMail
end tell

Have you had a read of this?

Yes, I have.

Often when I write a post, it suddenly occurs to me where I should be looking. I had gotten this far:

Airmail.kmmacros (3.4 KB)

But am having no luck actually inserting it into the appropriate place on the original email. The macro either doesn't run (presumably because it has an error), or in the BBC field it has the variable name, not the actual variables. It's clear the property BCC :{"ASCMCEmails"} is incorrect, but I'm not sure how to correct it.

Edit: Had to update image to remove company info, but had already made edits suggested by @Nige_S

Oh, I'm seeing my variables don't even match up. I should be using MyASVar in the second script. Apologies. I'll work some more on it tomorrow.

No -- it's already text, which is what the "..." signifies in the AppleScript. But in the script it's a list of text items -- you keep the KM variable as you have it in your OP, pass it into the AppleScript, then convert lines of text to items in a list in your AppleScript using

set RECIPIENTS to (every paragraph of myASVar) as list

BUT... Each AppleScript action in your macro stands alone -- you can't get the value of ASMCEmails in one AS action then use it in another. Put that bit into the top of your "Airmail" script and see if that helps.

And you've spotted your variables don't match. But as it stands you are filling in the BCCs yet have no "To" recipient -- that greatly increases the chance of the email being treated as spam...

1 Like

So the script below is getting email to open but sending emails to "myasvar". I've tried {"myASVar"}, "myASVar", {myASVar} and myASVar.

Any further advice. Thanks in advance.

Airmail.kmmacros (3.1 KB)
Keyboard Maestro Export

But you've removed the bit where AppleScript sets the value of it's variable myASVar to the value of the KM variable ASMCEmails -- put that back in and that should solve your problem.

Thanks @Nige_S

Two questions:

  1. When setting a property to a variable, is it with or without curly brackets and/or inverted commas? ie: property BCC : myASVar ?

  2. The Wiki gives this example:

--- Set AppleScript Variables to KM Variable Name and Value ---
set myKMVar to "My KM Var Name"
set myASVar to "TBD" -- default value of KM variable, will be updated

I'm presuming that's two separate Variables, one being set to the KM Variable "My KM Var Name" and the other being set to text "TBD"?

Thanks in advance.

Airmail.kmmacros (3.2 KB)

In AppleScript, "..." denotes text. So

set address1 to ""
set address2 to ""

...sets the variable address1 to the text "" then the variable address2 to "".

{...} denotes a list. So

property BCC: {address1, address2}

...sets the property BCC to a list containing the values held by the variable address1 and address2. So it's like

property BCC: {"", ""}

If you just want to set a property to a variable it would be

property BCC: address1

No -- they are both being set to text. myKMVar is now the name of the KM variable you want to use, myASVar has been set to a default text value. You need to look further down the example to see how that text is then used:

set myASVar to getvariable myKMVar

AS resolves the value of myKMVar to "My KM Var Name", then gets the contents of KM variable "My KM Var Name" and puts them into myASVar (replacing the previously stored contents).


Hey Damian,

It appears that Airmail's AppleScript dictionary has improved since last I looked...

  • Please compile AppleScript actions when you post a macro image containing them.
    (Pressing the Enter key when the cursor is inside the action will get that done.)
    • I have fixed your images this time around.
    • Note – if the script won't compile there's something wrong with it.
      • Don't write AppleScript in Keyboard Maestro Actions – use Apple's Script or better yet Script Debugger (even the free ware lite version is miles ahead of the Script Editor).
        • Get your script working in a proper script editor and then paste it into your KM action.
  • Properties are generally placed at the beginning of a script, so they are convenient to locate and change.
    • This is not required by any means but is typical practice.
      • If memory serves it was required a very long time ago.
  • Please – for your own sake – never use variable names like myKMVar and myASVar in real scripts. Those were example names to try and make the syntax for each variable type (KM & AS) clear. Use properly descriptive variable names – you'll thank yourself when debugging time comes around.