In my previous post, I showed how you can leverage AppleScript to check out a file or a project that resides on a Perforce server. The reason for this is that Xcode 3.x does not work well with Perforce.

Now, I figured out how to do this with the build in scripts menu in Xcode. The limitations I found so far is that you can only check out a file, can’t do a project yet, and that the cursor has to be in the file that you want to check out.

So I created a script, named it “Check out file” and put it in a section named “Gravy” since that is what I work with most. The script code is:

#!/bin/sh
# This shell script checks out the current file from Perforce, so long as it has the cursor in it

# Get the file's full path
FULL_FILE_PATH="%%%{PBXFilePath}%%%"

# Check to make sure it exists
if [ -f "$FULL_FILE_PATH" ]
then
	# Separate the filename and the path
	SRC_FILE=${FULL_FILE_PATH##*/}
	FILE_PATH=${FULL_FILE_PATH%/*}
	
	# Go to the folder and invoke the p4 command line app with the appropriate arguments
	cd "$FILE_PATH"
	/usr/local/bin/p4 -cjrios_My_Perforce_ClientName -pperforce.jaimerios.com:1666 -PHah -ujaimer edit "$SRC_FILE"

# This is a hack to get Xcode to recognize that the file was checked out
# Notice that the code is not indented
osascript - "$1" << ENDOFSCRIPT
tell application "Xcode"
	set myFile to associated file name of front window
	save myFile
end tell
tell application "Finder"
	activate
end tell
tell application "Xcode"
	activate
end tell
ENDOFSCRIPT
	
else
	echo File not found:"$SRC_FILE"
fi

For the output and error options, I set them to "Display in Alert".

And that's it. I hope this helps you and if you have any comments for improving this, post a comment and let me know.

Happy coding!

Reference:
Xcode shell scripting

One Comment

  1. Looks like the above Apple documentation is gone. If anyone has helpful links, please post here in the comments.

Leave a Reply

Your email address will not be published. Required fields are marked *