 
     null
 
-  open: (pathsToOpen) ->
-    if pathsToOpen?
-      for pathToOpen in pathsToOpen
-        pathToOpen = path.resolve(@executedFrom, pathToOpen) if @executedFrom and pathToOpen
-        if existingWindow = @windowForPath(pathToOpen)
-          existingWindow.focus()
-          existingWindow.sendCommand('window:open-path', pathToOpen)
-        else
-          atomWindow = new AtomWindow
-            pathToOpen: pathToOpen
-            bootstrapScript: 'window-bootstrap'
-            resourcePath: @resourcePath
+  openPaths: (pathsToOpen=[], pidToKillWhenClosed) ->
+    @openPath(pathToOpen, pidToKillWhenClosed) for pathToOpen in pathsToOpen
+
+  openPath: (pathToOpen, pidToKillWhenClosed) ->
+    if openedWindow = @windowForPath(pathToOpen)
+      openedWindow.focus()
+      openedWindow.sendCommand('window:open-path', pathToOpen)
     else
-      atomWindow = new AtomWindow
-        bootstrapScript: 'window-bootstrap'
-        resourcePath: @resourcePath
+      bootstrapScript = 'window-bootstrap'
+      openedWindow = new AtomWindow({pathToOpen, bootstrapScript, @resourcePath})
+
+    if pidToKillWhenClosed?
+      @pidsToOpenWindows[pidToKillWhenClosed] = openedWindow
+
+    openedWindow.browserWindow.on 'closed', =>
+      for pid, trackedWindow of @pidsToOpenWindows when trackedWindow is openedWindow
+        process.kill(pid)
+        delete @pidsToOpenWindows[pid]
 
   openConfig: ->
     if @configWindow


    ipc.on 'open', (processId, routingId, pathsToOpen) =>
      if pathsToOpen?.length > 0
        @open(pathsToOpen)
      else
        pathsToOpen = dialog.showOpenDialog title: 'Open', properties: ['openFile', 'openDirectory', 'multiSelections', 'createDirectory']
        @open(pathsToOpen) if pathsToOpen?

    ipc.on 'new-window', =>
      @open()

  version: null
  socketPath: '/tmp/atom.sock'

  constructor: ({@resourcePath, @executedFrom, @pathsToOpen, @testMode, @version, wait, pid}) ->
    @pidsToOpenWindows = {}
    if @pathsToOpen?
      @pathsToOpen = @pathsToOpen.map (pathToOpen) =>
        path.resolve(@executedFrom, pathToOpen)
    else if @executedFrom
      @pathsToOpen = [@executedFrom]
    else
      @pathsToOpen = [null]

    @executedFrom ?= process.cwd()
    pidToKillWhenClosed = pid if wait
    atomApplication = this
    @windows = []

    @sendArgumentsToExistingProcess pidToKillWhenClosed, (success) =>
      process.exit(0) if success # An Atom already exists, kill this process
      @listenForArgumentsFromNewProcess()
      @setupNodePath()
