Little help with attempt to write applescript function?


#1

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 / atom.io, 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 =
  config:
    whichApp:
      type: 'string'
      default: 'Stata 13.1'
      description: 'Which application to send code to'
    advancePosition:
      type: 'boolean'
      default: false
      description: 'Cursor advances to the next line after ' +
        'sending the current line when there is no selection'
    focusWindow:
      type: 'boolean'
      default: true
      description: 'After code is sent, bring focus to where it was sent'
    notifications:
      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: ->
    @subscriptions.dispose()

  _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
#2

The full package if it’s helpful: https://github.com/nickeubank/atom-stata-exec


#3

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?


#4

@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.


#5

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