0

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; }

1 Answers1

0

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

JakeCigar
  • 603
  • 6
  • 12
  • I should've been more clear. The issue is that when it is run in an automator workflow, the workflow doesn't recognize the .length. The code works in script editor, but in automator it returns nothing. –  May 14 '16 at 20:33
  • I tried everything you did, but I think my error must be coming from elsewhere because the text that is selected is not replaced. You are correct about the variable issue. It outputs 5 now, but it doesn't replace text. [AutomatorPic](http://i.imgur.com/eiSrtYx.png) –  May 14 '16 at 23:35
  • The javaScript that you posted ignores input and gets the length of a var called ina which is 5 chars long. What text are you expecting to replace? – JakeCigar May 15 '16 at 16:26
  • I was just continuing on with our example in an attempt to see if the text replace feature would work. The original program takes in text, manipulates it, and spits out an answer. However, at the most basic level (the code above), it can't even replace the text with the number 5. Sometimes the code works with other replacements, but why isn't it working here? –  May 15 '16 at 20:56
  • I’ve only seen the javascript that returns the length of a constant string. And it works. Maybe try another post with the rest of the automator script. It doesn’t seem to have anything to do with JXA. – JakeCigar May 15 '16 at 23:21