April 23, 2008

More Macintosh Office Integration

Surround SCM
It’s possible to use AppleScript and Microsoft Office for the Mac to provide improved integration between Surround SCM and Microsoft Word on the Macintosh. Below are some sample AppleScript snipets that show you how to do this. None of these have full error handling, but should server as an example of how you might leverage these two tools. For all of these samples, open up the AppleScript Script Editor application, past the script in and save the file in your Documents/Microsoft User Data/Word Script Menu Items/ folder.

Checkout

To use AppleScript to checkout the current document, try the following.
tell application "Microsoft Word"
	set fullPath to path of active document as string
	set oldDelimiters to AppleScript's text item delimiters -- always preserve original delimiters
	set AppleScript's text item delimiters to {":"}
	set pathItems to text items of (fullPath as text)
	set fileName to get last text item of pathItems
	set parentPath to ((reverse of the rest of reverse of pathItems) as string) & ":"
	set AppleScript's text item delimiters to oldDelimiters -- always restore original delimiters
	do shell script "cd '" & POSIX path of parentPath & "' ; sscm checkout '" & fileName & "' -c- "
	close active document
	open file name fullPath
end tell

Checkin

Try the following AppleScript for checking in the current document. This script prompts you for a checkin comment.
tell application "Microsoft Word"
	set fullPath to path of active document as string
	set oldDelimiters to AppleScript's text item delimiters -- always preserve original delimiters
	set AppleScript's text item delimiters to {":"}
	set pathItems to text items of (fullPath as text)
	set fileName to get last text item of pathItems
	set parentPath to ((reverse of the rest of reverse of pathItems) as string) & ":"
	set AppleScript's text item delimiters to oldDelimiters -- always restore original delimiters
	display dialog "Enter checkin comments:" default answer ""
	set dialogInfo to result
	set checkinComments to text returned of dialogInfo
	if length of checkinComments > 0 then
		set comments to "' -cc'" & checkinComments & "'"
	else
		set comments to "' -c-"
	end if
	do shell script "cd '" & POSIX path of parentPath & "' ; sscm checkin '" & fileName & comments
	close active document
	open file name fullPath
end tell

Undo Checkout

Just to round things out, here is a script to undo a check out.
tell application "Microsoft Word"
	set fullPath to path of active document as string
	set oldDelimiters to AppleScript's text item delimiters -- always preserve original delimiters
	set AppleScript's text item delimiters to {":"}
	set pathItems to text items of (fullPath as text)
	set fileName to get last text item of pathItems
	set parentPath to ((reverse of the rest of reverse of pathItems) as string) & ":"
	set AppleScript's text item delimiters to oldDelimiters -- always restore original delimiters
	close active document
	do shell script "cd '" & POSIX path of parentPath & "' ; sscm uncheckout '" & fileName & "' "
	open file name fullPath
end tell