Saturday, November 18, 2017

#1 2015-05-14 12:27:59 pm

Craig Smith
Administrator
From:: Tacoma, WA
Registered: 2005-05-16
Posts: 1027
Website

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:

Applescript:

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?


Craig Smith

Offline

 

#2 2015-05-14 12:54:58 pm

StefanK
Member
From:: St. Gallen, Switzerland
Registered: 2006-10-21
Posts: 11482
Website

Re: Run JavaScript Action

Hi,

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

Applescript:


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

Last edited by StefanK (2015-05-14 12:58:17 pm)


regards

Stefan

Offline

 

#3 2015-05-14 01:18:05 pm

Craig Smith
Administrator
From:: Tacoma, WA
Registered: 2005-05-16
Posts: 1027
Website

Re: Run JavaScript Action

Perfect!  Thank you very much, Stefan.


Craig Smith

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)