Basics of Creating a Counter

I don't understand the basics of building a counter which will then be attached to a filename.

I used "Set Variable ... To Calculation" as follows:

Set Variable Count
to: Count + 1

This appears to be incorrect since the "+" sign is red when entered.

The bottom of the pane says "empty" perhaps implying that there needs to be an initial condition but I don't see how this could be established.

I'd appreciate any help with understanding who to construct a counter.

Thanks.

This should serve as a good example to get you started.
As always, please feel free to post any comments, issues, and/or suggestions you may have concerning this macro.

Example Output

image

MACRO:   How to Use a Counter [Example]


#### DOWNLOAD:
<a class="attachment" href="/uploads/default/original/3X/5/5/55417b8b2628112929bf26e976c6ff7adc01ad31.kmmacros">How to Use a Counter [Example].kmmacros</a> (4.7 KB)
**Note: This Macro was uploaded in a DISABLED state. You must enable before it can be triggered.**

---

![image|617x998](upload://1FLUVAdNnCJBXsEHFMyea9gtChq.jpg)

---

**NOTE:** The `+ 1` shows up in red because the variable "Local__Counter" is a _local_ variable, and is not defined until execution time.

![image|543x170](upload://z1RFB8qzNFR0EBfGjQ3sBm7gBEr.png)

@peternlewis, this display behavior does not make any sense to me, and misleads us.
You should not show any "errors" in red related to _Local_ or _Instance_ variables.
Please consider making a change to KM.
2 Likes

2 posts were split to a new topic: Request: Change Behavior of KM Editor Display of "Invalid" Actions

Thanks for your response.

I'm still having a problem understanding the counter.

Just a little background: My use of the counter is to attach the counter number to a file name when the file is saved. The original file is an email that I'm exporting as a pdf and then naming before its saved.

Results
I did some experimenting with the counter as a result of your macro and this resulted in getting the macro "seeded" with the number 1. However none of my experiments worked so I reverted to using simply:

Set Variable Count
to Count +1

As a result of seeding the macro with a previous value (1) when I ran the macro a second time it worked as expected and added the number 2 to the filename.

This suggests to me that my lack of understanding has to do with the initial condition. Meaning that the first time through the action if there is no history of a value the calculation cannot be completed.
I can't set the initial value to zero in the macro since as a result it would be reset each time the macro is executed.

So I'm guessing that I don't understand how to configure this particular function.

Further assistance would be much appreciated .

Thanks.

Correct. If Count has no value, then the expression “Count + 1” is invalid.

So you need to initially set Count to a value, eg 1 or 0, either at the start of your macro, or before running your macro.

@JMichaelTX’s macro had this as the first action:

which he used to initialise is counter (called Local__Counter).

Thanks for your response.

Just for my own clarification.

Anytime I use a counter like this I need to run a separate macro to set the value.
Once that is done I can use the counter variable in the intended macro.

I'm seeing that was probably the intention in the example macro from @JMichaelTX but it appeared to me that they were all one macro.

Thanks

No.

But you do need to initialize the counter before you increment it.
You can do this within the same macro, as I did in my example.
If you want a counter that increases over multiple executions of the same macro, or over executions of multiple macros, then you need to use a KM Global Variable.

I would usually do something like this to ensure the variable is initialized.
Put this at the very top of your macro(s) -- all macros that use the counter.

image

Then, later in the macro:

image

I use the KM Variable prefix of "DND" for Do Not Delete, to make clear when I'm in the Preferences Variable Panel to NOT delete it.

I also use the two underscores so that the "DND" prefix does NOT show in Prompt for User input.

Make sense now?

1 Like

This macro is amazing! I'd love to know how can I modify this counter. What I'm gonna do is a little complicated, but I'll make it simple to understand so someone can help me.
Let say I'm going to put these numbers in Numbers' cells one by one and each time it presses the return key.

Thanks

Hey BB,

Please provide a real-world example of the kind of text you want to insert.

-Chris


Hey Chris! Thanks for being here trying to help me.
I tried to describe my problem as much as I could, but please let me know if something doesn't make sence.

Hey Chris! Good news! I've just made the macro I needed and it works very well.
I have to thank you because if you didn't message me I wouldn't try to make this myself.
I made it and I'm super happy.
Thank you

2 Likes