Remove Dup Items in List [Example] Macro (v9.2)

Use Case

  • You have a list of items, one per line, that contain duplicate items
  • You need a unique list of items

As I mentioned in the Replacing Duplicates Using RegEx topic, this can easily be done using JavaScript. I said it could be done in one line, but actually it takes 4 lines of code unique to this script:

//--- YOUR CODE HERE ---
var sourceStr = kmeApp.getvariable("Local__SourceStr",  {instance: kmInst});
var sourceList = sourceStr.split('\n');
var uniqueList = [... new Set(sourceList)];

scriptResults = uniqueList.join('\n');

All I had to do is enter those 4 lines in my JXA script template (which you should have if you write even a little bit of JXA scripts).

I hope you find this helpful.

MACRO:   Remove Dup Items in List [Example]

-~~~ VER: 1.0    2021-06-11 ~~~
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:

Remove Dup Items in List [Example].kmmacros
Note: This Macro was uploaded in a DISABLED state. You must enable before it can be triggered.


ReleaseNotes

Author.@JMichaelTX

PURPOSE:

  • Remove Dup Items in List [Example]
    • Make use of the JavaScript Set function

HOW TO USE

  1. First, make sure you have followed instructions in the Macro Setup below.
  2. See the below "How to Use" Comment Action
  3. This macro is just an example written in response to a user's request. You will need to use as an example and/or change to meet your workflow automation needs.

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. ??
      .
      Make These Changes to this Macro
  1. Assign a Trigger to this Macro .
  2. Move this macro to a Macro Group that is only Active when you need this Macro.
  3. ENABLE this Macro, and the Macro Group it is in.
    • For more info, see KM Wiki article on Macro Activation
      .
  • REVIEW/CHANGE THE FOLLOWING MACRO ACTIONS:
    (all shown in the magenta color)
    • SET Source String
      • List of Items (one per line) thata contains dups

REQUIRES:

  1. KM 9.0+ (may work in KM 8.2+ in some cases)
  2. macOS 10.12.6 (Sierra)+

TAGS: @Example @JavaScript @JXA @Dups @Lists @Set

4 Likes

If it makes it any easier, there is a shell command (in Terminal) named "uniq". Look at "man uniq" from Terminal.

But then that has some requirements the Chris @ccstone pointed out.

Once you have the JXA script, it is just a easy to use, maybe easier, than a shell script.

Important point – JavaScript will handle Unicode text more seamlessly than the shell.

1 Like