changing character in Terminal

This is probably very easy, but since I’m a noob…
What I’ve done so far:
I get Applescript to open Terminal, then using System Events, it logs into another terminal and does some pretty basic stuff - just some simple unix commands.
One of these is vi .hi - which tells the vi editor to open the document .hi and then I replace a single character in that doc.
The lines in Applescript are basically :
keystroke “vi .hi”
keystroke return
delay 1
keystroke “xak” --where ‘x’ deletes the existing character (because now I’m in the vi editor), ‘a’ then puts me in edit mode, and finally the character “k” is actually typed into the doc.

And that works just fine - only next time, I want the “k” to be an “l”, then the next time an “m” and on and on. So, what I do now is to just go in and edit my little Applescript and manually change the line: keystroke “xak” to keystroke “xal”. It works, but not very elegant.

What I want to do:
Have Applescript read the existing character (there’s only the one character in the whole doc anyway).
Then have Applescript know which character to replace it with and then go and replace it. So, if it finds a “k” there, it’ll replace it with an “l”. In other words, “a” is replaced with a “b”, “y” is replaced with a “z”, and “z” is replaced with an “a”, etc., etc.

Sorry if this is a dumb question - but it sounds like it should be easy to do.

I just don’t know how to get Applescript to read and recognize a character inside Terminal. Do I have to get out of “System Events” briefly for that part?

Thanks for reading this question and for any ideas!

Model: PowerPC G4
AppleScript: 1.9.3
Browser: Firefox 2.0.0.14
Operating System: Mac OS X (10.3.9)

How about a solution that doesn’t use the terminal at all, but rather leverages AppleScript’s ability to read & write to files.

set filePath to (path to desktop as Unicode text) & ".hi"

set asciiCode to ASCII number of (read file filePath)
try
	close access file filePath
end try
set fileRef to (open for access file filePath with write permission)
set eof fileRef to 0
if asciiCode is 122 then
	set asciiCode to 97
else
	set asciiCode to asciiCode + 1
end if
write (ASCII character of asciiCode) to fileRef
close access fileRef

Yes! I’ll be able to use that! I had to make another doc on my desktop named hi.txt instead of .hi The script changes the character there (perfectly!) then after all that, I have a line like this:
set txtvar1 to (ASCII character of asciiCode)

then further down:
tell application “Terminal” to activate
tell application “System Events”
keystroke “vi .hi”
keystroke “xa”
keystroke txtvar1

Just like magic! Thank you greatly! :slight_smile:

I didn’t really understand the filePath stuff or how to make a filePath inside Terminal. I don’t use Terminal to access my Mac, only to connect to other servers (I use telnet or ssh). But it’s working!

ya lost me LOL

So are you using this along side vi still? Or… what? =)

Well, Im not a unix expert either, so Im extra special lost. LOL

I use your code to get the new character - thats the hard part - and then I add that code to my previous code which just telnets to a unix server (using Terminal) and uses vi editor in that unix environment - only now it “remembers” what character to put in the doc .hi

When I ran your code, it first gave an error message that said it could not find the doc .hi on the desktop - so I tried to create one there. But when I tried, it didnt like the “.” in the name, so I made a doc named “hi.txt” instead, and then changed your code to use “hi.txt”. It worked perfectly there, but now I had to get it to put the new shiny character in the actual .hi document in the unix world - a unix-type server thats not on my mac.

Your code made the new character and to “remember” it, I used the line:
set txtvar1 to (ASCII character of asciiCode)

I could then use txtvar1 later when changing the character in the actual document .hi.

at the prompt, in the Terminal, I type vi .hi> It pulls up my document, then I type an “x” to delete the existing character in the document named “.hi”, then I type an “a” to enable edit, then I just have to type the new character.

Applescript does this for me with these lines
keystroke “vi .hi” --to open the doc
keystroke return
keystroke “xa” --“x” to delete the existing character, then “a” to get into edit mode

Now, still in the document,Applescript types in the new character with this line
keystroke txtvar1

to exit the vi editor, Applescript uses
key code 53 --thats the “escape” key to get out of edit mode
keystroke “ZZ” --to actual exit the vi editor

That brings me back to the unix prompt.

But before I can keystroke “vi .hi”, I need to do this:
tell application “Terminal” to activate – so that I’m in the Terminal application
tell application “System Events” – so I can have Applescript type things for me like “cd, etc”

So altogether, that part of the code looks like this:

set filePath to (path to desktop as Unicode text) & “hi.txt”

set asciiCode to ASCII number of (read file filePath)
try
close access file filePath
end try
set fileRef to (open for access file filePath with write permission)
set eof fileRef to 0
if asciiCode is 122 then
set asciiCode to 97
else
set asciiCode to asciiCode + 1
end if
write (ASCII character of asciiCode) to fileRef
close access fileRef

set txtvar1 to (ASCII character of asciiCode)

tell application “Terminal” to activate
tell application “System Events”
keystroke “vi .hi”
keystroke return
keystroke “xa”
keystroke txtvar1
key code 53
keystroke “ZZ”

So Applescript uses my mac to create the new character, then goes into Terminal to place it in a file that’s on a server. I think the server directory is something like

/home/astro/bba/name

Im still learning! Thank you again!