Creating a Loop for AppleScript

Hi there,

I am new to Keyboard Maestro and I need some help with it. I tried to figure it out with ChatGPT but didn't succeed. After a full day of trying, I am now asking for your help.

I am trying to create a macro that, with AppleScript code, takes the result from Excel and pastes it into the needed text box. The problem is that the script can only retrieve one answer or all of them at once. I need to create a loop in the script that will change the value. The starting value is B2 and needs to go up to B12 by adding +1 each time.

I am attaching an example of what I tried to create, but it didn't work out. Maybe someone can tell me how to make this code run in a loop and change the %Variable%MyRange% value every loop.

Thank you for everyone's help!

Produces little better than hallucinatory output for AppleScript – probably hasn't seen enough of it to form more than a caricatural statistical impression.

You want to retrieve all of them at once. Each time you "Execute AppleScript" there's a resource overhead, so do it once then separate the values in KM if you need to. Easiest way to do that is to return a line-delimited list from your AppleScript then use that in a "For Each... line in a variable" action.

So, assuming you've got the path right in your screenshot and that this is a one-sheet workbook:

Values from Excel.kmmacros (4.0 KB)


Don't worry so much about the AppleScript (although I've based it on yours to make it as familiar to you as possible), but do take a look at the way the "For Each" action is structured and see how it differs from yours -- it's a very useful action and it will help you to understand how to set the options and use the output. In this case you can replace the "Display" action with actions that go to the correct field and then paste the value.

Please don't. It's wildly over-hyped and really only useful if you already know enough about the subject to be able to asses and correct its output. At least with a Google search you get to to see the context around, and often a discussion about, the answers you get.

If you want help with Keyboard Maestro, start with the manual and the Wiki, then ask here. Try deconstructing a few of the macros posted to see how they work and how actions, variables, tokens, functions, and so on work together. It's always fun to jump into something complicated using scripts and other "extras", but nailing the basics will help you more in the long run!

Thanks for giving help and some hints to complete my wanted macro, thank you!