Jump to content

Return Javascript Output

Recommended Posts

How would I return the output from javascript in a "Run Script" Action?


The following code returns nothing. I tried in OS X Script Editor and it let me know that the "return" command can only be used inside a function. Is there something I'm missing?


My code is below


var query = "{query}";

var time = query.split(":");
var hours = time[0];
var minutes = time[1];
var seconds = time[2];
var secondsDecimal = (seconds / 60)
return secondsDecimal
Link to comment

What you're missing is a function ;)

function run(argv) {
    // your code goes here...

When you run a script in Script Editor or from the command line, it will call the run function if there is one. argv will be an array of any command-line arguments (if you run the script on the command line). 
You can just write your code at the top level, too, but obviously you can't use return. In that case, the script output is typically the result of the last expression. So in your case, it'll probably work if you just leave off the final return secondsDecimal statement.

Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Create New...