Search code examples
javascriptmacosjavascript-automation

JXA and .length


I am trying to turn an old javascript program of mine into a service on a mac using automator. When I use my Code, it doesn't work. I have checked what I can, and I have found that the most basic issue I encounter is that the .length property from javascript does not work. This simple program doesn't return what it should. It returns nothing:
function run(input) { var ina = 'hello'; var newn = ina.length; return newn; }


Solution

  • Your code returns 5.

    input is an array of 0 elements. Not of much use in script editor. When run from a command line it is the parameters.

    As a side note run is the run handler for all AppleScript (including JXA), so you would not call function run. It is called for you.

    UPDATE: In automator, The result (5) is sent to the next automator task. You can do a "Set Value of Variable" right after you do "Run JavaScript" with your script to capture the 5. enter image description here