Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!


Click here to return to the 'Use Terminal's vi as default text editor' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
Use Terminal's vi as default text editor
Authored by: lsequeir on Mar 13, '08 08:35:45AM
Regarding your problem that "Unfortunately, the one thing I haven't figured out is how to get it to open without an input file, so to start a new text file I have to go to the Terminal and type vi filename. Please let me know if this script can be improved to handle that case, or in any other ways. "

It is easy to have your VI application also open by double clicking. You just have to put in there a similar AppleScript piece of code with "on run", as well as "on open inputfile" and do the appropriate adjustments. You would end up with something the following (which seems to be working for me).

on run
ignoring application responses
tell application "Terminal"
activate
tell application "System Events" to tell process "Terminal" to keystroke "t" using command down
do script "vi " in selected tab of the front window
end tell
end ignoring
end run

on open inputfile
ignoring application responses
tell application "Terminal"
activate
tell application "System Events" to tell process "Terminal" to keystroke "t" using command down
do script "vi " & quoted form of POSIX path of inputfile in selected tab of the front window
end tell
end ignoring
end open

---
Luís

[ Reply to This | # ]

Use Terminal's vi as default text editor
Authored by: ob1cannoli on Mar 13, '08 11:51:24AM
or one better, add an input for a file name/path: on run display dialog "Enter file name:" default answer "filename" copy the result as list to {theFilename, theButton} ignoring application responses tell application "Terminal" activate tell application "System Events" to tell process "Terminal" to keystroke "t" using command down do script "vi " & quoted form of fileName in selected tab of the front window end tell end ignoring end run on open inputfile ignoring application responses tell application "Terminal" activate tell application "System Events" to tell process "Terminal" to keystroke "t" using command down do script "vi " & quoted form of POSIX path of inputfile in selected tab of the front window end tell end ignoring end open

[ Reply to This | # ]
Re: Use Terminal's vi as default text editor
Authored by: Uncle Asad on Mar 13, '08 07:25:50PM

When I'm writing an applet that duplicates a lot of code in its on run and on open handlers, I find it useful to prevent duplication and ease maintenance by making those handlers "stubs" that collect some information and then run the shared code in another method.

For example,

on run
	set inputfile to ""
	my launchVI(inputfile)
end run

on open inputfile
	my launchVI(inputfile)
end open

on launchVI(inputfile)
	ignoring application responses
		tell application "Terminal"
			activate
			tell application "System Events" to tell process ¬
				"Terminal" to keystroke "t" using command down
			if inputfile ≠ "" then
				do script "vi " & quoted form of POSIX path of inputfile ¬
					in selected tab of the front window
			else
				do script "vi " in selected tab of the front window
			end if
		end tell
	end ignoring
end launchVI

There's probably a way to eliminate the if/else, but it's late and I'm not thinking very hard right now ;)

[ Reply to This | # ]