Kivak Wolf
Kivak Wolf

Reputation: 870

How to log to message window in Script Editor using JavaScript for Automation

"JavaScript for Automation" is brand new in OSX Yosemite. I am not familiar with AppleScript as I don't really like how verbose it is. But Javascript looks great! However, I can't figure out how to send debug messages to the message window of ScriptEditor! I've looked everywhere... Is this possible? What is the code to do it?

I tried:

console.log("Hello World");

But that doesn't work. Any ideas? This should be so simple!

Note: This isn't an HTML/Javascript question. I am talking about the App called "Script Editor" in OSX using the Javascript language instead of AppleScript. I would tag this question with "Script-editor" but SO won't allow me to tag.

Upvotes: 6

Views: 5522

Answers (3)

Pat Gilmour
Pat Gilmour

Reputation: 701

As of 10.11 you can view console messages from Script Editor in the Safari Web Inspector (yep!), which is very useful for debugging.

Enable this option in the Safari menus: Develop>[your-mac-name]>Automatically Show Web Inspector for JSContexts

(Note: You'll need to activate the Safari Develop menu in Preferences if you haven't done so)

Then in your Script Editor script include a debugger line, which will open the Web Inspector in Safari and pause your script execution. Note the script file must be saved to disk.

You can then see your console messages from Script Editor in the Safari Web Inspector Console.

For example, try running the following in Scripter and step through the script:

debugger
Mail = Application('Mail')
console.log("Mail App's ID is: " + Mail.id())

Note that you don't need "this."

Look in the Console tab of the Web Inspector and you should see: "Mail App's ID is: com.apple.mail"

You can read more about this on the Apple Developer Site

Upvotes: 3

Timmmm
Timmmm

Reputation: 96596

console.log() works but the output isn't in the default Result pane. Go to View->Show Log and then click the Messages tab.

The output is shown in grey text surrounded by /* */.

Upvotes: 5

Kivak Wolf
Kivak Wolf

Reputation: 870

OK Apparently it was easy enough.

this.console.log("Hello World");

I found this by simply typing this into a script and running it. It returns back GlobalObject for this with plenty of interesting properties and functions including the console property which contains a log() function.

Upvotes: 8

Related Questions