Little help with attempt to write applescript function?


Hi All,

Trying to write a script modeled off r-exec to run dofiles in Stata, and am having trouble. I’m a reasonable programmer in Python, etc., but novice in javascript /, and can’t figure out what’s going wrong. The lib block is pasted below. When I execute, I get nothing – no errors, no success, nothing. Keybindings checker shows that I am executing the function I think I am, but nothing happens.

Any input appreciated!

{CompositeDisposable, Point, Range} = require 'atom'

module.exports =
      type: 'string'
      default: 'Stata 13.1'
      description: 'Which application to send code to'
      type: 'boolean'
      default: false
      description: 'Cursor advances to the next line after ' +
        'sending the current line when there is no selection'
      type: 'boolean'
      default: true
      description: 'After code is sent, bring focus to where it was sent'
      type: 'boolean'
      default: true
      description: 'Send notifications if there is an error sending code'

  subscriptions: null

  activate: (state) ->
    @subscriptions = new CompositeDisposable

    @subscriptions.add atom.commands.add 'atom-workspace',
      'stata-exec:run-dofile', => @runDofile()

  deactivate: ->

  _getEditorAndBuffer: ->
    editor = atom.workspace.getActiveTextEditor()
    buffer = editor.getBuffer()
    return [editor, buffer]

  runDofile: ->
    osascript = require 'node-osascript'
    command = []
    focusWindow = atom.config.get 'stata-exec.focusWindow'
    documentTitle = getPath()+getTitle()
    dofileCommand ='tell application "Stata 13.1" to open ' + documentTitle
    if focusWindow
      command.push 'tell application "Stata 13.1" to activate'
    command.push dofileCommand
    command = command.join('\n')

    osascript.execute command, {code: selection},
      (error, result, raw) ->
        if error
          console.error error
          console.error 'code: ', selection
          console.error 'Applescript: ', command

Import commands from textmate bundles

The full package if it’s helpful:


I’m not sure how getPath() and getTitle() are supposed to function. When I type them into the JavaScript console I an error saying they’re undefined.

Are you sure the package you’re copying from works?


@leedohm aha, thanks! That’s part of the code I modified. Looks like I mis-understood the Atom API docs – I thought those were stand alones, but looks like they’re methods?

A related question: is there a “best practice” for testing code, like a debug mode that will throw exceptions for that kind of thing? Or at least an analogue for a “print” statement in other languages so I can check on how things are executing? I can run javascript in a stand-alone, but when debugging calls to the Atom API I feel a little in the dark as to what’s going on.


nvm! Found the developer console. Will play with that!