Applescript SVN Automatization scripts(completed,suggest optimisation)

For some time i have been creating some applescipts scripts to make it simple to work on multiple SVN projects. There are two scripts the first adds the data on the server and exports to the working folder. The other loops through all the projects in the working folder and commits those with changed commit message. This makes it easy to work on several projects and use one action to commit everything.

These scripts use the local setting on SVN, and in each of them the directory is inserted for local folder and external folder on SVN server.

The first script is to create a svn project on the server:

-- 1. Find the folder of the project
tell application "Finder"
	activate
	set svnFolder to folder (choose folder with prompt "Find the folder containing the project you want to add to svn.")
	set als to svnFolder as alias
	set svnFolderPOSIXPath to quoted form of POSIX path of als
end tell

tell application "Terminal"
	activate
	
	set window_1 to id of first window whose frontmost is true
	set moveToFolder to "cd " & svnFolderPOSIXPath
	do script moveToFolder in tab 1 of window id window_1 of application "Terminal"
	
	set a to 0
	repeat until (a = 1)
		if (window 1 is not busy) then
			set a to 1
		end if
	end repeat
	
	-- ASK FOR USER INFORMATION ABOUT NEW PROJECT
	set svnDirectoryLocal to "/Users/SVNFOLDER/"
	set svnDirectoryServer to "http://svn.xdomain.com/"
	set displayString to "Enter Project Project dir(no spaces)"
	
	set defaultAnswer to 0
	
	repeat
		set response to display dialog displayString default answer defaultAnswer
		try
			set responceString to (text returned of response) as string
			exit repeat
		on error errstr
			set displayString to errstr & return & "Please try again."
			set defaultAnswer to text returned of response
		end try
	end repeat
	
	-- 2. EXPORT ON SERVER
	
	set ts to "svn import "
	set te to " -m 'new project ("
	set teend to ")'"
	set createONServer to ts & svnDirectoryServer & responceString & te & responceString & teend
	do script createONServer in tab 1 of window id window_1 of application "Terminal"
	
	set a to 0
	repeat until (a = 1)
		if (window 1 is not busy) then
			set a to 1
		end if
	end repeat
	
	-- 3. CHECKOUT
	set commandCD to "cd "
	set commandCDComplete to commandCD & svnDirectoryLocal
	do script commandCDComplete in tab 1 of window id window_1 of application "Terminal"
	
	set a to 0
	repeat until (a = 1)
		if (window 1 is not busy) then
			set a to 1
		end if
	end repeat
	
	set ts to "svn checkout "
	set checkoutOnServer to ts & svnDirectoryServer & responceString
	do script checkoutOnServer in tab 1 of window id window_1 of application "Terminal"
	
	
end tell

The other script lets you specify commit messages for all projects in the current active folder, and only those changed (commit message) will get committed:

set svnDirectoryLocal to "/Users/SVNFOLDER/"
set svnDirectoryServer to "http://svn.xdomain.com/"

set stringCommitMessage to "-CommitMessage"
set ASCIICharacter to (ASCII character 13) -- change this dependent on keyboard (can be 10)

-- 1. FIND ALL THE FOLDERS THAT CAN BE COMMITTED
tell application "Finder"
	activate
	set svnFileList to name of every folder of (svnDirectoryLocal as POSIX file as alias)
end tell

-- 2. ASK FOR COMMIT MESSAGES (ONLY COMMIT IF CHANGED)

-- 2.1 Find the folders and display them
set svnItemString to ""
set numCount to 0
set numItems to count svnFileList
repeat with svnItem in svnFileList
	set numCount to numCount + 1
	
	set svnItemString to svnItemString & svnItem & stringCommitMessage
	if numCount is not equal to numItems then
		set svnItemString to svnItemString & ASCIICharacter
	end if
end repeat

set returnCommitMessages to text returned of (display dialog "Write in commit messages to the projects you would like to update. (Only changed projects will be committed)" default answer svnItemString)

-- 2.2 check what is changed, and create the set of svn folders to update
set AppleScript's text item delimiters to ASCIICharacter
set returnCommitSet to every text item of returnCommitMessages

-- 2.3 Loop through the changed list and see where changes have been made
set finalCommitMessages to {}
set finalDirectories to {}
set numCount to 0
repeat with itemFolderCommitInitMessage in svnFileList
	set numCount to numCount + 1
	set itemCommit to item numCount of returnCommitSet
	set itemFolderCommitInitMessageWithString to itemFolderCommitInitMessage & stringCommitMessage
	
	if (itemCommit as string) is not equal to (itemFolderCommitInitMessageWithString as string) then
		set end of finalCommitMessages to itemCommit
		set end of finalDirectories to itemFolderCommitInitMessage
	end if
end repeat


-- 3. INFORMATION
set numItems to count finalCommitMessages
if numItems = 0 then
	display dialog "No commit messages written, nothing will get updated." buttons {"RETURN"}
else
	set infoString to "UPDATING CURRENT SVN DIRECTORIES:" & ASCIICharacter & ASCIICharacter
	set numCount to 0
	set numItems to count finalCommitMessages
	repeat with finalCommitMessageItem in finalCommitMessages
		set numCount to numCount + 1
		set infoString to infoString & item numCount in finalDirectories & ASCIICharacter & "- Commit message: " & finalCommitMessageItem
		if numCount is not equal to numItems then
			set infoString to infoString & ASCIICharacter & ASCIICharacter
		end if
	end repeat
	display dialog infoString buttons {"CANCEL", "COMMIT"}
end if

-- 4. COMMIT ALL THE CHANGED FOLDERS
tell application "Terminal"
	activate
	set window_id to id of first window whose frontmost is true
	
	-- 4.1 Loop through all the folders to commit
	set numCount to 0
	set numItems to count finalCommitMessages
	repeat with finalCommitMessageItem in finalCommitMessages
		set numCount to numCount + 1
		
		set a to 0
		repeat until (a = 1)
			if (window 1 is not busy) then
				set a to 1
			end if
		end repeat
		-- 4.2 Goto the correct folder
		set cdAction to "cd " & svnDirectoryLocal & item numCount in finalDirectories
		do script cdAction in tab 1 of window id window_id of application "Terminal"
		
		set a to 0
		repeat until (a = 1)
			if (window 1 is not busy) then
				set a to 1
			end if
		end repeat
		
		-- 4.3 Find the files to add
		do script "svn stat | grep '^?' | awk '{print $2}' | xargs svn add" in tab 1 of window id window_id of application "Terminal"
		
		set a to 0
		repeat until (a = 1)
			if (window 1 is not busy) then
				set a to 1
			end if
		end repeat
		
		do script "svn status | grep '^?' | awk '{print $2}' | xargs -I {} svn add {}@" in tab 1 of window id window_id of application "Terminal"
		
		set a to 0
		repeat until (a = 1)
			if (window 1 is not busy) then
				set a to 1
			end if
		end repeat
		
		-- 4.4 Commit action
		set ts to "svn commit -m '"
		set te to "'"
		set commitAction to ts & finalCommitMessageItem & te
		
		do script commitAction in tab 1 of window id window_id of application "Terminal"
	end repeat
end tell


Possible things that i would like to get suggestions on:

  1. Focus problems, how do i make the script work if terminal is not open?
  2. Is there a better way to check if terminal is working than “window 1 is busy”?
  3. Other places i should optimize?
tell application "Finder"
	activate
	set svnFolder to folder (choose folder with prompt "Find the folder containing the project you want to add to svn.")
	set als to svnFolder as alias
	set svnFolderPOSIXPath to quoted form of POSIX path of als
end tell

=

set svnFolderPOSIXPath to quoted form of (POSIX path of (choose folder with prompt "Find the folder containing the project you want to add to svn."))

Referring to Adayzdone’s post above; with a starting path to choose folder. ( so you don’t have to navigate to /Users/SVNFolder/ all the time.)

I couldn’t see you using any svn client interactively in your script, so why not just drop the Terminal and use do shell scripts?

Well :slight_smile:

I think it started from simple custom triggers through quicksilver and i just though applescript was the way to do it.

Hello!

I meant using do shell script commands from within AppleScript! :slight_smile:

i just though applescript was the way to do it.

What McUsr is suggesting is that you issue the do shell script command separated by semicolons from within your applescript As long as there is no user interaction with the shell, I think he is right.

set xxx to do shell script "echo command1 ; echo command2"

EDIT
McUsr beat me to it.

He did, this time. :slight_smile:

What about the way i “wait” for terminal to finish? is this a good solution?
or would it not be a problem if i did it through the shell?

Now!

If you were in the middle of writing a comment to a commit with your svn client, then it would be a good thing.

But the terminal is a process among other processes, and what you do with a do shell script, is totally independent of that. Applescript forks a shell, it uses to execute the commands you specify in it.

Every thing will work smoothly, without considering the terminal, as long as you don’t manage to make a share violation among the files svn is about to process. Even if you are in the middle of committing something, i think it still will work.

Quoted form of Posix Path is by the way a commonly used command to escape spaces, and hyphens and what not from the command you pass to it.

Is there not a problem just running the script through the shell? What about warnings and all the info that svn posts to terminal - this can be vital if there is a problem.
(is there a way to read how it went back into applescript?) or tell in the end of the terminal script it failed?

Another thing is it possible to easily create GUI elements so that there are multiple input fields instead of just one text field? from what i have looked at - you need a program to export it for you?

Hello!

Maybe you get most “bang for the buck” right now, by staying into the terminal.

But you can redirect the terminal output, also the stderr by a 2>&1 construct on the command line.

When a command returns an errocode not equal to 0, and error is raised by the do shell script.

This status code is one that you can mask by ending your command by an exit 0.

so you could use a line like this:


set output to ( do shell script "your command 2>&1 ; exit 0 " )

You could then open a new text document with text edit, and paste in the output from svn there.

As for multi field dialogs you’d use AsObjC-Runner, or pashua. ( I live well with single live dialogs. Hitting cmd-enter or tab, doesn’t make that big a difference! :slight_smile: )