Skip to content

Instantly share code, notes, and snippets.

@davedelong
Created March 8, 2011 00:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save davedelong/859575 to your computer and use it in GitHub Desktop.
Save davedelong/859575 to your computer and use it in GitHub Desktop.
Copies the derived data directory for the current Xcode4 project or workspace.
(************************************************************************************
Copies the derived data directory for the current Xcode4 project or workspace to the clipboard
The copied path is in POSIX form suitable for pasting into a Terminal instance.
Created 03/04/2011 by Ben Dolman
This code is in the public domain
************************************************************************************)
set derivedDataDir to (path to current user folder as string) & "Library:Developer:Xcode:DerivedData:"
tell application "Xcode"
-- path to the active workspace
set workspaceFile to file of active workspace document
end tell
set workspacePosixPath to POSIX path of (workspaceFile as text)
if (workspaceFile as text) ends with "project.xcworkspace:" then
-- workspace inside a project. product directory is named after the project.
set command to "echo " & workspacePosixPath & " | sed 's/.*\\/\\([^\\/]*\\)\\.xcodeproj.*/\\1/'"
set productName to do shell script command
else
-- standalone workspace. product directory is named after the workspace.
set command to "echo " & workspacePosixPath & " | sed 's/.*\\/\\([^\\/]*\\)\\.xcworkspace.*/\\1/'"
set productName to do shell script command
end if
-- verify that we parsed out the product name
if productName = workspacePosixPath then
display alert "Unable to parse out the product name"
return
end if
-- find the right folder in the derived data directory
tell application "Finder"
set allFolders to folders of folder derivedDataDir
end tell
set itemCount to (get count of items in allFolders)
set matchingItems to {}
repeat with i from 1 to itemCount
set theItem to item i of the allFolders
-- each folder is of the form "<product name>-<genid>"
-- find all of them that match the product name
if name of theItem starts with (productName & "-") then
copy POSIX path of (theItem as text) to the end of matchingItems
end if
end repeat
-- verify that we found at least one matching directory
if (count of matchingItems) = 0 then
display alert "No matching directory in DerivedData"
return
end if
set buildFolder to ""
-- if there is only one result we're done. otherwise we have to further
-- disambiguate by looking at the plist contained in each directory
if (count of matchingItems) = 1 then
set buildFolder to item 1 of matchingItems
else
set itemCount to (get count of items in matchingItems)
repeat with i from 1 to itemCount
set thePath to item i of matchingItems
set plistFile to thePath & "info"
try
-- each plist has a key/value pair that points to the workspace or project file that we can match on
set plistWorkspacePath to do shell script "defaults read " & plistFile & " WorkspacePath"
end try
-- use starts with because in the case of projects (vs. workspaces) the plist contains
-- the path to the project rather than the workspace inside the project
if workspacePosixPath starts with plistWorkspacePath then
set buildFolder to thePath
exit repeat
end if
end repeat
end if
if buildFolder is not equal to "" then
-- copy the result to the clipboard
set the clipboard to (buildFolder as string)
else
display alert "Unable to find a matching directory in DerivedData"
end if
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment