Guidance on Clipboard & text extraction

Hello all,

Over the past few years, the single area where I feel I am under-utilising KM the most, has been in regards to the Clipboard Manager…

I’ve finally stumbled across a scenario where I think KM can be of great assistance, but figured I would check things here first, before spending hours chipping away at something that could’ve been done differently, and with far less effort.

In a nutshell – is the clipboard manager, and building a macro around it, what I should be looking at??


I receive emails from staff that are in the following format in Outlook/

“Surname, Initial, Mr./Ms <staff>”

To call up the details of that staff member in our CMS, I need to input (in plain text) their staff number into the CMS.

Currently, a copy and paste of the above obviously tries to input the entire string, which doesn’t work since the input field only allows input of 9 digits, being the staff number.

Can KM do the following?

1.) I copy the entire string using a particular keyboard shortcut(?)
2.) This gets placed on a ‘named clipboard’, in plain text
3.) KM runs a macro(?) of sorts, and only(?) extracts the staff number from the named clipboard
4.) On my hitting paste, presumably with a different keyboard shortcut(?), the end result of this extraction is pasted into the CMS?

Could that work?

Many thanks!

Hey Neal,

<shrug>   :sunglasses:

Your process description has too many holes in it to say for certain.

Do you mean you are using both Microsoft and

Which versions on what OSX?

What's a “CMS”? A content management system, or something else?

Which one? What software?

The way I'd want this to work is something like this:

A) I'm reading an email from staff that, and I want to look them up in the CMS.
B) I press 1 key, and the process is done.

Given Mail and/or Outlook the front end of this is probably doable.

Without knowing the CMS software/system I cannot speculate on the back end.

Can the back end of this be done manually? Sure.

But without understanding the process a little better it's hard to advise.

In general – NEVER use a named clipboard – UNLESS it is of REAL benefit to do so.

People who use Keyboard Maestro often think they can copy and paste to/from a named-clipboard and leave the contents of the system clipboard untouched, but this is NOT true.

The system clipboard is the ONLY mechanism by which to copy/cut something as clipboard data.

(Some applications like TextWrangler are scriptable and can get and/or set the selection directly (usually as text) without using the clipboard at all).

Unless you deliberately restore the system clipboard it is changed every time you cut/copy/paste to/from a named-clipboard.

Therefore you should always use the system clipboard for clipboard operations – UNLESS you really do need to store it away.

If you DO need to store it away it is often better to use a variable than a named clipboard, but this depends upon several factors such as whether you're dealing with styled or plain text, how long you need to store the data, etcetera.

Here's an example of how to extract the Staff-ID of your example from using AppleScript and the Satimage.osax using an Execute an AppleScript action:

# REQUIRES Satimage.osax { }
tell application "Mail"
  set selectedMessageList to selection
  if selectedMessageList ≠ {} then
    set selectedMessage to item 1 of selectedMessageList
    tell selectedMessage
      set messageText to its content
    end tell
  end if
end tell

  # •• NOTE •• Discourse mangles the code in the next line.
  # •• REMOVE •• the bullet in front of the 1 in the next line.
  set staffID to find text "(\\d{9})" in messageText using "\\•1" with regexp and string result
  set the clipboard to staffID
  tell application "myCMSapp" to activate
on error eMsg
  error "Find Staff ID Failed!" & return & return & eMsg
end try


Due to poor rendering of some elements in the script I'm posting a downloadable text file:

Extract Staff-ID (1.1 KB)

Added 2016/01/29

After fighting with JXA (JavaScript for Applications) for 45 minutes I've pared that down to this (which doesn't require a 3rd party scripting addition):

(function () {

  // Declare variables.
  var mailAppRef, msgContentStr, reMatchedStr;
  // Assign the Mail application object to a variable for later use.
  mailAppRef = Application("Mail");
  // Include functions from the StandardAdditions.osax.
  // Without this you cannot set the Clipboard.
  mailAppRef.includeStandardAdditions = true;

  // Get the textual contents of the first selected message in
  msgContentStr = mailAppRef.selection()[0].content();
  // Use a regular expression to find the desired text in the messsge contents.
  reMatchedStr = msgContentStr.match(/(\d{9})@our\.company\.email\.address\.com/);

  // Set the Clipboard


To be run from an Execute a JavaScript For Automation action.

* I don't have any error-handling in the JavaScript, because I don't know how to do that yet.

You can use AppleScript to grab the message text and then use Keyboard Maestro's regular expressions actions to extract your ID-number.

You can select some text in your mail application and then use Keyboard Maestro to extract your ID-number.

And that brings us back to my way of looking at this task – if I did it regularly I'd want it to be as effortless as possible.

More than you wanted to know?   :smiley:


1 Like

Holy Moses Chris! :blush:

Thank you SO much for the effort you put into this. I will give it a bash, and see what comes from it.
I’ll take the plunge on satimage.osax (I presume I need to download and install it?) – and then give the Java go!

This is something I am doing countless times a day, so it will be worth it if I can remove the friction using automation.

Just to quickly answer your questions:

I use both Outlook (Mac) and – the latter has Mailtags and Mail-Act-On installed, so I’m sometimes in there, making sure the rules are running to get keywords and tags set-up, to fire off specific rule outcomes. The former plays nicely with the company’s Exchange, particularly where meeting requests are being set-up, and my (long-term) use of Categories.

Latest versions (Outlook 15.17.1 and Mail 9.2) on El Capitan.
Meant Client Management System – not sure what the backend is, it’s a in-house design that runs the gamut of legacy software (Oracle, SAP, MS Sharepoint etc. all in the mix)… Sorry, but don’t really know enough to be more specific!

Lastly, thank you for the explanation about the Named/System clipboard – food for thought!

As it stands, I fortunately don’t need it to be stored for long, essentially, the mail comes in with a query, I copy the necessary staff ID, jump into my browser, and drop it into the relevant input field, and call up their record. Find what I need to know, respond, and move on.

As mentioned – thanks for all the help – really appreciated! :thumbsup:

Hey Neal,

Just catching up.

Yes, you need to install the Satimage.osax. (You may need to reboot to get the system to notice.)

Use the AppleScript instead of the JavaScript.

It’s more convenient to write in AppleScript than JavaScript (for me at least).

You’re using Safari as the front-end of your CMS?

I have Outlook 2011, so I’ll have a crack at extracting text here in a bit.


Of course, and as usual, @ccstone is correct. However as a novice, it is often better to start a bit slower with a workflow that requires multiple applications.

In this case, lets assume you first copy the address line, then let Keyboard Maestro extract the staff number, and then you go to your CMS, click in the search field, and Paste the staff number.

So now you just need the macro to extract the staff number. This is easily done with a Search and Replace on the clipboard using a regular expression.

That will basically take your copied address and extract out the nine digit number from it. If your staff numbers are not always nine digits, then you might want something like this instead:


With this trivial macro, you've not probably cut half the time of the process off, since it does the tedious part.

After that, you start extending:

  • Well, after extracting the number, I want to go to my CMS, and building the actions for that is probably not too hard.
  • And then I want to do a search, and that is probably not too hard to do either.
  • And then it is just Command-V, Return.

The point being, even the trivial macro is useful, and then you can add each bit in turn to simplify the process further.

And remember, there is nothing at all wrong with breaking a tricky process into a a sequence of macros and manual actions if there are parts you can't easily figure out how to automate - the macros in between each part can still improve your speed and reliability.

  • I don't have any error-handling in the JavaScript

One simple pre-emptive approach is to use immutable variables – immediately binding the variable name (in the declaration statement) to a value which:

  • is not going to change
  • is conditional (the ternary conditional expression works well here).

More generally, if you are running El Capitan, a good way to quickly develop a sense of the JavaScript for Automation data model and interfaces is to step through your script in the Safari debugger.

(I personally upgraded to El Capitan just for this, and found it rewarded the effort)

function run() {

    var m = Application("Mail"),
        lstSelns = m.selection(),
        strContent = lstSelns.length ? lstSelns[0].content() : '',
        lstMatches = strContent ? strContent.match(/(\d{9})\@/) : [];
    if (lstMatches.length > 1) {
        m.includeStandardAdditions = true;
        return m.theClipboard();
1 Like

Many thanks for all the assistance given here!

I’m probably going to go (in the interim) with @peternlewis suggestion - since it’s what I am currently able to understand and implement straight away >> whilst doing my utmost to get the point where I am able to use/implement @ccstone and @ComplexPoint suggestions… Will be incredibly useful as another source of learning!

Really appreciate the willingness to set it all out in such a manner that even programming neophytes can try and aspire towards giving it a go! :+1:

Hey Neal,

Zoiks! It’s taken me 12 days to get back to this…

Here’s the basics of the Outlook version:

# REQUIRES the Satimage.osax AppleScript Extension.
# Tested on OSX 10.11.3 with Outlook 14.5.7

tell application "Microsoft Outlook"
  set selectedMessage to selection
  if class of selectedMessage is not incoming message then
    error "ERROR!" & return & return & "Selection Failure!"
  end if
  tell selectedMessage to set messageText to plain text content
end tell

set staffID to find text "(\\d{9})" in messageText using "\\1" with regexp and string result

# set the clipboard to staffID
# tell application "myCMSapp" to activate


NOTE: the improperly rendered line in the script above should look like this:

set staffID to find text “(\d{9})” in messageText using “\\1” with regexp and string result