[SE] Set Language of Script Editor Document using JXA

###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() ~~~~

I think I’ve posted this before, but here’s the AppleScript version:

tell application "Script Editor"
	activate
	set _javaScript to first language whose name is "JavaScript"
	
	set _window to window 1
	if name of _window is not "Untitled" then
		return "Can't find Untitled window"
	end if
	set _doc to document of _window
	set language of _doc to _javaScript
end tell

And what’s wrong with the world? @JMichaelTX is posting JXA, and I’m posting AppleScript?!? WTH? :wink:

Thanks for posting your script, Dan.

I was going to add it later. Your script served as an inspiration and guide for me as I tested and explored it using Script Debugger 6 (a terrific tool).

1 Like

Hey Dan,

FWIW you can write that more efficiently:

tell application "Script Editor"
   set _javascript to first language whose name is "JavaScript"
   tell front document
      if its language's name is not "JavaScript" then set its language to _javascript
   end tell
end tell

-Chris

3 Likes