"Uppercase" the First Word in a Clipboard

I have a requirement to switch something like

Parker, Douglas, Colorado
(which is city, county, state)

to

COLORADO, Douglas, Parker.

I have a bash script that will invert the "fields", and have created a macro where I select "Parker, Douglas, Colorado" in text and call Keyboard Maestro through a keystroke. This copies the string into a named clipboard, calls a bash script to invert the delimited string, and then pastes it back into the same location. However, I cannot figure out how to UPPERCASE just the first field (i.e. the part before the comma, as this could just as easily be NEW YORK, Oswego, Orwell). I suspect that if I were a smarter awk user, I might be able to do this in the script. But since I'm not such a smart awk user, I was hoping I could figure out a way for Keyboard Maestro to do this.

Any ideas?

Thanks

Danita

Regex for the win !

without the use of bash script

third word of clipboard goes FIRST (in uppercase) Macro (v9.0.6)

third word of clipboard goes FIRST (in uppercase).kmmacros (2.3 KB)

3 Likes

Regex for the win !

: - )

( and of course, regular expressions, like salt, work best in tiny quantities )

Script is probably not what you are after, but here FWIW is a JavaScript approach, with just a tiny regular expression for the split:

flipped triple, first uppercase.kmmacros (18.6 KB)

JS source
(() => {
    'use strict';

    const main = () => {
        const
            s = Application('Keyboard Maestro Engine')
            .getvariable('commaTriple'),
            ws = s.split(/,\s+/g).reverse();

        return [toUpper(ws[0])].concat(ws.slice(1))
            .join(', ');
    };

    // toUpper :: String -> String
    const toUpper = s =>
        s.toLocaleUpperCase();

    return main();
})();

@danitaz:
Great solution. Shows how a simple RegEx can easily be used in KM. No script required.

I find RegEx to be one of the most useful languages to learn. Very powerful, lots of support, and can be used just about everywhere.

Regular expressions (RegEx or RegExp) are extremely powerful, but have an initial steep learning curve that is often intimidating. But once you get over that initial hump, and you continue to write new RegExp, it will become much easier.

I do all of my RegEx development at this free website:

You may also find these sites helpful:

3 Likes

And another approach is to use JSON tokens:

CSV snippet reordered, case shifted.kmmacros (20.0 KB)

Reusable Execute JavaScript for Automation snippet for splitting, on some delimiter, to a JSON list:

(() => {
    'use strict';

    const 
        kme = Application('Keyboard Maestro Engine'),
        kmVar = k => kme.getvariable(k);

    return JSON.stringify(
        kmVar('somePhrase')
        .split(kmVar('someDelimiter'))
    );
})();

ּּּּּ

Or if you prefer, in a more basic (definition)(application) style
(
    kme => (
        kmVar => JSON.stringify(
            kmVar('somePhrase')
            .split(kmVar('someDelimiter'))
        )
    )(
        k => kme.getvariable(k)
    )
)(
    Application('Keyboard Maestro Engine')
)
1 Like

I should have been more specific, because sometimes it's not just 3 fields. These are from census records. Sometimes they are even like "Area D, Tulsa, Tulsa City, Tulsa, Oklahoma, United States". I would want this to be "OKLAHOMA, Tulsa, Tulsa City, Tulsa, Area D" (i.e, I do a filter to remove United States or USA depending on the record). BUT Since I already have a macro that gets them into the proper order, using RegEx should get the first field capitalized.

Thanks

Danita

If all you need is to make the first field all caps, that is very easy with RegEx:

Example Output

image

Below is just an example written in response to your request. You will need to use as an example and/or change to meet your workflow automation needs.

Please let us know if it meets your needs.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

MACRO:   Make first Field All Caps [Example]

-~~~ VER: 1.0    2020-11-07 ~~~
Requires: KM 8.2.4+   macOS 10.11 (El Capitan)+
(Macro was written & tested using KM 9.0+ on macOS 10.14.5 (Mojave))

DOWNLOAD Macro File:

Make first Field All Caps [Example].kmmacros
Note: This Macro was uploaded in a DISABLED state. You must enable before it can be triggered.


Here is another solution which will work for a LIST of locations (one line per location) and just one location,

If all you need is to make the first field all caps, that is very easy with RegEx:

Example Output

image

Below is just an example written in response to your request. You will need to use as an example and/or change to meet your workflow automation needs.

Please let us know if it meets your needs.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

MACRO:   Make first Field in List All Caps [Example]

-~~~ VER: 1.0    2020-11-07 ~~~
Requires: KM 8.2.4+   macOS 10.11 (El Capitan)+
(Macro was written & tested using KM 9.0+ on macOS 10.14.5 (Mojave))

DOWNLOAD Macro File:

Make first Field in List All Caps [Example] .kmmacros
Note: This Macro was uploaded in a DISABLED state. You must enable before it can be triggered.


OK, just having fun now with RegEx.
To show the power and simplicity of using RegEx in KM, here is a complete solution that both moves the last field to the first field, and makes it all upper case.

Example Output

image

Below is just an example written in response to your request. You will need to use as an example and/or change to meet your workflow automation needs.

Please let us know if it meets your needs.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

MACRO:   Move LAST Field in List to First field and Make All Caps [Example]

-~~~ VER: 1.0    2020-11-07 ~~~
Requires: KM 8.2.4+   macOS 10.11 (El Capitan)+
(Macro was written & tested using KM 9.0+ on macOS 10.14.5 (Mojave))

DOWNLOAD Macro File:

Move LAST Field in List to First field and Make All Caps [Example] .kmmacros
Note: This Macro was uploaded in a DISABLED state. You must enable before it can be triggered.


ReleaseNotes

Author.@JMichaelTX

PURPOSE:

  • Move LAST Field in List to First field and Make All Caps

NOTICE: This macro/script is just an Example

  • It is provided only for educational purposes, and may not be suitable for any specific purpose.
  • It has had very limited testing.
  • You need to test further before using in a production environment.
  • It does not have extensive error checking/handling.
  • It may not be complete. It is provided as an example to show you one approach to solving a problem.

REQUIRES:

  1. KM 8.0.2+
  • But it can be written in KM 7.3.1+
  • It is KM8 specific just because some of the Actions have changed to make things simpler, but equivalent Actions are available in KM 7.3.1.
    .
  1. macOS 10.11.6 (El Capitan)
  • KM 8 Requires Yosemite or later, so this macro will probably run on Yosemite, but I make no guarantees. :wink:

MACRO SETUP

  • Carefully review the Release Notes and the Macro Actions
    • Make sure you understand what the Macro will do.
    • You are responsible for running the Macro, not me. :wink:
      .
  • Assign a Trigger to this maro.
  • Move this macro to a Macro Group that is only Active when you need this Macro.
  • ENABLE this Macro.
    .
  • REVIEW/CHANGE THE FOLLOWING MACRO ACTIONS:
    • ALL Actions that are shown in the magenta color

3 Likes

Any documentation link for explaining \U\2, \E\1?
Thanks.

for \U and \E https://wiki.keyboardmaestro.com/manual/Tokens#Text_Case_Conversions

\1 \2 are the first and second capture groups in the regex search ($1 and $2 do the same thing and seem to be more frequently used)

1 Like