###MACRO: [SE] Set Language of Script Editor Document using JXA
####Download:
[SE] Set Language of Script Editor Document.kmmacros (9.2 KB)
The primary purpose of this post is to provide an example of how to set the script language in Script Editor using JavaScript for Automation (JXA). Most likely you will want to incorporate the script in this macro in your own macro/script when making a new script.
It is really fairly simple once you know the few commands required.
Please feel free to ask any questions, challenge my approach, and/or make suggestions for improvement.
Author: @JMichaelTX
PURPOSE:
• Set the Language of the FrontMost Script Document in Script Editor
• Language is set in the KM Variable "ScriptLang"
HOW TO USE:
(1) Create a script document in Script Editor, and make it the FrontMost window
(2) Set the KM Variable "ScriptLang" to the language you want to use
(the below Action does this)
(3) Run this macro (set the trigger of your choice first)
REQUIRES:
• macOS Yosemite 10.10.5+
• Keyboard Maestro 7.1+
• Script Editor
###Here's the Script
/*
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
PURPOSE:
• Set the Language of the FrontMost Script Document in Script Editor
• Language is set in the KM Variable "ScriptLang"
VER: 1.0 2016-07-28
AUTHOR: JMichaelTX (in most forums)
Find any bugs/issues or have suggestions for improvement?
Reply below or PM me.
REQUIRED:
1. Mac OS X Yosemite 10.10.5+
2. Mac Applications
• Script Editor
• Keyboard Maestro
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*/
function run() {
'use strict';
var thisScriptName = "Set Language of FrontMost Document in Script Editor"
var kmeApp = Application("Keyboard Maestro Engine");
var seApp = Application('Script Editor');
seApp.includeStandardAdditions = true
//--- GET SCRIPT LANGUAGE FROM Keyboard Maestro VARIABLE ---
var scriptLangStr = kmeApp.getvariable('ScriptLang');
//--- VALIDATE SCRIPT LANGUAGE ---
if (["AppleScript", "JavaScript"].indexOf(scriptLangStr) === -1) {
var msgStr = "Script Name: " + thisScriptName + "\n\nERROR: Invalid Script Language: " + scriptLangStr;
return msgStr; // *** INVALID LANGUAGE ***
}
//--- GET A REF TO THE DOCUMENT (SCRIPT) ---
// • documents[0] is the script in the FrontMost Window
var oDoc = seApp.documents[0]
//--- SET THE LANGUAGE OF THE SCRIPT DOCUMENT ---
oDoc.language = seApp.languages.byName(scriptLangStr)
return "OK"
} //~~~~ END OF function run() ~~~~