3

I want switch from AppCode to Xcode and continue editing file on the same line.

Solution based on UI Automation seems working. Is the are any solution how to do the same without UI Automation; without need of showing Open Quickly dialog and typing FileName:LineNumber?

Thanks!


Solution based on UI Automation.
The code below will open File -> Open Quickly ... dialog, then will type into File or Symbol search field and finally will hit Enter:

//
// File: openFileInXcode.scpt
//
function run(argv) {
    locationToOpen = argv[0]
    if (locationToOpen == undefined) {
        doSysLog("Nothing to do. Exiting...")
        return
    }
    openFileinXcode(locationToOpen)
}

function openFileinXcode(location) {
    var appXcode = Application('Xcode')
    appXcode.activate()
    doSysLog("Opening file: " + location)
    delay(0.2)

    var appSysEvents = Application('System Events')
    appSysEvents.keystroke('o', { using: ['command down', 'shift down'] })
    appSysEvents.keystroke(location)
    appSysEvents.keyCode(36)
}

function doSysLog(message) {
    app = Application.currentApplication()
    app.includeStandardAdditions = true
    app.doShellScript('syslog -s -l W "WaveLabs Automation: ' + message + '"')
    console.log(message)
}

Usage from command line:

 osascript -l JavaScript "/path/to/openFileInXcode.scpt" "AppDelegate.swift:11"

Usage from AppCode:

  1. Go to File -> Preferences -> Tools -> External Tools
  2. Add new tool and configure parameters using macros (i.e. $FileName$:$LineNumber$)

Update 07.06.2016: Detail description of External Tool setup:

  • Program: /bin/bash
  • Parameters: -c "[[ -r ~/.bashrc ]] && . ~/.bashrc; osascript -l JavaScript \"$PATH_TO_YOUR_SCRIPTS_DIR/OpenFileInXcode.scpt\" \"$FileName$:$LineNumber$\""
  • Working directory: $ProjectFileDir$
Vlad
  • 6,402
  • 1
  • 60
  • 74
  • How do you configure the tool in AppCode? I'm getting an `syntax error near unexpected token `argv'` error when running the script from AppCode. – mattsson Jun 07 '16 at 11:43
  • I did a trick shown in original post under section "Update 07.06.2016". Idea behind that we launching bash with option `-c`. So, that bash execute commands from passed string ,) – Vlad Jun 07 '16 at 15:19
  • FYI, `xed -l $LineNumber$ $FilePath$` is a shorter method, but for some reason that does not go to the line right away, there's a short delay. – mattsson Jun 08 '16 at 12:19

0 Answers0