Xcode 3.1, Perforce and Scripts


Posted by Jaime | Posted in Coding | Posted on 09-03-2009

Tags: , ,

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:

# 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
# Check to make sure it exists
if [ -f "$FULL_FILE_PATH" ]
	# Separate the filename and the 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"
end tell
tell application "Xcode"
end tell
	echo File not found:"$SRC_FILE"

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!

Xcode shell scripting

Xcode 3.1 and Perforce


Posted by Jaime | Posted in Coding | Posted on 07-03-2009

Tags: , ,

I use Perforce and I love it! Over the years I’ve used Visual SourceSafe, CVS, SVN and I experimented with other tools, but none of them have come close to the usefulness of Perforce.

I also use Xcode for my Mac coding but when you use Xcode with Perforce, you get some wierd behaviour. Xcode get’s hung, for no reason whatsoever and although Apple seems to know this is a problem, it doesn’t seem like they are fixing this problem any time soon.

Lucky for me you can use AppleScript from within Xcode! I usually work on one project at a time, so I can create an AppleScript that checks out the frontmost project file for me and another AppleScript to check out the currently selected source code file.

When you open Xcode you’ll notice an AppleScript icon in the menubar between Window and Help, but this is not an AppleScript drop down. This is just for the General Scripts that can be invoked from Xcode. So, if you want to run a shell script from Xcode, you can use this scripts menu for doing that, but for me, I wanted to leverage AppleScript for what I want to do.

You think this is for AppleScript, but it's not.

So, to make the AppleScript menu available, you have to open the AppleScript Utility in “/Applications/AppleScript/”. Open that program and click the “Show Script menu in menu bar” checkbox.

Use this to make the Script menu available

Once you do that, you should see an AppleScript menu item by the other icons on the left side of your menubar.

Now, open the “Script Editor” program within the “/Applications/AppleScript” folder. Copy the code below, which attempts to check out the frontmost project file from Perforce:

-- Check out the frontmost project
tell application "Xcode"
set myProj to (active project document)
set myCmd to full path of myProj
end tell
set myCmd to "/usr/local/bin/p4 -cjrios_My_Perforce_ClientName -pperforce.jaimerios.com:1666 -PHah -ujaimer edit " & myCmd & "/..."
do shell script myCmd

Basically, the script gets the frontmost project file, sets a variable to the full path to the project file and then I create a string with the command that I would normally invoke in Terminal.app. The changes you need to make to the command line are:

  • The -c option which tells p4 which client you are working in
  • The -p option which is the name, or ip, with TCP port to the perforce server
  • The -P option which is your password to the server and
  • The -u option which is your username

Notice that I don’t have a space between the switch and the value and that I have the full path to p4 spelled out.

Paste the code into a new script in “Script Editor” and save the script, with the name of your choice, into the “/Users/jaimerios/Library/Scripts/Applications/Xcode” folder. Once you go back to Xcode, you will see your script in the menu item.

Now the following script is almost the same thing, except for it checks out the current file that is selected in the project. Repeat the same steps for creating the project check out script for the script code below:

-- Check out the currently selected file
tell application "Xcode"
set myFile to associated file name of front window
set textDocuments to text documents
repeat with i in textDocuments
if path of i is myFile then
set myCmd to path of i
set myCmd to "/usr/local/bin/p4 -cjrios_My_Perforce_ClientName -pperforce.jaimerios.com:1666 -PHah -ujaimer edit " & myCmd
do shell script myCmd
end if
end repeat
end tell

I hope these scripts help you out. If you have any suggestions for making these scripts better, post a comment and let me know.

Happy coding!