Slice(s) of a Variable Array

Keyboard Maestro lets us get at parts of a string by their index number, where the parts are split on a custom delimiter:

( See the section on custom delimiters in manual:Variable Arrays [Keyboard Maestro Wiki] )

Here is a subroutine which lets us get at a continuous range (or 'slice') of zero-indexed parts of a string.

Variable Array SLICES Macros.kmmacros (5.7 KB)

For example:

If in the following long URL (see: Shorten Home Depot URL):

we want to drop one part in the middle:


and retain just the preceding and following text, leaving 4 (/-delimited) parts from the beginning, and 1 part from the end:

we could refer to the first four parts, starting at index 0, as 0,4

and refer to the last part with a negative index, counting back from the end, as -1

( These are the slice index conventions used by JavaScript, see Array.prototype.slice() - JavaScript | MDN )

Where more than one slice is wanted, slice arguments are separated by white space.

Expand disclosure triangle to view JS source of subroutine
    delimiter = kmvar.local_Custom_delimiter,
    parts = kmvar.local_Source_string.split(

return kmvar.local_Slice_indices.split(/\s+/u)
    sliceString => sliceString.split(",")
    sliceIndices => parts.slice(...sliceIndices)