Skip to content

Instantly share code, notes, and snippets.

@wearhere
Created February 9, 2012 09:34

Revisions

  1. wearhere revised this gist Feb 9, 2012. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions keep_current_file_open.sh
    Original file line number Diff line number Diff line change
    @@ -7,8 +7,8 @@
    # By setting Xcode (in Behaviors) to run this script when "Run Starts"
    # and when "Run Completes", you can prevent it from switching to main.m
    # when a run finishes.
    # See http://stackoverflow.com/questions/7682277/xcode-4-2-jumps-to-main-m-every-time-after-stopping-simulator for a description of the problem
    # and all the solutions which don't work.
    # See http://stackoverflow.com/questions/7682277/xcode-4-2-jumps-to-main-m-every-time-after-stopping-simulator
    # for a description of the problem and a bunch of solutions which don't work.

    # We use the defaults system to save the source path.
    # Change the domain to whatever you like.
  2. wearhere revised this gist Feb 9, 2012. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion keep_current_file_open.sh
    Original file line number Diff line number Diff line change
    @@ -12,7 +12,7 @@

    # We use the defaults system to save the source path.
    # Change the domain to whatever you like.
    DOMAIN="com.jeffwear.keep_current_file_open"
    DOMAIN="com.wearhere.keep_current_file_open"
    KEY="savedSourcePath"


  3. wearhere created this gist Feb 9, 2012.
    44 changes: 44 additions & 0 deletions keep_current_file_open.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,44 @@
    #! /bin/sh

    # On alternate invocations, this script
    # saves the path of the source file currently open in Xcode
    # and restores the file at that path in Xcode.
    #
    # By setting Xcode (in Behaviors) to run this script when "Run Starts"
    # and when "Run Completes", you can prevent it from switching to main.m
    # when a run finishes.
    # See http://stackoverflow.com/questions/7682277/xcode-4-2-jumps-to-main-m-every-time-after-stopping-simulator for a description of the problem
    # and all the solutions which don't work.

    # We use the defaults system to save the source path.
    # Change the domain to whatever you like.
    DOMAIN="com.jeffwear.keep_current_file_open"
    KEY="savedSourcePath"


    # The first time this script is invoked, this call will return a "does not exist" error.
    # The second time this script it invoked, this call will return the saved source path.
    # We copy stderr to stdout so that we can read both results using a single call.
    savedSourcePath=`defaults read $DOMAIN $KEY 2>&1`

    if [[ "$savedSourcePath" == *"does not exist"* ]]; then
    # read path of current source file out of Xcode
    sourcePath="`osascript << EOT
    tell application "Xcode"
    set windowName to (name of first window)
    set currentFileName to item 2 of (words of windowName)
    set currentSourceDocument to first item of (source documents whose name ends with currentFileName)
    set currentSourceFile to (file of currentSourceDocument)
    set currentSourcePath to (POSIX path of currentSourceFile)
    end tell
    return currentSourcePath
    EOT`"
    # save current source path
    defaults write $DOMAIN $KEY "$sourcePath"
    else
    # open saved source path in Xcode
    open "$savedSourcePath"
    # clear saved source path
    defaults delete $DOMAIN $KEY
    fi