Window Activation Trigger

Sometimes FileMaker developers miss a script trigger to run when you switch between windows. We usually recommend people check if Window.Current function returns a new number. And you may do that with Schedule functions.

Here is a script to start a schedule to check the window regularly:

# current window
Set Variable [ $currentWindow ; Value: MBS("Window.Current") ] 
Set Variable [ $r ; Value: MBS( "FM.VariableSet"; "WindowActivateTriggerCurrentWindow"; $currentWindow ) ] 
# Schedule a calculation to check window every second
Set Variable [ $schedule ; Value: MBS( "Schedule.EvaluateAfterDelay"; 1; "Let([
 
lastWindow = MBS( \"FM.VariableGet\"; \"WindowActivateTriggerCurrentWindow\" );
e1 = MBS(\"IsError\"); 
currentWindow = MBS( \"Window.Current\" );
e2 = MBS(\"IsError\");
 
r = If(e1 = 0 AND e2 = 0 AND lastWindow ≠ currentWindow; Let([
 
	w = MBS( \"FM.VariableSet\"; \"WindowActivateTriggerCurrentWindow\"; currentWindow );
	e = MBS( \"FM.RunScript\"; \"" & Get(FileName) & "\"; \"Window Activated Trigger\")
 
]; 0); 0)
 
];1)"; ""; ""; 1 ) ] 
Set Variable [ $r ; Value: MBS( "FM.VariableSet"; "WindowActivateTrigger"; $schedule ) ] 

As you see, we use a variable via MBS Plugin to store the number of the current window independent of the scope of $$ variables. The calculation is evaluated once a second (when FileMaker is not busy) and if we get the window number, we remember it and trigger the script.

To stop the trigger, we can release the schedule:

# stop trigger if it is running
If [ MBS( "FM.VariableExists"; "WindowActiateTrigger" ) ] 
	Set Variable [ $w ; Value: MBS( "FM.VariableGet"; "WindowActivateTrigger" ) ] 
	Set Variable [ $r ; Value: MBS("Schedule.Release"; $w) ] 
	Set Variable [ $r ; Value: MBS( "FM.VariableClear"; "WindowActivateTrigger" ) ] 
	Set Variable [ $r ; Value: MBS( "FM.VariableClear"; "WindowActivateTriggerCurrentWindow" ) ] 
End If

Please try and comment. Let us know if you have questions. The example database will be included with the next pre-release.

1 Like