IF THEN vs Switch

Last night I used this trick on an old macro, using the assigned key in parenthesis :wink:
This is a great trick. Thanks!

2 Likes

I was checking the wiki page. I can see how using just / or /. is assigned to Enter/Return or Escape. Thanks

1 Like

Glad to help! It is great being able to assign buttons to keys. :grin:

1 Like

Do you mind sharing the 2 versions so I can see what you mean?

Yes, I can see how having Switch instead of IF THEN looks better and cleaner, especially when it comes to nesting IF THEN actions, which is kinda messy

1 Like

I think the wiki page explains this

The actions in the first section that matches will be executed. If no section matches, then no actions will be executed and the execution will continue after the Switch/Case action. Under no circumstances will actions from more than one section be executed - that is, execution does not “fall through” to following sections as happens in some languages.

So basically, once there's a match (reading top to bottom), it leaves the Switch action and goes to the next action.

2 Likes

Thank you all for sharing your approaches to Switch vs If Then.
Always learning a few tricks here and there :wink:

1 Like

That is really good to know, I assumed (always a bad idea) that it continued on.

Switc/Case First Only

Switc-Case Stops.kmmacros (2.6 KB)

I wonder what the reasoning behind that is. Seems like it would be way more powerful if it could do all and other than being clean and easier to read over an If Then Else action I was super excited to use this for multiple statements being true.

Dan was exactly right according to my question but I got exciting thinking I could also use this to make multiple things be true through variables and use the clean Switch Case instead of having to use multiple switch case actions to do the same thing. I guess in some ways it makes for an easy hierarchy so you can put what you want most to be seen and then stop. If they are all the same priority then just make multiple Switch Case actions (Or If Then Else with no Else).


I very much agree!

You inspired me to finally just make a quick action macro for this.

Menu Prompt

Create Action - User Prompt.kmmacros (48 KB)

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<array>
	<dict>
		<key>ActionName</key>
		<string>Group - Trigger Macros through button promps with a menu to help guide what you are doing.</string>
		<key>ActionUID</key>
		<integer>16817810</integer>
		<key>Actions</key>
		<array>
			<dict>
				<key>ActionUID</key>
				<integer>16817811</integer>
				<key>Actions</key>
				<array>
					<dict>
						<key>ActionUID</key>
						<integer>16817812</integer>
						<key>IsDisclosed</key>
						<false/>
						<key>MacroActionType</key>
						<string>Comment</string>
						<key>StyledText</key>
						<data>
						cnRmZAAAAAADAAAAAgAAAAcAAABU
						WFQucnRmAQAAAC65CgAAKwAAAAEA
						AACxCgAAe1xydGYxXGFuc2lcYW5z
						aWNwZzEyNTJcY29jb2FydGYyODIx
						Clxjb2NvYXRleHRzY2FsaW5nMFxj
						b2NvYXBsYXRmb3JtMHtcZm9udHRi
						bFxmMFxmc3dpc3NcZmNoYXJzZXQw
						IEhlbHZldGljYTtcZjFcZm5pbFxm
						Y2hhcnNldDAgSGVsdmV0aWNhTmV1
						ZTt9CntcY29sb3J0Ymw7XHJlZDI1
						NVxncmVlbjI1NVxibHVlMjU1O1xy
						ZWQwXGdyZWVuMFxibHVlMDtccmVk
						MTFcZ3JlZW4xMlxibHVlMTI7XHJl
						ZDI1NVxncmVlbjI1NVxibHVlMjU1
						OwpccmVkNDVcZ3JlZW40OVxibHVl
						NTM7fQp7XCpcZXhwYW5kZWRjb2xv
						cnRibDs7XGNzc3JnYlxjMFxjMFxj
						MFxjODQ3MDZcY25hbWUgY29udHJv
						bFRleHRDb2xvcjtcY3NzcmdiXGM0
						NzA2XGM1MDk4XGM1NDkwO1xjc3Ny
						Z2JcYzEwMDAwMFxjMTAwMDAwXGMx
						MDAwMDA7Clxjc3NyZ2JcYzIzMTM3
						XGMyNTA5OFxjMjcwNTk7fQp7XCpc
						bGlzdHRhYmxle1xsaXN0XGxpc3R0
						ZW1wbGF0ZWlkMVxsaXN0aHlicmlk
						e1xsaXN0bGV2ZWxcbGV2ZWxuZmMy
						M1xsZXZlbG5mY24yM1xsZXZlbGpj
						MFxsZXZlbGpjbjBcbGV2ZWxmb2xs
						b3cwXGxldmVsc3RhcnRhdDFcbGV2
						ZWxzcGFjZTM2MFxsZXZlbGluZGVu
						dDB7XCpcbGV2ZWxtYXJrZXIgXHtk
						aXNjXH19e1xsZXZlbHRleHRcbGV2
						ZWx0ZW1wbGF0ZWlkMVwnMDFcdWMw
						XHU4MjI2IDt9e1xsZXZlbG51bWJl
						cnM7fVxmaS0zNjBcbGk3MjBcbGlu
						NzIwIH17XGxpc3RuYW1lIDt9XGxp
						c3RpZDF9fQp7XCpcbGlzdG92ZXJy
						aWRldGFibGV7XGxpc3RvdmVycmlk
						ZVxsaXN0aWQxXGxpc3RvdmVycmlk
						ZWNvdW50MFxsczF9fQpccGFyZFx0
						eDU2MFx0eDExMjBcdHgxNjgwXHR4
						MjI0MFx0eDI4MDBcdHgzMzYwXHR4
						MzkyMFx0eDQ0ODBcdHg1MDQwXHR4
						NTYwMFx0eDYxNjBcdHg2NzIwXHBh
						cmRpcm5hdHVyYWxccGFydGlnaHRl
						bmZhY3RvcjAKClxmMFxmczI2IFxj
						ZjIgVGhlIGZvcndhcmQgc2xhc2gg
						d2l0aCBub3RoaW5nIGFmdGVyIG1h
						a2VzIGl0IHRoZSBkZWZhdWx0IGFj
						dGlvbiBoaWdobGlnaHRlZCBvbmx5
						IGlmIHRoZXJlIGFyZSBubyB2YXJp
						YWJsZSBjb25kaXRpb25zLCB0aGVu
						IHRoZSBmaXJzdCBidXR0b24gd2ls
						bCBiZSBzZWxlY3RlZC5cCntcZmll
						bGR7XCpcZmxkaW5zdHtIWVBFUkxJ
						TksgImh0dHBzOi8vd2lraS5rZXli
						b2FyZG1hZXN0cm8uY29tL2FjdGlv
						bi9Qcm9tcHRfZm9yX1VzZXJfSW5w
						dXQifX17XGZsZHJzbHQgaHR0cHM6
						Ly93aWtpLmtleWJvYXJkbWFlc3Ry
						by5jb20vYWN0aW9uL1Byb21wdF9m
						b3JfVXNlcl9JbnB1dH19XApcClxw
						YXJkXHBhcmRlZnRhYjcyMFxzYTMz
						MFxwYXJ0aWdodGVuZmFjdG9yMAoK
						XGYxXGZzMzAgXGNmMyBcY2I0IFxl
						eHBuZDBcZXhwbmR0dzBca2Vybmlu
						ZzAKRm9yIE1hY09TIC0ge1xmaWVs
						ZHtcKlxmbGRpbnN0e0hZUEVSTElO
						SyAiaHR0cHM6Ly9kZXZlbG9wZXIu
						YXBwbGUuY29tL2Rlc2lnbi9odW1h
						bi1pbnRlcmZhY2UtZ3VpZGVsaW5l
						cy9idXR0b25zIn19e1xmbGRyc2x0
						IGh0dHBzOi8vZGV2ZWxvcGVyLmFw
						cGxlLmNvbS9kZXNpZ24vaHVtYW4t
						aW50ZXJmYWNlLWd1aWRlbGluZXMv
						YnV0dG9uc319XGNiMSBcClxwYXJk
						XHBhcmRlZnRhYjcyMFxwYXJ0aWdo
						dGVuZmFjdG9yMApcY2Y1IFxjYjQg
						QSBidXR0b24gdGhhdCBpbml0aWF0
						ZXMgYW4gYWN0aW9uIGlzIGZ1cnRo
						ZXN0IHRvIHRoZSByaWdodC4gVGhl
						IENhbmNlbCBidXR0b24gaXMgdG8g
						dGhlIGxlZnQgb2YgdGhpcyBidXR0
						b24uXApcClxwYXJkXHBhcmRlZnRh
						YjcyMFxzYTMzMFxwYXJ0aWdodGVu
						ZmFjdG9yMApcY2YzIEZvciBXaW5k
						b3dzIC0gVGhlIFdpbmRvd3MgTGF5
						b3V0IE1ha2VzIG1vcmUgc2Vuc2Ug
						d2hlbiB0aGVyZSBhcmUgbW9yZSB0
						aGFuIG9uZSBidXR0b24gYW5kIHRo
						YXQgaXMgdGhlIG9yZGVyIEkgd2ls
						bCBmb2xsb3cuXGNmNSBcClByZXNl
						bnQgdGhlIGNvbW1pdCBidXR0b25z
						IGluIHRoZSBmb2xsb3dpbmcgb3Jk
						ZXI6XGNiMSBcClxwYXJkXHR4MjIw
						XHR4NzIwXHBhcmRlZnRhYjcyMFxs
						aTcyMFxmaS03MjBcc2ExNjVccGFy
						dGlnaHRlbmZhY3RvcjAKXGxzMVxp
						bHZsMFxjZjUgXGNiNCBca2Vybmlu
						ZzFcZXhwbmQwXGV4cG5kdHcwIHtc
						bGlzdHRleHQJXHVjMFx1ODIyNiAJ
						fVxleHBuZDBcZXhwbmR0dzBca2Vy
						bmluZzAKT0svW0RvIGl0XS9ZZXNc
						Y2IxIFwKXGxzMVxpbHZsMFxjYjQg
						XGtlcm5pbmcxXGV4cG5kMFxleHBu
						ZHR3MCB7XGxpc3R0ZXh0CVx1YzBc
						dTgyMjYgCX1cZXhwbmQwXGV4cG5k
						dHcwXGtlcm5pbmcwCltEb24ndCBk
						byBpdF0vTm9cY2IxIFwKXGxzMVxp
						bHZsMFxjYjQgXGtlcm5pbmcxXGV4
						cG5kMFxleHBuZHR3MCB7XGxpc3R0
						ZXh0CVx1YzBcdTgyMjYgCX1cZXhw
						bmQwXGV4cG5kdHcwXGtlcm5pbmcw
						CkNhbmNlbFxjYjEgXApcbHMxXGls
						dmwwXGNiNCBca2VybmluZzFcZXhw
						bmQwXGV4cG5kdHcwIHtcbGlzdHRl
						eHQJXHVjMFx1ODIyNiAJfVxleHBu
						ZDBcZXhwbmR0dzBca2VybmluZzAK
						QXBwbHkgKGlmIHByZXNlbnQpXGNi
						MSBcClxwYXJkXHR4MjIwXHR4NzIw
						XHBhcmRlZnRhYjcyMFxsaTcyMFxm
						aS03MjBccGFydGlnaHRlbmZhY3Rv
						cjAKXGxzMVxpbHZsMFxjZjUgXGNi
						NCBca2VybmluZzFcZXhwbmQwXGV4
						cG5kdHcwIHtcbGlzdHRleHQJXHVj
						MFx1ODIyNiAJfVxleHBuZDBcZXhw
						bmR0dzBca2VybmluZzAKSGVscCAo
						aWYgcHJlc2VudClcY2IxIFwKXHBh
						cmRccGFyZGVmdGFiNzIwXHNhMzMw
						XHBhcnRpZ2h0ZW5mYWN0b3IwClxj
						ZjMgXGNiNCBTbyBDYW5jZWwgaXMg
						YWx3YXlzIG9uIHRoZSByaWdodCBv
						ZiBPSyBidXR0b24ufQEAAAAjAAAA
						AQAAAAcAAABUWFQucnRmEAAAAMe4
						sGe2AQAAAAAAAAAAAAA=
						</data>
						<key>Title</key>
						<string>Comment</string>
					</dict>
					<dict>
						<key>ActionUID</key>
						<integer>16817813</integer>
						<key>Buttons</key>
						<array>
							<dict>
								<key>Button</key>
								<string>Button 1 (A)/a</string>
								<key>Cancel</key>
								<false/>
							</dict>
							<dict>
								<key>Button</key>
								<string>Button 2 (2)/2</string>
								<key>Cancel</key>
								<false/>
							</dict>
							<dict>
								<key>Button</key>
								<string>Okay (Highlighted Blue)/</string>
								<key>Cancel</key>
								<false/>
							</dict>
							<dict>
								<key>Button</key>
								<string>Cancel/.</string>
								<key>Cancel</key>
								<true/>
							</dict>
						</array>
						<key>MacroActionType</key>
						<string>PromptForUserInput</string>
						<key>Prompt</key>
						<string>What calendar would you like this action put on? 
"Automation/Programming?”

For your last Pomodoro, you did:
 “Automation/Programming (Keyboard Maestro Forum)”</string>
						<key>TimeOutAbortsMacro</key>
						<true/>
						<key>Title</key>
						<string>Session App Pomodoro Action and Category</string>
						<key>Variables</key>
						<array/>
					</dict>
				</array>
				<key>MacroActionType</key>
				<string>Group</string>
				<key>TimeOutAbortsMacro</key>
				<true/>
			</dict>
			<dict>
				<key>ActionUID</key>
				<integer>16817815</integer>
				<key>CaseEntries</key>
				<array>
					<dict>
						<key>Actions</key>
						<array>
							<dict>
								<key>Action</key>
								<string>DisplayLarge</string>
								<key>ActionUID</key>
								<integer>16817865</integer>
								<key>MacroActionType</key>
								<string>InsertText</string>
								<key>Text</key>
								<string>Button 1</string>
							</dict>
						</array>
						<key>ConditionType</key>
						<string>Is</string>
						<key>TestValue</key>
						<string>Button 1 (A)</string>
					</dict>
					<dict>
						<key>Actions</key>
						<array>
							<dict>
								<key>Action</key>
								<string>DisplayLarge</string>
								<key>ActionUID</key>
								<integer>16817866</integer>
								<key>MacroActionType</key>
								<string>InsertText</string>
								<key>Text</key>
								<string>Button 2</string>
							</dict>
						</array>
						<key>ConditionType</key>
						<string>Is</string>
						<key>TestValue</key>
						<string>Button 2 (2)</string>
					</dict>
					<dict>
						<key>Actions</key>
						<array>
							<dict>
								<key>Action</key>
								<string>DisplayLarge</string>
								<key>ActionUID</key>
								<integer>16817867</integer>
								<key>MacroActionType</key>
								<string>InsertText</string>
								<key>Text</key>
								<string>Okay</string>
							</dict>
						</array>
						<key>ConditionType</key>
						<string>Is</string>
						<key>TestValue</key>
						<string>Okay (Highlighted Blue)</string>
					</dict>
				</array>
				<key>MacroActionType</key>
				<string>Switch</string>
				<key>Source</key>
				<string>Text</string>
				<key>Text</key>
				<string></string>
			</dict>
		</array>
		<key>MacroActionType</key>
		<string>Group</string>
		<key>TimeOutAbortsMacro</key>
		<true/>
	</dict>
</array>
</plist>

Not a fan of Apple's suggested order of buttons if there are more than one. Peter was smart in how he listed them to follow Apple's standards with Cancel at the end but shows first. If there are no variables I like having the first button highlighted rather than cancel but oh well. The action button on the right makes sense for two buttons and right handed people holding a phone.


Brilliant. Thanks for sharing, and good to see how much faster Switch/Case is.

Interesting

If Then Else vs. Switch/Case Action… why does one work and the does not?


The first three Switch/Case actions (System Clipboard, Named Clipboard, and Trigger Clipboard) are all found under one If Then Else action of "Clipboard Condition."

System Clipboard (Switch/Case) = Clipboard Condition (If Then Else)
Named Clipboard (Switch/Case) = Clipboard Condition (If Then Else)
Trigger Clipboard (Switch/Case) = Clipboard Condition (If Then Else)

Variable (Switch/Case) = Variable Condition (If Then Else)
Text (Switch/Case) = Text Condition (If Then Else)

Calculation (Switch/Case) ≠ Calculation Condition (If Then Else) It is more powerful in the Switch condition then it is in If Then Else but there is probably a way around.

Calulation

image

Here are the options in the Calculation Switch Case that you don't get in If Then Else
image

image

File (Switch/Case) ≠ File Attribute (If Then Else) Many more options in If Then Else action.

File (Switch/Case)vs File Attribute (If Then)

image

Switch Case - If Statements

If Then Else - Statements Conditions

On the other hand, the "If Then Else" action supports many more conditions, including:

    • Action Result Condition
    • Application Condition
    • Button Condition
    • Environment Variable Condition
    • File Attribute Condition (More Powerful than Switch/Case)
    • Found Image Condition
    • Key Condition
    • Location Condition
    • Macro/Macro Group Condition
    • Menu Condition
    • Modifiers Condition
    • Mounted Volume Condition
    • Mouse Button Condition
    • OCR Condition
    • Path Condition
    • Pixel Condition
    • Script Condition
    • Typed String Condition
    • USB Device Condition
    • Any Window Condition
    • Front Window Condition
    • Wireless Network Condition
Key Differences
  1. More Conditions in "If Then Else"
  • "If Then Else" can evaluate conditions based on UI elements, environment, images, scripts, keys, and external devices, which "Switch/Case" does not support.
  1. "Switch/Case" Is Primarily for Matching Values
  • It works best when you need to check a single value against multiple possibilities (like a clipboard or variable content).
  1. "If Then Else" Can Handle Complex Logic
  • It allows multiple types of conditions (e.g., "If an application is running AND a specific modifier key is held AND a file exists").

Of course, please feel free to correct anything here. This is me making sense of everything and when to use which.

In some languages it does. The downside is that you have to include an explicit break; or similar when you don't want it to.

You can do "multiple statements being true" by putting multiple truths into your case :wink: You can then separate those results with further "If" or "Switch" actions:

Switch:If.kmmacros (10.7 KB)

Image

That's cool and would make the Switch/Case pretty easy to read still if that were an option. Something like a simple "Stop If True" underneith each switch case plus or minus and select "yes" or "no" (having déjà vu moment here for some reason).

Thanks for the macro and absolutely, That is where it starts getting a little hairy to read but not too bad in this example.

It can get quite tricky quite quickly, since all the actions within the first match happen then all the actions in the next match and so on until a break; is found. The actual logic can get difficult to follow because fall-through cases execute even when they don't match.

Example JavaScript you can put in Script Editor and change the number in the first line to see behaviour:

const foo = 1;
switch (foo) {
  case 1:
  	console.log("It was 1");
  case 2:
  	console.log("It was 2");
	break;
  case 3:
  	console.log("It was 3");
  default:
    console.log("A number greater than 3");
}

As written:

And you can see both case 1 and case 2 are evaluated -- obvious when you pay attention, a brain-melter if you don't!