Panic! Coda Script for inserting CSS - Just looking for comments...

and improvements.

I do a lot of web development and recently purchased Panic!'s Coda editor. It’s written in XCode and, to me, is a bit cleaner and easier to use than Dreamweaver. But since they are just starting their Gall’s Law curve so there are some things missing from the app which DW has and I found convenient. One of them is inserting a link to a external CSS file. I was kind of surprised the developers left this out since it is essentially something every web developer would use today.

Regardless, Coda is scriptable so I wrote a little script that will let me select the CSS file I want to insert and then rewrite the file I am working on with the relevant code. It couldn’t get the path to the front most document so I was backtracking from my css file to the next level up in my directories and getting it that way (this is a very specific to me script : D ) and it worked fine. Then Tim over at Panic! sent me the snippet for getting the path and now I have a problem with the script. Whenever I run it I have to exit Coda and come back to it to see the changes. I don’t think Update works outside of ASS, does it? Also, the path to the front most document is returned as a POSIX path but I can’t seem to change it to a POSIX file. Anyone have any ideas on either or these I’d appreciate it. Also feel free to comment on ways to improve the code.

Thanks

Steve



-->Get CSS file
set thisFile to choose file
set fileinfo to info for (thisFile)

-->make sure (kind of, there's no guarantee) we're dealing with a CSS file
set nameext to name extension of fileinfo
if nameext is not equal to "css" then
	display dialog "You must select a CSS file"
else
	-->parse path, we want to get past the site folder, this won't be a universal fix but just work in my own little world
	set OAD to AppleScript's text item delimiters
	set AppleScript's text item delimiters to ":"
	set localFilePath to ""
	repeat with x from 1 to count of every text item of (thisFile as string)
		set thispathitem to text item x of (thisFile as string)
		if (x > 5 and x < (count of every text item of (thisFile as string))) then
			set localFilePath to localFilePath & thispathitem & ":"
		else if (x = (count of every text item of (thisFile as string))) then
			set localFilePath to localFilePath & thispathitem
		end if
	end repeat
	
	
	-->put it in front most document of coda
	tell application "Coda"
		activate
		try
			-->Thanks Tim over at Panic!
			set thisFilePath to file path of current editor of document 1 as string
		on error errmsg
			display dialog errmsg
		end try
	end tell
	
	-->ug, I can't convert this to POSIX file for some reason so I'm using this blunt instrument
	set AppleScript's text item delimiters to "/"
	set newFilePath to ""
	repeat with i from 1 to count of every text item of thisFilePath
		if i > 2 then
			if i < (count of every text item of thisFilePath) then
				set newFilePath to newFilePath & text item i of thisFilePath & ":"
			else
				set newFilePath to newFilePath & text item i of thisFilePath
			end if
		end if
	end repeat
	
	-->read file
	try
		set theContents to my readFile(newFilePath)
	on error errmsg
		display dialog errmsg
	end try
	
	-->rewrite file contents (this script only works if you set up for html, php, asp, whatever file with the html comment <!--CSS Link--> where you want the link to go. In Coda you can set up a "clip" that you insert into every new file to ensure this is done automatically. 

	set AppleScript's text item delimiters to "<!--CSS Link-->"
	set fileStart to text item 1 of theContents
	set fileEnd to text item 2 of theContents
	set CSSLink to "<!--CSS Link-->" & (ASCII character (10)) & (ASCII character 9) & (ASCII character 9) & "<link rel=\"stylesheet\" href=\"" & localFilePath & "\" type=\"text/css\" media=\"screen\" />"
	
	set newContent to fileStart & CSSLink & fileEnd
	
	tell application "Coda"
		close document 1 saving yes
	end tell
	
	my writeFile(newFilePath as alias, newContent)
	
	tell application "Coda"
		open alias newFilePath
	end tell
end if



on readFile(theFile)
	set f to open for access theFile as alias
	set fSize to (get eof f)
	set theContents to read f from 1 to fSize
	close access f
	return theContents
end readFile

on writeFile(theFilePath, theContent)
	set f to (open for access theFilePath with write permission)
	try
		write theContent to f
	on error errmsg
		display dialog errmsg
	end try
	close access f

end writeFile