###MACRO: Choose File to Open or Read into KM Variable @JXA
~~~ VER: 1.1 2017-04-27 ~~~
####DOWNLOAD:
Choose File to Open or Read into KM Variable @JXA.kmmacros (19 KB)
NOTE: You will need to change the "SCPT__ParentFolder" to a folder your prefer. It is the first Action.
###ReleaseNotes
Author.@JMichaelTX
PURPOSE:
- Provide User with a Choose File Dialog, and then perform some action on that file, like Open or Read into KM Variable.
HOW TO USE:
- If the Group for this macro is NOT Global, make sure the FrontMost app is one which is required for Macro Activation
- Just 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.
.
- Assign a Trigger to this maro. I prefer ⌃⇧O.
- Move this macro to a Macro Group that is only Active when you need this Macro.
- Enable this Macro.
- Enable/Disable/Add the Actions at the bottom of the Macro to perform the Actions you want.
. -
REVIEW/CHANGE THE FOLLOWING MACRO ACTIONS:
(all shown in the magneta color)- SET Default Folder for File
- SET Required File Extensions
TAGS: @File @Choose @Open @Read
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:
- Keyboard Maestro Ver 7.3+ (don't even ask me about KM 6 support).
- 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
'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 ~~~