Need help with a window condition and action

It always seems like I’ve run out of ways to get confused about variables, tokens, conditions, actions, text, and calculations, but I have just run aground again and need some help.

What I want to do is call macro “Resize Window” with parameter width, height. Then, I want to resize the front window according to the parameter, but making sure that the window edges are:

  • at least 15 pixels from the left edge of the screen
  • 15 pixels from the right
  • 25 from the top, and
  • 80 from the bottom. I don’t care what happens if the window is too big to meet those constraints.

So I have steps like the following, a mix of real KM “code” and “abstract code”. I need instructions supplying the right %s, %Calculation%%, etc.

If all of the following are true:
     The calculation:
        Left of Front Window + TriggerValue[1] < SCREEN(0, Right) - 15
     returns true
     execute the following actions:
        Set the Variable 'New Left' to Text
            MAX(15, Left of Front Window)
    otherwise execute the following actions:
        Set the Variable 'New Left' to Text
             SCREEN(0, Right) - TriggerValue[1] - 15
    . . .

I do something similar for New Top. Then at the end I want to Move Front Window to Position New Left, New Top. What should go in the Move to lines? New Left and New Top appear in orange.

So basically you’re ensuring that the size you are requesting will be small enough that it will fit within the constraints?

So if you resize the window, and it would be off the screen to the right, you want to move the window left so that the constraint is just met?

So the basic process would be:

  • Set variable Frame to calculation WINDOW(1,Left),WINDOW(1,Top)
  • Set variable Frame to to text %Variable%Frame%,%TriggerValue%

So now frame will have a value like left, top, width, height. You can use the Use Variable action to set the window frame directly from this.

  • if calculate Frame.Right > SCREEN(Main,Right) - 15
    • Set variable Frame to calculation SCREEN(Main,Right) - Frame.Width - 15,Frame.Top,Frame.Width,Frame.Height
  • if calculate Frame.Bottom > SCREEN(Main,Bottom) - 80
    • Set variable Frame to calculation Frame.Top,SCREEN(Main, Bottom) - Frame.Height - 80,Frame.Width,Frame.Height
  • if calculate Frame.Left < SCREEN(Main,Left) + 15
    • Set variable Frame to calculation SCREEN(Main,Left) + 15,Frame.Top,Frame.Width,Frame.Height
  • if calculate Frame.Top < SCREEN(Main,Top) + 25
    • Set variable Frame to calculation Frame.Top,SCREEN(Main,Top) + 25,Frame.Width,Frame.Height

Then use the variable to set the front window frame.

Something like that should work, and tracking the value of the Frame variable should show any issues.

Sorry if I wasn’t clear — I want to specify the size, resize the window to that size, and reposition to meet my constraints, in particular that it completely fits on the screen.

This is in a submacro, so width and height are specified by TriggerValue. Each macro that calls it calls it with a width, height pair of numbers.

The main thing I need help with is turning the logical code — yours or mine — into legitimate KM condition and action “code”. I was too embarrassed to post the mess I have, so I was hoping I could get away with asking for someone to translate my abstract code. Here is an example:

If all of the following return true
    the calculation
        %WindowPosition%0%[1] + TriggerValue[1] < SCREEN(0, Right) - 15 
    execute the following actions:
        Set Variable New Left to
            %Calculation%MAX(15, %WindowPosition%1%[1])%
    otherwise execute the following actions:
        Set Variable New Left to
            SCREEN(0, Right) - %TriggerValue%[1] - 15

and the equivalent for New Top. Displaying New Left, New Top gives me a horror such as

SCREEN(0, Right) - 740, 1007[1] - 15, MAX(25, 
MIN(WindowPosition[2],SCREEN(0, Bottom) - 80 - 740, 1007[2

so obviously things are not being evaluated that should be. I have struggled with this for quite a while, again to my embarrassment. So I am looking for the exact KM formulation of my conditions and variable setting. My problems here indicate my continuing confusing about using functions, tokens, calculations, etc.

I translated Peter's pseudo-code here. Note that I could have easily fat-fingered something, so if it doesn't work right, compare to what Peter wrote.

Oh, and change the first action. Change "900,700" to %TriggerValue%" (no quotes).

Set Window Size On Screen.kmmacros (5.9 KB)

1 Like

A big problem in what I was trying to do is that I mistakenly used Set Variable to Text not Set Variable to Calculation.

That would do it. :slight_smile:

How would I refer to TriggerValue[1] and TriggerValue[2] in the calculation of a Set Variable to Calculation? Your solution avoids that by using TriggerValue in a Set Variable to Text. When I use TriggerValue in the calculation field it is shown in orange — what does that mean? I was trying to use it as TriggerValue[1] or [2].

Also, where did Top, etc. come from in your references to Frame.Top, etc.? It looks like any 4-value array can be treated as .Top, .Width, etc.

First off, see this:

Secondly, I believe you can use Variables in calculations, but not Tokens. So I think you have to set a variable to %TriggerValue%, then you can refer to variable.x and variable.y in calculations.

Does this answer your question? This always confuses me.

Thanks. I think there’s often a subtext to my questions: “does this confuse you too?” :slight_smile:

1 Like

I'm still not getting this completely. What's wrong with this:

With TriggerValue 900, 1100,
%Variable%Width% shows 900, 1100[1]

First of all, I believe you can only use that notation in a Calculation.

Secondly, you do not use percent signs in Calculations.

Thirdly, this will fail:

You have to use this construct:

  1. Using Set Variable to Text, store %TriggerValue% in a variable.
  2. Now you can use the variable in a Calculation.

But you said you thought you can’t use tokens in calculations.

What I showed was a Set Variable to Text. Why can’t I use a token there?

It seems to be stretching things to have to first set a variable to the value of text that contains just a token, then set another variable that unpacks that token, and finally use that variable.

You can use a token in a text field. You just can’t use the .x, .y, [1] or [2] syntax in a text field - that only works in a Calculation field.

Hey, I didn’t write KM. :slight_smile:

To be completely honest here, it wouldn’t surprise me if Peter or someone came along and said I was wrong. In fact, I expect it to happen. This has confused me from the start.

Until then, what I showed you works. Either use what I showed you, or prove me wrong. Which, by the way, would be totally fine. I’ve been wrong so many times about this subject that it would be kind of strange to actually be right.

No, we just worship it. :joy:


One thing that befuddled me is that in Set Variable to Text, %TriggerValue%[1] did not show red. With TriggerValue 900, 1100 the value of the variable after setting it to text %TriggerValue%[1] was 900, 1100[1]

Right. Because it’s in a text field.


replace %TriggerValue% with its contents, and you end up with

900, 1100



replace %TriggerValue% with its contents, and you end up with

900, 1100[1]

Because “[1]” doesn’t mean anything special in a text field, so it gets treated just like any other text. In a text field.

You cannot use tokens in a calculation field. So to use the %TriggerValue% token at all, you must use it in a text field, such as that found in the Set Variable to Text action.

You cannot apply array indexing (or coordinate dot notation like Variable.x) to a text token.

So if you want the concept of TriggerValue[2], then you need to use Set Variable to Text to set a variable to %TriggerValue% and then use a calculation field Var[2].

Pretty much nothing in a text field will show up red because nothing is invalid in a text field - it just results in some string of characters.