MACRO: Get Common Lines (Words) of Two Lists [Examples]
~~~ VER: 1.0 2019-10-30 ~~~
Requires: KM 8.2.4+ macOS 10.12 (Sierra)+
(Macro was written & tested using KM 9.0+ on macOS 10.14.5 (Mojave))
DOWNLOAD Macro File:
Get Common Lines (Words) of Two Lists [Examples].kmmacros
Note: This Macro was uploaded in a DISABLED state. You must enable before it can be triggered.
Example Output
ReleaseNotes
Author.@JMichaelTX
PURPOSE:
-
Get Unique List of Common Lines of Two Lists
- If each line is a word, then it will be a list of words
- Duplicate lines are removed.
HOW TO USE
- First, make sure you have followed instructions in the Macro Setup below.
- Trigger this macro.
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
- Assign a Trigger to this macro.
- Move this macro to a Macro Group that is only Active when you need this Macro.
- ENABLE this Macro, and the Macro Group it is in.
.
-
REVIEW/CHANGE THE FOLLOWING MACRO ACTIONS:
(all shown in the magenta color)- Set Local__List1 and Local__List2 to your two lists
- Each line must NOT contain any extra whitespace
- To use the results, change the output of the Script Action to a Variable.
- Set Local__List1 and Local__List2 to your two lists
REQUIRES:
- KM 8.2.4+ (
- macOS 10.12+ (Sierra)+
- macOS must support ES6+
TAGS: @JavaScript @JXA @Lists @Arrays @Intersection
USER SETTINGS:
- Any Action in magenta color is designed to be changed by end-user
ACTION COLOR CODES
- To facilitate the reading, customizing, and maintenance of this macro,
key Actions are colored as follows: - GREEN -- Key Comments designed to highlight main sections of macro
- MAGENTA -- Actions designed to be customized by user
- YELLOW -- Primary Actions (usually the main purpose of the macro)
- ORANGE -- Actions that permanently destroy Variables or Clipboards,
OR IF/THEN and PAUSE Actions
USE AT YOUR OWN RISK
- While I have given this a modest amount of testing, and to the best of my knowledge 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.
JXA Script
'use strict'; // See http://www.w3schools.com/js/js_strict.asp
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
(function myMain() { // ~~~ automatically executed when this script is executed ~~~
var ptyScriptName = "Get Common Lines of Two Lists (Arrays)"
var ptyScriptVer = "1.0"
var ptyScriptDate = "2019-10-30"
var ptyScriptAuthor = "JMichaelTX"
/*
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
RETURNS: Unique Text List of Lines Common to Both Input Lists
• IF an unresolved ERROR occurs, THEN:
• Display Notification with error msg
• Throw an error with actual error msg, which will cause the KM Script Action to fail and terminate the Macro. (requires KM 9+)
KM VARIABALES REQUIRED:
• Local__List1
• Local__List2
KM VARIABLES SET:
• NONE
REQUIRES:
• KM 8.2.4+
• macOS 10.12+
• Support for ES6+
~~~~~~~~~~~~~~~~~~~ END HEADER COMMENTS ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*/
// --- SET CURRENT APP VARIABLE NEEDED FOR DIALOGS & StandardAdditions.osax ---
var app = Application.currentApplication()
app.includeStandardAdditions = true
// --- KM ENGINE APP ---
var kmInst = app.systemAttribute("KMINSTANCE"); // needed for Local & Instance variables
var kmeApp = Application("Keyboard Maestro Engine")
var scriptResults = "OK" // Set your results to this var
try { //~~~~~~~~~~~ START TRY ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// GET KM VARIABLES
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
var list1Str = kmeApp.getvariable("Local__List1", {instance: kmInst});
var list2Str = kmeApp.getvariable("Local__List2", {instance: kmInst});
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// PROCESS DATA
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
var list1 = list1Str.split('\n');
var list2 = list2Str.split('\n');
// --- Create Lower Case Version of List2 for Comparison ---
var list2lc = list2Str.toLowerCase().split('\n');
// --- Create Array whose elements are common (case insensitive) to Both Arrays ---
var commonList = list1.filter(x => (list2.includes(x.toLowerCase()) || list2lc.includes(x.toLowerCase())));
// --- Remove Dups ---
commonList = [...new Set(commonList)];
scriptResults = commonList.join('\n');
} //~~~~ END TRY ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
catch (oError) {
//console.log(oError);
if (oError.errorNumber === -128) { // User Canceled
scriptResults = "[USER_CANCELED]\n\n"
+ "SCRIPT: " + ptyScriptName + " Ver: " + ptyScriptVer
}
else {
var errNum = oError.errorNumber || "Custom Error";
scriptResults = "[ERROR]\n\n"
+ "SCRIPT: " + ptyScriptName + " Ver: " + ptyScriptVer + "\n"
+ "Error Number: " + errNum + "\n"
+ oError.message
var errMsg = oError.message;
//~~~~ Display Notification & Throw Error ~~~
// (will cause KM Script Action to fail, and terminate macro)
app.displayNotification(
errMsg,
{
withTitle: ptyScriptName,
subtitle: '[ERROR] Macro CANCELLED',
soundName: "Glass"
})
throw new Error(errMsg);
} // END if/else on ERROR Number
} //~~~~ END TRY/CATCH BLOCK ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//--- RETURN ---
return scriptResults
//=============== END OF MAIN SCRIPT =======================================
//### YOUR FUNCTIONS HERE ###
})(); // ~~~ function myMain() is automatically executed when this script is executed ~~~