# How to create an if else calculation?

I am trying to create an if else calculation and am thinking along the lines of this:

Except I don't know the correct syntax how to use a clipboard value in the calculation and unsure if this is the correct action to create a calculation in the 'else' section.

If anyone has any advice I would appreciate it greatly!

When you use an Action that requires a calculation, you do NOT use the %variable% notation. You just enter the variable name.

BTW, in your image you show `saturationfunction/10 x 40`.
Well, since 40/10 is 4, and the multiplication symbol is "`*`", you would just use:
`saturationfunction * 4`

This macro should server as an example:
Change the "YourNewVariable" to whatever you would like.

###Macro: TEST Variable Calculation

Thank you for your help, but the number characters in the program I will be checking I need to see if they are over 40 and if so then use this number as a percentage of 40 to replace the original number. This is the reason of dividing the original number by 10 in brackets (to create a decimal point number) to multiply against 40.

For example if the number in the program I am using is 69, since 69 is higher than 40 I would like to replace the 69 number in the program with 69% of 40 which would be 27.6.

Well, then you need to be dividing by 100 and not 10, but JMicaheelTx’s point still stands - dividing by 100 and then multiplying by 40 is the same as just multiplying by 0.4

Hey Simon.

When posting macro and/or action graphics to the forum please take a second and narrow down the width of the action pane in the Keyboard Maestro Editor, so there’s not so much waste in the posted image. (I’ve edited the posted image this time.)

I had this sitting ready to post, so despite JM’s coverage I’m going to post it anyway.

You’re misunderstanding the different types of fields in Keyboard Maestro.

A calculation field will take a bare variable name without the “%” delimiter.

For that matter a delimited variable name (for use in a text-field) must have this format:

%Variable%YourVariableName%

So. Your first calculation field should look like this:

`saturationfunction > 40`

The next set variable to calculation should be:

`(saturationfunction/10) * 40`

Your next action is Set Variable to Text NOT a calculation. If you want to do a calculation in a text field you have to do it like this:

`%Calculate%var * 10%`

See menu: Edit > Insert Token > Calculation

As far as I’m aware you cannot dump the `%CurrentClipboard%` token directly into a text-field calculation – nor can you use it directly in a calculation field.

You have to get the %CurrentClipboard% to a variable and then use the variable name in your calculations.

-Chris

Ok I will try this thank you both

I have created a new version of the macro but I get an error when the calculation is done. I created a new clipboard specially for the macro but this doesn't appear to be able to select in the 'token' menu when I'm setting my variable to the clipboard. Instead I have typed the clipboard's name between %%. Could this be the cause of my error? I have decided to ditch the if/else statement.

I'll iterate what I'm trying to achieve again to be clear, I have a number value in a program that once highlighted I am copying to the clipboard, then a variable and then carrying out a calculation on this value which is set as the new value of this variable. The variable is then copied back to the clipboard 'saturation function' which shall then be pasted back in the original number value text box to replace it.

Where have I went wrong?

Where you are going wrong is using Named Clipboards. There is no need to use a named clipboard in this macro (the only time you need to use a named clipboard is if you have styled text or images or such that you need to store).

So the actions you want are (simplistically matching what you’ve got).

Note especially the difference between the text fields (in actions 2 & 4) and the numeric field (in action 3).

Now this can be done more simply as:

Note also that because the text is already selected there is no need to delete it first, the typing/pasting will replace it, but if you really want to you can keep the Delete keystroke before the Paste/Insert Text by Typing or Pasting action.

I can’t use the current clipboard as it is used to store other information as this is part of a larger macro, this is why I wanted to create a new clipboard.

There is no “new clipboard”.

There is only one system clipboard, and that is where things go to when you copy them and where they are pasted from. You cannot get the selection except by copying it into the system clipboard (not in general anyway) and then copying it from the system clipboard to somewhere else (like a variable), and you cannot paste into the application except by putting the data into the system clipboard and pasting it from there.

So if you need to preserve the system clipboard, then you need to save a copy of it and restore it later, which is fraught with peril (see the wiki FAQ: Why is the clipboard not restored after any clipboard action?).

Why there an option to create more than one clipboard and name for that clipboard in keyboard maestro if they can’t be used in variables?

Found out how to do it, I just need to use a named clipboard which gave me the syntax

%NamedClipboard%saturation function%

it works fine now.

Thank you to everyone who helped me

The other time that I often use a Named Clipboard is when I want to preserve the current clipboard before I copy a selection to the clipboard.

####So, at the beginning of my macro, I do this:

Then, the Macro Actions I want to use.

####At the end of my macro, I do this, which restores the System Clipboard to its contents BEFORE I ran my macro:

Note that "SAVE" is the name I gave my Named Clipboard.

I see how that works but I find using a named clipboard works just aswell.

Hey Simon,

You said you needed to retain the current clipboard.

What I’m not sure you understand is that ALL copied and pasted items MUST go through the system clipboard.

When you copy to a named clipboard it FIRST copies to the system clipboard and then makes a copy in the named clipboard.

When you paste from a named clipboard that data is copied to the system clipboard and then pasted.

So anything on the system clipboard is destroyed.

UNLESS you first save the system clipboard to a different named clipboard.

You can then restore it as needed.

-Chris