macro(s) for tn3270

I am very, VERY new to making apple scripts, and I’ve struggled to figure it out on my own, and tried tons of searches - and this is probably the most BASIC and SIMPLE stuff, but I can’t figure it out.

I want to run a script that will know where in my hard drive to find a document (it’s always in the same place), so following the path (how do I write the path in the script?) to the doc, then just opening it (yes, very basic, simple stuff), and then for it to do the following:

set firstText to text returned of (display dialog “Password” default answer “”)

tell application “tn3270 X” to activate

tell application “System Events”
keystroke “fi”
keystroke return
delay 2
keystroke "mcec "
keystroke firstText
keystroke return
delay 2
keystroke return
keystroke “*DEFINE”
keystroke return
end tell

I got it to do THIS much all by myself, basically just to log me into the system. IDEALLY I’d like to just launch the Applescipt and only type in my password there, and it would open the correct settings file (or preference doc or whatever it’s called), then log me in for me, and then take me to *DEFINE, where I normally go. One problem with the above code is that I have to first have the correct file already open. BUT, I want the script to open it for me instead of me opening it myself. In old Excel 4.0 macro vernacular I would use something like the following to pull up the directory: =DIRECTORY(“Hard_Drive:Applications:TN3270-OCSM-X:tn3270 3.2.4:ITS tn3270 X”) then I would just tell it to open (with the right application of course). I don’t know how to do that simple task in Applescipt (with OS X 10.4.11 if that matters).
I’m using Script Editor 2.1.1

So I just want the script to open the right preference file and then to log me in with the password the script asked me for.

THEN, once logged in, I want it to do a bit more. I sometimes have tons of screens to scroll through, and I’d like the script to take care of some of the redundancy. I understand some of the very basic “keystroke” commands that tell the script to type “this” word or “that” word to get to the right screens in the mainframe, so I can already write some of the code. BUT, what I want to do is have the script “hit the enter button” for me (I know that’s ‘keystroke return’) and then, and more importantly, “grab the next screen that displays” (don’t know how to do that) and keep doing this until some key word pops up on the “last” screen that lets it know it can stop grabbing screens. And somehow, this would magically produce a file filled with these “grabbed” screens. So it would have to be some sort of loop that it would somehow know how to get out of by reading what’s on each screen (so not just blindly capturing each screen). Normally, I might have it look for one word that would not appear anywhere on any of the screens except for the last screen, such as the word ‘end’. It could find the word ‘end’ in a sentence like ‘end of transactions’ or ‘End of list, please type in your next command.’ But I don’t know how that could be done. Ideally, it would just look everywhere and if it saw the word ‘end’ in any statement, anywhere on the entire screen, it would know to stop and get out of the “screen grabbing” loop.

I don’t know how to do this, or even just simply how to “grab” a screen, even just one screen. Would it capture the screen one single character at a time, reading in each line or row one at a time, or would it be some sort of copy and paste of the entire screen into some new doc, and still be able to tell when to stop? How would it create a new doc or file? It doesn’t really matter what the doc is called or named as long as I can find it, because I would then simply have an Excel macro open the newly-created file and do more stuff. The Excel stuff, I can do, as long as the applescript makes sure not to overwrite any previous doc it has created and always makes a doc with a new name. I would also like the option to have the script create a doc filled with 'grabbed screens" and know when to stop and then have another script be able to find and open that same file later and then ADD even more screens to the end of that same file or doc.

So, to my thinking, it might be just terribly easy and simple: One script would log me in. Then I would manually type in a particular command or go to a specific screen in the mainframe and simply run the ‘screen-grabber’ script.

And since my simple script above already works, it makes me think this whole thing is actually possible.

Thanks for any info or advice,
Henry

Model: PowerPC G4
AppleScript: 2.1.1
Browser: Firefox 3.0.10
Operating System: Mac OS X (10.4)

The first part of your problem is easy to fix. To open any file, just tell the Finder what file to open. You open a file on the desktop like this:

tell application "Finder"
	open "Macintosh HD:Users:hcantu99:desktop:somefile.txt"
end tell

Unfortunately, the second part of your problem isn’t easy, maybe not possible. The tn3270 application isn’t scriptable, so you can’t ask it (in Applescript) for the incoming text. I downloaded the app, but don’t have a server to log in to, so I can’t see much. It has an Edit menu, if you use “Select All” (cmd-A) and copy, does it give you the text?

Not sure if there’s any way around this one.

Okay, I’ve got the logging in part perfectly done! So far, so good. I did this:

set firstText to text returned of (display dialog "Password" default answer "" with hidden answer)

tell application "Finder"
	open "Macintosh HD:Applications:TN3270-OCSM-X:tn3270 3.2.4:ITS tn3270 X"
end tell

delay 2

tell application "tn3270 X" to activate

delay 2

tell application "System Events"
	keystroke "fi"
	keystroke return
	delay 2
	keystroke "mcec	"
	keystroke firstText
	keystroke return
	delay 2
	keystroke return
	keystroke "*DEFINE"
	keystroke return
end tell

Now, I just don’t know what to do to capture the screens. There is no “Select All” in the Edit menu (pretty sad). So I’ll probably have to do that bit manually. I’ll just select the entire screen by dragging the mouse over the area to select it.

But how do I tell the Finder or System Events (which one?) to “copy”? There is a command-C or apple-C. Would it be something like this?

tell appication "System Events"
       keystroke cmd-C
end tell

Because if I can at least get it to do that simple thing, to type command C to copy - then maybe I can get it to switch to another app, paste the screen there, and return to tn3270 - and maybe it could do this one last thing after pasting somewhere else and returning to tn3270

tell appication "System Events"
       keystroke return
end tell

just to have it hit ‘enter’ to give me another screen I can manually hi-light. Then I’d just re-run the macro dozens of times. Hi-Light, run macro, hi-light, run macro, etc. It’s a bit awkward, but it would be some progress, and it would at least save a few steps.

But thank you greatly for your advice so far! It’s working!

If you have to highlight the screen portions you need manually, is the macro to just hit “return” worth the time? If you have to press some keys to make the macro run, isn’t that the same as hitting “return?”

I’d recommend a clipboard utility. They would let you stack up each copy and then you could switch to your other application and paste them all at once. I use CuteClips, another one is iClip. Either one is good, and I think there are more.

I’ll try them. Thanks for the info!

Henry