codemorphis.com Forum Index codemorphis.com
Software development: pure and simple.
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Programming Tip #15: The Application Interface Component

 
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    codemorphis.com Forum Index -> Visual Programming Forum
View previous topic :: View next topic  

Please rate on a scale of 1-5 (5 being max)
how helpful (clarity, ease of understanding, relevance) this article was for you.
You must be logged in to vote.
1
0%
 0%  [ 0 ]
2
0%
 0%  [ 0 ]
3
0%
 0%  [ 0 ]
4
0%
 0%  [ 0 ]
5
100%
 100%  [ 1 ]
Total Votes : 1

Author Message
CodeMorphis



Joined: 02 Dec 2003
Posts: 252

PostPosted: Sat Feb 12, 2005 1:45 am    Post subject: Programming Tip #15: The Application Interface Component Reply with quote

Programming Tip #15: The Application Interface Component

The Application Interface component is a new component introduced in Synopsis 1.1.7 that allows you to send keystroke and mouse commands to a Windows program. Since this provides for some interesting programming possibilities, we’ll take a closer look at this component in this article.

We’ll show two programming examples using the Application Interface component. In the first example, we’ll see how to launch a Windows program and send it keystroke commands. In the second example, we’ll see how to send mouse move and mouse click commands.

Our first program (which can be downloaded at http://www.codemorphis.com/articles/tip15/notepad_program.vpd) looks like this:



The first service of the Application Interface component that we use is the “Start app” service, which allows us to launch a Windows application. Here’s what the input data for the component looks like:



The “Path” argument is where we specify the path to the application that we want to invoke. Here we are using the Microsoft Notepad program. Since the notepad.exe program is located in the Windows operating system’s folder, we don’t have to specify the exact path to the application. In fact, if the path to your application is included in the Windows “Path” environment variable, then you can omit the full path. The “Window state” argument allows us to select how the application window should be displayed on startup. It can be started as maximized (occupying the entire screen) or minimized or in normal default state. We’ll use the normal default state.

The next component in the program uses the “Activate (PID)” service. Note that the “Start app” service outputs the process ID (PID) of the Windows application that gets invoked. This system process ID allows us to keep track of the applications that we run so that we can later send commands to them. In our program we have the PID feeding the “Activate (PID)” service call component. This service gives application focus to our application so that when we send commands we are sure that the correct application is receiving them. It is always a good idea to activate the target application before sending new commands as application focus can be changed. The user, for example, could click on another window, thereby changing the focus. There is an equivalent service called “Activate” which activates based on the title in the application window. The title is the text that appears in the control bar at the top of a window. This name based service is available since if we did not create the application that we want to control, it may be easier to find it using the text in the window’s title. Since we are creating the application that we want to control, it is better to use the PID version, as it is more deterministic.

After we have activated the application, we can send commands. We do this with the next service call which is the “Keys” service. We can simply add whatever text we want to have automatically written to the Notepad application in the input data port. In our program we have added the text: “This is some text added via the Application Interface component”.

The last component in the program uses the “Keys” service also but here we send a keystroke command to invoke the Notepad program’s Save function.



The data that we enter in the input data port is “%FSc:\myfile”. The “%F” is a special code that means “ALT F” which causes the application’s File menu to be opened. Then the “S” character that follows invokes the Save function in the File menu. Finally, the text “c:\myfile” is the text that will be entered automatically in the file path field of the Save menu.

If we run our program, this is what we get:


Note that the text specified in the first “Keys” call appears in the Notepad editor and that the Save dialog is open (via the “%FS” command sequence) and that our desired file path “c:\myfile” appears in the dialog’s “File name” field.

There are many other special control commands available for the “Keys” service. Refer to the Help module for a complete listing.

In our program we used two separate “Keys” service calls. We could have used only one by combining the text and commands as follows:

“This is some text added via the Application Interface component“%FSc:\myfile”

We used two separate calls to the “Keys” service only for clarity in our discussion but you can see how it is possible to string and mix input text with control keystrokes.

Our second program (which can be downloaded at http://www.codemorphis.com/articles/tip15/graphics_program.vpd) looks like this:



In this program, we will start the Synopsis demo program “Graphics.exe” located in the “Sample Programs\Executables” directory of the Synopsis installation. We will then send mouse commands to automatically drawn a textured square.

Once again, we use the “Start app” service to invoke the desired application. This time our path argument looks like this:



Here we have to specify a full path since the Synopsis “Sample Programs” directory is not in the operating system’s “Path” environment variable. But we use the $PROG_START_PATH variable to indicate that the path should begin with the directory where Synopsis (or the final program that we build) is located. This Synopsis programming variable is explained in detail in the Help module.

The next component is the same call to the “Activate (PID)” service to make sure that we send our commands to the correct application.

Our program will now do two basic tasks. The first is to move the mouse cursor to an offset of (100, 100) from the window’s top left corner. The second task is to hold the left mouse button down, drag the mouse to draw a square that is 50 pixels by 50 pixels and then release the left mouse button to draw the resulting textured square.

The first task is achieved by the first Nesting component that we have labeled “Place cursor” and the second is achieved by the Nesting component labeled “Draw rect”.

Here is what “Place cursor” does:



We first use the “Window pos (PID)” service to get the position of the application’s window on the screen. Output data port 0 tells us the left coordinate of the window and output data port 2 tells us the top coordinate of the window. We then add 100 to each of these coordinates and send them to the “Mouse move” service through input data port 0 and 1. This instructs the target application to move the mouse cursor to an offset of (100, 100) from the top left corner of the window.

In the “Draw rect” Nesting component” we give the mouse commands to draw our textured square:



The first component uses the “Mouse click” service with an input argument “Left down” in input data port 0. This causes a simulation of the left mouse button being clicked and held down. Next, we use the “Mouse move” service to drag the mouse cursor to the right by 50 pixels and down by 50 pixels. This is accomplished by setting the input data ports 0 and 1 to 50 – if you double click these ports then you will see in our program that a value of 50 has been set. By moving the mouse while the left mouse button is held down, we effect a drag operation and in Graphics program, this causes a square to be drawn. Finally, the last component uses the “Mouse click” service as well but its input argument is “Left up”, which causes the left mouse button to be released. In the Graphics program, this completes the drawing command and generates a textured square. If we run the program we get the following result:

Back to top
View user's profile Send private message Send e-mail Visit poster's website
Display posts from previous:   
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    codemorphis.com Forum Index -> Visual Programming Forum All times are GMT - 5 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group