Run JavaScript Action

I am totally new to JavaScript. I am trying to use a Run JavaScript Action in Automator to produce a formatted date string:

ObjC.import('Cocoa');
rightNow = $.NSDate.date;
dtFormatter = $.NSDateFormatter.alloc.init;
dtFormatter.dateStyle = $.NSDateFormatterFullStyle;
dtFormatter.timeStyle = $.NSDateFormatterMediumStyle;

formattedDate = dtFormatter.stringFromDate(rightNow);

// --> $("Thursday, 14 May 2015 at 10:26:51 AM")

The console in Script Editor displays the formatted date string variable correctly, but I cannot get it to pass from the Automator Action to the next Action. I just want the string. I have tried log, console.log, this.console.log, write, and a few other commands that I have found out in cyberspace, all to no avail.

Any ideas?

Hi,

formattedDate contains a Objective-C object (NSString), so you have to unwrap it to get a “primitive”


function run(input, parameters) {
	
	ObjC.import('Cocoa')
	rightNow = $.NSDate.date
	dtFormatter = $.NSDateFormatter.alloc.init
	dtFormatter.dateStyle = $.NSDateFormatterFullStyle
	dtFormatter.timeStyle = $.NSDateFormatterMediumStyle

	formattedDate = dtFormatter.stringFromDate(rightNow)

	return ObjC.unwrap(formattedDate)
}

Perfect! Thank you very much, Stefan.