Incremental letters & numbers w/ reset

Brand new to KM, but experienced with Quickeys. I’m trying to set up so I can press a button and have it type “1”, and then either on another button or the same one have each subsequent press type “a”, “b”, “C”, etc. Then, have a second or third button press type “2”, followed by the alphabet, starting over. Does that make sense? Basically I’m trying to make a counter for sets and subsets that’s triggered manually, as though I was counting the number of people walking in a store, and then counting how many bags they were carrying (except I’m counting the bags using letters). I feel like it’s simple enough to do just in KM, but the alphabet thing throws a wrench in the works. In QK, I’ve used variables quite a lot for basic counters, but never managed to do the subset thing.

I don't have a complete solution for you, just an idea that might get you started.

It's easy enough to reuse a KM variable set by a previous run of the macro.
For the numbers, you can just add 1 each time.
How do you handle incrementing the letters?

One approach is to use ASCII character codes, which are numeric.
The character "a" is 97. You can go from there.
I'm not sure if KM has a ASCII code-to-char converter, but if not it is easily enough done in AppleScript.

Good luck.

Hey James,

More than one way to go about this.

Let’s try JM’s general methodology first.

Put this in an Execute AppleScript action, and type or paste the output.

You are responsible for resetting the letterCounter variable.

if not kmVarExists("letterCounter") then setKMVar({varName:"letterCounter", varValue:"96"})
set letterCounter to getKMVarValue("letterCounter")
set letterToType to character id (letterCounter + 1)
setKMVar({varName:"letterCounter", varValue:(letterCounter + 1)})
return letterToType

------------------------------------------------------------
on getKMVarValue(varName)
  tell application "Keyboard Maestro Engine"
    return value of variable varName
  end tell
end getKMVarValue
------------------------------------------------------------
on kmVarExists(varName)
  tell application "Keyboard Maestro Engine"
    set foundVarList to variables whose name is varName
    if length of foundVarList = 0 then
      return false
    else if length of foundVarList = 1 then
      return true
    end if
  end tell
end kmVarExists
------------------------------------------------------------
on setKMVar(varRec) -- Record Format: {varName:"text", varValue:"text"}
  set {varName, varValue} to varRec's {varName, varValue}
  tell application "Keyboard Maestro Engine"
    try
      set value of variable varName to varValue
    on error
      make new variable with properties {name:varName, value:varValue}
    end try
  end tell
end setKMVar
------------------------------------------------------------

Simpler still is something like this:

set alphabetList to {"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y"}
return item 1 of alphabetList

Note that I’m only returning item 1. You’ll need to use a counter similar to the one in the script above.

If you use an AppleScript applet you should be able to increment to a property.

Yada, yada.

-Chris

2 Likes

Works for me! :smile:

The easiest way I know of to "increment" a letter is to use the fact that perl will do that for you:

Execute Shell Script: perl -e '$a = $ENV{KMVAR_Count}; print ++$a'

This will increment 1➤2, a➤b, A➤B, z➤aa, etc.

4 Likes

Awesome! :+1:

EverNoted. :smile:

FWIW the idiom in a JavaScript for Applications (or browser JavaScript) context might be something like:

function succ(c) {
	return String.fromCharCode(
		c.charCodeAt(0) + 1
	);
}

// succ('4') → '5'
// succ('A') → 'B'
// succ('x') → 'y'
 
2 Likes

Wow! Another awesome snippet. :+1:
EverNoted.

This one ended up being the keeper. It’s pretty awesome that that’s just…built into perl. I don’t know much at all about programming, and so far I’m used to solutions like ccstone’s above, where I have to kind of “tell” the computer what an alphabet is before having it use one. Thank you!

1 Like

This one also worked out pretty well - I ended up using peternlewis’ solution because of the simplicity, but thank you!

These are the little nuggets of gold I live for. Even if I had a good idea of the scope of the search I would never think to turn to Perl. Yet another reason to love Perl.

So what's your workflow process filing strategy when using EverNote for something like this @JMichaelTX?

This whole thread is great. Saving for later use. Currently using a For Each with a list of letters. These are better options.

A post was split to a new topic: Using Evernote with Keyboard Maestro

Note that for fuller coverage of Unicode characters, the String.codePoint functions are best in SF9 and SF10 JavaScript (including where embedded as ‘JavaScript for Automation’)

// succCodePoint :: Character -> Character
function succCodePoint(c) {
    return String.fromCodePoint(
        c.codePointAt(0) + 1
    );
}

succCodePoint('🍳') // -> "🍴"

Tersification... :sunglasses:

perl -e 'print ++$ENV{KMVAR_Count}'

-ccs

\#!/usr/bin/env python2
print 'I _think_ I see what you did there.'

Peter had an extra variable assignment that I could dispose of in this instance.

-ccs