Parse String from File into Multiple Variables @RegEx @Example

###MACRO:   Parse String from File into Multiple Variables @RegEx @Example

~~~ VER: 2.1    2017-04-13 ~~~

####DOWNLOAD:
Parse String from File into Multiple Variables @RegEx @Example.kmmacros (14 KB)

This macro was built in response to this topic/post:
Find specific line in document


###Example Results

###ReleaseNotes

Author.@JMichaelTX

PURPOSE:

  • Read File and Parse into Multiple Variables

NOTICE:

  • This is just an example.
  • It has had very limited testing and most likely will require further testing and changes by you BEFORE you use it in production environment.

HOW TO USE:

  1. 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 Maco, not me. :wink:
      .
  • Assign a Trigger to this maro. I prefer TBD.
  • Move this macro to a Macro Group that is only Active when you need this Macro.
  • Enable this Macro (if needed).
    .
  • REVIEW/CHANGE THE FOLLOWING MACRO ACTIONS:
    (all shown in the magneta color)
    *

TAGS:

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 Varibles or Clipboards,
    OR IF/THEN and PAUSE Actions

REQUIRES:

  1. Keyboard Maestro Ver 7.3+ (don't even ask me about KM 6 support).
  2. El Capitan 10.11.6+
  • It make work with Yosemite, but I make no guarantees.

USE AT YOUR OWN RISK

  • While I have given this limited testing, and to the best of my knowledge will do no hard, 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 to Choose File

'use strict';
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

(function chooseFileKM() {    // ~~~ automatically executed when this script is executed ~~~
  
var ptyScriptName   = "Choose File Return POSIX Path"
var ptyScriptVer     = "2.1"
var ptyScriptDate   = "2017-04-13"
var ptyScriptAuthor = "JMichaelTX"
/*
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
PURPOSE:  Allow User to Choose File from Popup Window

RETURNS:  One of these, as text:
  • Actual Results of script if all goes well
    • POSIX path to selected file
    
  • "[USER_CANCELED]" at start of results if the user canceled something
  • "[ERROR]" at start of results if a script error occurred.
  
AUTHOR:  @JMichaelTX

KM VARIABALES REQUIRED:
  • SCPT__ParentFolder       [optional]: "~/Documents"
  • SCPT__ChooseFilePrompt  [optional]: "Choose FILE for KM Macro"
  • SCPT__FileType        [optional]: ['public.item']
  
  
TAGS:  @File @Prompt @Choose @Script @KM @JXA

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*/

var scriptResults = "TBD"  // Set your results to this var


try {
  //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  
  // --- SET CURRENT APP VARIABLE NEEDED FOR DIALOGS & StandardAdditions.osax ---
  var app = Application.currentApplication()
  app.includeStandardAdditions = true
  
  //--- SET SYSTEM UI SERVER FOR USE WITH DIALOGS IN KM ---
  var susApp = Application('SystemUIServer');
  susApp.includeStandardAdditions = true;

  
  // --- SET KME APP VARIABLE NEEDED TO GET/SET KM VARIABLES ---
  //      (remove if not needed)
  var kme = Application("Keyboard Maestro Engine");
  
  //--- GET KM VARIABLES ---  
  var defaultFolderPath   = kme.getvariable("SCPT__ParentFolder")       || "~/Documents";
  var choosePrompt         = kme.getvariable("SCPT__ChooseFilePrompt")   || "Choose FILE for KM Macro";
  var fileTypeStr         = kme.getvariable("SCPT__FileType")           || "public.item";

  defaultFolderPath = defaultFolderPath.replace("~", app.pathTo("home folder").toString())
  var  fileTypeList = fileTypeStr.split(/, |,/g)
    
  susApp.activate();
  
  var myFile = susApp.chooseFile({ 
    withPrompt:       choosePrompt,
    defaultLocation:   defaultFolderPath,
    ofType:           fileTypeList
    })
    
    /*
      Other File Types:
        • All File Types: ['public.item']
        • File Extension:  Use ext without period, like ["aup"]
        • Images:   ['public.jpeg', 'public.png']
        • Text:    ["public.text", "text", "public.html", "public.xml", "public.script"]
        • MS Word Documents:
            ["com.microsoft.word.doc", "com.microsoft.word.docx", "org.openxmlformats.wordprocessingml.document", "org.openxmlformats.wordprocessingml.document.macroenabled"]
        • see Apple System-Declared Uniform Type Identifiers for other "type" values
          https://developer.apple.com/library/ios/documentation/Miscellaneous/Reference/UTIRef/Articles/System-DeclaredUniformTypeIdentifiers.html
    */
    
  scriptResults = myFile.toString();
  
  //~~~~ END TRY ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

} catch (oError) {
  
  var msgLog;
  
  if (oError.errorNumber === -128) {  // User Canceled
  
    scriptResults =  "[USER_CANCELED]\n\n"
      + "SCRIPT: " + ptyScriptName + "   Ver: " + ptyScriptVer;
      
    msgLog = "User Canceled";
  }
  
  else {
    scriptResults = "[ERROR]\n\n"
      + "SCRIPT: " + ptyScriptName + "   Ver: " + ptyScriptVer + "\n"
      + "Error Number: " + oError.errorNumber + "\n"
      + oError.message
      
    msgLog = oError.message;
    
  } // END if/else
  
  
} // END catch
//~~~~ END TRY/CATCH BLOCK ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

return scriptResults

//======================== END OF MAIN SCRIPT =============================================



})();  // ~~~ function is automatically executed when this script is executed ~~~