Updating single elements in an array

Context: Improving window manipulations.

I am sure the answer is very simple, but how do you do the following:-

I have an array with values
e.g. testarray is 100,200,300,400
I just want to replace 300 with 600

So I would say
ix = 3
testarray[ix] becomes 600

How does the logic change if my test array is characters
e.g. testarray2 is ONE,TWO,THREE
and I want to change TWO to SIX.
so ix = 2
so testarray[ix] = SIX

I need to use ix, and not absolute index values e.g. 3,2, which work OK.

Finally can an array hold characters as well as numbers eg

ONE,TWO,THREE etc
or Mixed
100,TWO,300,FOUR?

I don't know what journey you are headed on, but if it requires much array manipulation, then I would immediately abandon array handling in KM, and use a tool like JXA (JavaScript), which has very powerful and easy to use array functions.

To be clear: KM does NOT have true arrays.

ALL KM variables are TEXT.
It has pseudo array handling, but it is very limited and mainly intended to get numeric values (stored as text) out of the "array".

To use KM Variable "arrays" like you are asking about will require the following:

  1. Use of dynamic Variable names, with Process tokens filter
  2. Use of complex Regex to do the replacement

The following Macro demonstrates the use of these.
I did this only to see if I could do it. Having done it, I personally would NOT use it in production.

Again, I highly recommend JXA for this task.

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:   Demo Manipulation of KM Pseudo Arrays [Example]

-~~~ VER: 1.0    2020-06-20 ~~~
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:

Demo Manipulation of KM Pseudo Arrays [Example].kmmacros
Note: This Macro was uploaded in a DISABLED state. You must enable before it can be triggered.


Example Output


ReleaseNotes

Author.@JMichaelTX

PURPOSE:

  • Demo Manipulation of KM Pseudo Arrays

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

USE AT YOUR OWN RISK

  • While I have given this limited testing, and to the best of my knowledge it will do no harm, I cannot guarantee it.
  • If you have any doubts or questions:
    • Ask first
    • Turn on the KM Debugger from the KM Status Menu, and step through the macro, making sure you understand what it is doing with each Action.

I am trying to keep my journey as simple as possible before learning JXA.
(I have signed up for an online course to do whilst we are locked down)

I did not think this would be that complicated,
essentially all I want to do (for the moment) is

I have an array with values
e.g. testarray is 100,200,300,400
I just want to replace 300 with 600

So I would say
ix = 3
testarray[ix] becomes 600

I started to write a KM Sub-Macro to do this, and it just requires too many Actions.
So I wrote a simple JXA script instead. You can use this script anywhere you want to replace the KM array element. Just need to set these two KM Variables as input:

  REQUIRES These KM Variables as Input:
    • Local__Array          -- KM Pseudo array, comma-deliminted
    • Local__ReplaceParams  -- IndexToReplace, NewValue

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.

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

Example Output

MACRO:   Replace Element of KM Array using JXA [Eample]

-~~~ VER: 1.0    2020-06-21 ~~~
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:

Replace Element of KM Array using JXA [Eample].kmmacros
Note: This Macro was uploaded in a DISABLED state. You must enable before it can be triggered.


1 Like

There is no direct way to do this in Keyboard Maestro, so there are only two indirect ways:

  • Break the string in to three parts (before),(element),(after) and then put it back together (before),(newvalue),(after).
  • Iterate through all the elements, and use the current value unless it is the desired index in which case use the desired new value.

The first case requires some special casing for the start and end values, so the second case is probably easier. More special casing will be required if you want the ability to index outside the bounds of the existing array.

Assing Element to Array.kmmacros (5.7 KB)

1 Like

Thanks to both Peter an JMichaelTx for solutions.
I shall now spend next week trying them out.
Very grateful.

1 Like