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.
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.
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!
I am using something similar, but going directly to bash:
#!/bin/bash
# This script depends on a file named p4config being in the “root” directory
# For example, /Code/Flash/main/p4config has the following content:
# P4ROOT=/Code/Flash/main
# P4PORT=my.server.name:1666
# P4CLIENT=myclient
# P4USER=me
# P4PASSWD=mypassword
# Here is what the Perforce docs say about p4config:
#Contains a file name without a path. The file(s) it points to are used to store other Perforce environment or registry variables. The current working directory (returned by PWD) and its parents are searched for the file. If the file exists, then the variable settings within the file are used.
export P4CONFIG=”p4config”
# The next two lines get a path that resolves any ../ elements
# because p4 cannot handle them.
FILE=%%%{PBXFilePath}%%%
ABS_FILE=`pwd`/${FILE##*/}
/usr/local/bin/p4 edit $ABS_FILE
# If success, swap to Finder and back so Xcode notices the file is unlocked
if [ $? = 0 ]; then
open -a Finder
open -a Xcode $ABS_FILE
fi