Skip to content

Instantly share code, notes, and snippets.

@idoru
Created July 8, 2012 03:18
Show Gist options
  • Save idoru/3069155 to your computer and use it in GitHub Desktop.
Save idoru/3069155 to your computer and use it in GitHub Desktop.
XCode behavior script for quickly opening corresponding Cedar spec and vice-versa
#!/usr/bin/osascript
(*
What does this script do?
This script lets you quickly open the Cedar spec for the corresponding class header/implementation that is currently open in Xcode. If the spec is open, it opens the implementation instead.
How do I use it?
Save this script somewhere.
Start Xcode and go to Xcode > Behaviors > Edit Behaviors…
Add a new behavior that runs this script
Bind it to a shortcut of your choosing (I like Cmd+Ctrl+Shift+Down)
NOTE: If your shortcut binding contains the Option key, it will open the file in the assistant editor instead of the main editor.
*)
tell application "Xcode"
set workspaceProjects to projects of active workspace document
(* don't do anything if there aren't any projects in the current workspace *)
if (count of workspaceProjects) > 0 then
(* figure out the name of the current file from the current window title,
currently I don't know of a more reliable way to do this with Xcode 4.x *)
set currentFileName to name of text document 1 whose name ends with (word -1 of (get name of window 1))
if (length of currentFileName) > 0 then
try
set originalDelimiters to AppleScript's text item delimiters
set AppleScript's text item delimiters to "."
set filePrefixItems to text items of currentFileName as list
set AppleScript's text item delimiters to originalDelimiters
on error
set AppleScript's text item delimiters to originalDelimiters
end try
set filePrefix to ""
repeat with textItemNumber from 1 to ((count of filePrefixItems) - 1)
if textItemNumber > 1 then
set filePrefix to filePrefix & "."
end if
set filePrefix to filePrefix & item textItemNumber of filePrefixItems
end repeat
set prefixLen to count of filePrefix
set specSuffix to (characters (prefixLen - 3) thru (prefixLen) of filePrefix) as string
if (specSuffix = "Spec") then
set filenameToFind to (characters 1 thru (prefixLen - 4) of filePrefix) as string
else
set filenameToFind to filePrefix & "Spec"
end if
set objCppFilenameToFind to filenameToFind & ".mm"
set objCFilenameToFind to filenameToFind & ".m"
set projectDirs to ""
repeat with currentProject in workspaceProjects
set findDir to project directory of currentProject
set projectDirs to projectDirs & "\"" & findDir & "\" "
end repeat
set command to "find " & projectDirs & " -name " & objCFilenameToFind & " -or -name " & objCppFilenameToFind
set foundFile to do shell script command
if length of foundFile > 1 then open foundFile
end if
end if
end tell
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment