Split Text - Plugin Action


#1

# Split Text ***by Ian Kirkland***

Description

This plugin action takes input text and splits it into multiple strings based on a user-defined delimiter or ‘split point’ and is useful for getting different parts of a URL, IP address, filename, extension, email address, phone number, you name it. The split point can be a character, a string, or even a Keyboard Maestro token; anything you want to use to chop text into individual parts. Each of these parts are then saved to their own dynamically-generated Keyboard Maestro variable using a user pre-defined name-prefix and an automatic 2-digit serialization. To keep things within reason, the limit is 99 per series. The naming convention uses the following scheme:

Naming Pattern

[variable name-prefix]_##

Example

My Variable_03

Note that this action includes three different results:

  1. Dynamically-generated, serialized Keyboard Maestro variables, one for each resulting ‘piece’ of text (limit: 99)
  2. Dynamically-generated Keyboard Maestro ‘array’ variable with named suffixed by “_all”
  3. Built-in results option (save to variable, save to clipboard, display in a window, etc.) that returns a Keyboard Maestro flavored ‘array’ (i.e. a return-delimited list) of all the resulting pieces

Action Screenshot

Parameters & Options

Input text
Your input text. This can include tokens like %CurrentClipboard%, %Variable%Foo%, %MacIPAddress%, etc.

Split text at
This is the character or string of characters (including tokens) the action will use to split your input text. For instance, splitting the word “yes” using a ‘Split text at’ of “e” will yield two resulting substrings: “y” and “s”. Likewise, splitting the text “I-Love-Automation” using a split point of “-Love-” will give you “I” and “Automation”.

Variable series prefix
As mentioned in the description section above, this field is used to define a common prefix-name for your series of result variables. Serial numbers, or ‘counters’ ranging from “_01” to “_99” will be automatically appended to the end of each variable name as a suffix. (e.g. “My Variable-03”).

For example, if you split an IP address (e.g. 192.168.0.1) using “.” as a split point and “IP_Segment” for your ‘Variable series prefix’, you’ll end up with four appropriately-named and serialized variables; one for each of the four resulting pieces of the IP address:

Variable Name = Value
IP_Segment_01 = "192"
IP_Segment_02 = "168"
IP_Segment_03 = "0"
IP_Segment_04 = “1”

figure 1 )
figure 2 )

Although you won’t see immediate feedback on the validity of your variable name, the action will parse it so that your variable names are kosher. The action also generates an ‘array’ variable (suffixed by “_all”) that is also available through the action’s built-in results dropdown menu option which is described below. Immediately below are a couple screenshots that will help explain some of this option’s (and this action’s) functionality.

Results
The results dropdown (located at the bottom of the action) should be familiar to most Keyboard Maestro users. It is a built-in feature for ‘piping’ the action’s output into something else such as the clipboard, a variable, a window, etc. Rather than returning an individual substring, however, the results dropdown returns the action output as an ‘array’. I use quotes here because it’s not really a true array but rather a return-delimited list in string format (similar to the one you get when copying multiple objects and viewing the results in plain text). It pairs nicely with a “For Each” action that operates on lines / paragraphs.

Additional Notes

  • If you come up with any creative uses for the action, please share them here in the comments
  • Please report any bugs at: ianthekirkland@me.com

Download

Download Action:  Split_Text.zip (887 KB)

Legal

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.



RegEx: How to Split String into Words
Save parts of the text to different named clipboards
Macro: Set Default Printer Based on Location
#2

Hi Ian

This looks very good. And I just tested it.
I often use KM to loop over tab-delimited lines to process these.
Until now I have used the Search variable like this:

Your plugin can replace some of this.

But in my test it did not handle special characters in danish (æøå ÆØÅ) correctly.

Here is the macro:
CloudApp

And this is the text I copied to the clipboard:

Navn	Fornavn	Test.af.Noget	Eller noget første
Navn	Fornavn	Test.af.Noget	Eller noget tredje
Specialcharacters	æøå	ÆØÅ	üïä

#3

Excellent! Thank you for the feedback, Jimmy. I probably need to look at which items in the chain are using which encoding. Which encoding does Danish generally use in contexts like this? Latin/ISO-Western European? UTF-8? MacRoman? Other? Just curious.

Also, I’m wondering if it’s the feature that ‘validates’ variable names(?) that’s causing issues. Regardless, I’ll let you know if/when I can fix the character handling issue.

Thanks again for your feedback!

BTW: thanks for the macro! Looks sweet!


#4

I do not know much about text encodings, but think that UTF-8 would be the best way to go.

The macros where I use this is for is when I am supplied an Excel sheet of addresses, and then I need to use this info to lookup information only and add to this row in Excel.
But using a tab-file as an intermediate file, I can the automate a lot of it with KM.


#5

Ian, many thanks for bulding and sharing this plugin. It looks very powerful.

I didn’t see any installation instructions, so I thought I’d share what I found on the KM Documentation page:

Installation of KM Plug In Actions

Keyboard Maestro has support for user written and contributable plug in actions. You can get more plug in actions from our web site or the forum and you can create and optionally contribute your own.

You can drop a new plug in action .zip archive on the Keyboard Maestro application dock icon to install it (note that to update a plug in action you must manually remove it from the ~/Library/Application Support/Keyboard Maestro/Keyboard Maestro Actions folder before re-installing it).


#6

I love this macro. Is there any chance that you’ll allow it to support more than 99? Perhaps any depending on how many there are?