FOCUS

<variable> = FOCUS(<"Window Title">,<Timeout in Seconds>, [Option])

Wait for the window with <Window Title> to appear within the specified amount of time and then set the focus to it. When FOCUS command finds the matching window, this window becomes the current destination window. Therefore, subsequence SEND or SYSCMD commands will be directed to this window. It's important that you set the focus to every popup window that may need to be automated even if the window is a simple open file dialog.

Example:

'-- Change the focus to "Untitled - Notepad"
IF FOCUS("Untitled - Notepad", 5, 1) = 0 THEN
'-- If never appears within the time then report error
PROMPT "Cannot Find Notepad", 5
ELSE
SEND "Hello, World!!! "
'-- Select File | New
SEND "@fn"
'-- see if notepad popup save changes dialog
'-- if so WAITWIN will set the focus to it
IF WAITWIN("Notepad", "&Yes", 2) = 1 THEN
'-- Send N for Not to save
SEND "N"
ENDIF
ENDIF

Parameters:

<Window Title> is the title to look for (case insensitive). There are some special key characters that tells FOCUS to perform special functions:

"*" Tell FOCUS() to focus a window previously had the focus before Shortcut. This is necessary when you need to first make a selection from Shortcut to automate a window of your interest.

"^" Tell FOCUS() to focus a window that is currently in focus.

<Time in Seconds> specifies how long to wait. If the window does not appears within the specified time then FOCUS returns 0, otherwise, it returns 1.

[Option] is an optional parameter which determines how FOCUS finds the window. If you leave this option out, Shortcut defaults to value to 1.
1 = Left-most part of the title must match <Window Title>
2 = Any part of the title that matches <Window Title>

Returns:
0 = if it cannot find any matching window within the specified amount of time
1 = if it finds the matching window.

Example #1:
'-- Change the focus to "Untitled - Notepad"
x = FOCUS("Untitled - Notepad", 10, 1)
'-- If the window never appears within the time then
'-- report error
IF x = 0 THEN
PRINT "Time Out Error"
ELSE
SEND "Hello Notepad."
ENDIF

Example #2:
'-- Change the focus to "Untitled - Notepad"
'-- If the window never appears within the time then
'-- report error
IF FOCUS("Untitled - Notepad", 10, 1) = 0 THEN
PRINT "Time Out Error"
ELSE
SEND "Hello Notepad."
ENDIF

See Also

MACRO

SEND

SYSCMD

GETFOCUS

Current destination window

Shortcut Script Syntax