AppleScript and KM Variables - And Looping Actions

Hi all - I have been using KM for a while now. I usually am able to find answers to my challenges via the forums - this community is great for helping others out. However, I have a task I have been trying to solve and just can’t come up with an automation which works. I am mainly struggling with the concept of transferring AppleScript Variables to KM Variables and then Looping certain aspects of the automation. Any help would be appreciated.

Software:

  • Latest update of Mac;
  • Keyboard Maestro Version 8.2
  • Gravit Designer (unfortunately I can know longer use Adobe products - so this is a free graphics app)

Challenges:

  • Gravit Designer has “ZERO” AppleScript support.
  • I am rebuilding a map from 16,129 individual tiles. Each tile is 256 x 256 pixel. So that basically means there are 127 rows and 127 columns to complete the map.
  • User Ignorance should probably have been the first challenge listed :smile:

Automation Prep:

I created a spreadsheet, with two columns - basically the first column is the “X Axis” and the second column is the “Y Axis”.

xy-axis

Then I created a “workspace” using Grid Snapping - each grid is 256 x 256 pixels.

map_example

I will manually drag 127 tiles onto the workspace. The tiles appear to be a single entity, but it is all 127 - with the top tile being the topmost tile. Each time I complete the below workflow, I will drag the next 127 columns to the exact same position in the workspace - This allows me to later in the workflow use the “Activate Application” and then the “Move and Click” actions without any alterations necessary.

Workflow (concept):

The first Action in KM would be an “Execute AppleScript”.

AppleScript would activate my Numbers spreadsheet and save the value of cell A1 to a variable (firstCol) and then it would save the value of B1 to a variable (secondCol).

My assumption is that I then must take both AppleScript Variables and make them KM Variables - this is one of the places I just can’t wrap my head around.

The next Action in KM would be to “Activate GravitDesigner”.

Followed by another Action, the “Move and Click” action - this action would activate the topmost tile on the stack I manually placed.

mouse-click

And finally, there would be two additional “Move and Click” actions. The first would highlight the “X Axis Position” and paste the firstCol variable - followed by the next “Move and Click” action highlighting the “Y Axis Position” and pasting the secondCol variable.

map-position

Once those actions are done, the topmost tile would now be in the correct position.

I would then want to “Loop” the automation to go to the next row on the spreadsheet - reset the two variables - and place the next tile. This loop would do all 127 tiles for a single column.

I would then manually drag the next column to the exact same position on the workspace and run the KM Automation again. Not pictured, but I have columns C and D of my Numbers spreadsheet with the new positions and all I would do is delete the previously used column A and B - in turn moving columns C and D to the A and B position - this way nothing would need to be changed in the workflow.

I hope the above makes sense and I explained it properly - if not, please let me know if you need further details. Also, if there are easier options for me to consider - please don’t hesitate to let me know.

“TMI Zone”: Just in case anyone is wondering … I am a Disabled Vet, had my “bell rung” one to many times and it has caused some lasting cognitive deficits. A therapist has encouraged me to do two things: use my “techy” gadgets to help with reminders - hence why I love AppleScripting and Keyboard Maestro - my only issue is once I learn something it doesn’t always stick these days.

The other thing she recommended was “Game Therapy” - never knew it was a real thing - but, computer games can help by “exercising” the damaged areas of my brain, as well as hand eye coordination. So the above project has me using automation to make gaming a bit easier for me. The game is called Black Desert Online (BDO) and I wanted a map on my computer which I can use for different tasks. Again, probably TMI - but I just wanted to put some context on why I am working on this project.

Thanks in advance for any help!

Easy. See Using AppleScript to Get and Set Keyboard Maestro Variables.

While Apple Numbers does support AppleScript, it can be a real challenge.
If you can use Excel instead, the AppleScript support is much, much better.

I don't have GravitDesigner, so I can only make general comments.
If GD is well designed, you may be able to use UI scripting to identify, get, and set specific fields and specific grid elements. You can try using UI Browser to identify the window elements for use by AppleScript.

You have a complex workflow, and I know I haven't addressed all steps in it, but may this will help.
Please feel free to ask any follow-up questions.

If you are determined to solve the problem through a Keyboard Maestro macro because you want the cognitive exercise of doing so, then follow @JMichaelTX's advice.

If you want to make a single image from a set of tiles relatively quickly, I suggest installing the ImageMagick set of tools and running the montage command from the Terminal.

I am assuming that you have each tile as its own file. If that's the case, and all 16,129 files are in the same folder, you could generate the map in one step with a Terminal command like this:

montage *.jpg -geometry +0+0 -tile 127x127 map.jpg

There are caveats to this:

  1. You have to be willing to install command-line software and run it from the Terminal.
  2. As I said, you have to have all the tiles as individual files. (I'm assuming they're JPEGs, but PNGs or GIFs would work, too.)
  3. The individual tile files have to be named in a way that reflects the order they will take in the map. If they aren't, the folks here could help you get them renamed.

Having done my share of user simulation scripting in both Keyboard Maestro and AppleScript, I think your chances of success are better with montage.

2 Likes

I'd suggest create a free Figma.com account.

It automatically creates a row from images you drag and drop:

Thanks for all the suggestions - I am presently testing each option out.

@JMichaelTX - LOL, I say this with all the best intentions - "Easy" - I have learned that what used to be easy for me (was a Digital Marketing Executive) - has recently moved that "easy" bar and unfortunately the bar has become much lower. I will take another stab at it later on today - and maybe at the very least I can get close - and follow it up with some additional questions. So thanks again,

@drdrang - I downloaded ImageMagick via HomeBrew. I dropped in the terminal code you provided and after about 3 minutes it spit out a jpg image. However, while it was 127 tiles wide, it only did 3 rows - and the images were in no logical order.

I have tried to search the help docs - but, being the first time using the software, I am struggling on which aspect of the code I need to focus on.

To your 3rd caveat: The .jpgs are labeled in what I thought was a logical order, but maybe you can suggest something different.

The naming structure I used was (column)_(row).jpg - so the first image is 0_0.jpg, the ones below follow the same structure 0_1.jpg, 0_2.jpg, etc. Then the next column starts with 1_0.jpg, the third column uses 2_0.jpg, etc.

So if you can point me to the user manual were it explains why it only did a 3x127 instead of the 127x127 - I will try to dig deeper. And if you can give feedback on the name structure - I have a macro to quickly rename the tiles.

@hello - I tried Figma. The issue I ran into almost immediately is that when I drag the images onto the workspace - it puts them in any order - which means I would have to manually drag them into the correct order. I did go to the user guide - but I couldn't find anything which discussed how to import images - specifically importing images in a specific order. It's a pretty cool little program - just need to figure out the best way to get 16k + images loaded onto layers in the correct order. Any thoughts?

Again thanks to each of you guys for your initial feedback.

-Scott

montage takes the input files and arranges them row-by-row. So if you had 25 images named 01.png through 25.png, and tiled them in a 5×5 grid, they’d end up arranged like this:

01.jpg  02.jpg  03.jpg  04.jpg  05.jpg  
06.jpg  07.jpg  08.jpg  09.jpg  10.jpg  
11.jpg  12.jpg  13.jpg  14.jpg  15.jpg  
16.jpg  17.jpg  18.jpg  19.jpg  20.jpg  
21.jpg  22.jpg  23.jpg  24.jpg  25.jpg

With your type of naming, two things would have to be changed:

  1. The row and column parts of the names have to be switched to (row)_(col).jpg.
  2. The rows and columns have to have leading zeros so their alphabetical sort order (which is what *.jpg returns) is the same as the numerical sort order. For example, in an alphabetical sort, 10.jpg comes before 2.jpg because the character “1” comes before the character “2.”

Since you have 127 rows and 127 columns, you need to make sure your row numbers and column numbers have three digits, with as many leading zeros as needed.

Right now, I have no idea why you got only three rows of output. It’s possible montage has some memory limit, but I’ve never heard of any such problem before.

I think I can work out a quick renaming solution, but before I get started, am I correct that your file names look like this?

0_0.jpg
0_1.jpg
.
.
.
0_126.jpg
1_0.jpg
1_1.jpg
.
.
.
1_126.jpg
2_0.jpg
.
.
.
126_0.jpg
126_1.jpg
.
.
.
126_126.jpg

(Just reread your post; looks like maybe you don't need any renaming help.)

Yep, "easy" is a relative and somewhat dangerous term.
By "easy", I meant that once you know how, it is easy to implement:

I hope you will find this easy to use:

tell application "Keyboard Maestro Engine"
    set asVarName to getvariable <KM Variable Name>
    setvariable <KM Variable Name> to <New Value>
end tell

the terms in angle brackets can be either literal text in double-quotes, or an AppleScript Variable that contains the text.

damn,
thought it'd place them in the same order from Finder...

If the 'montage' route worked, try to do it in smaller batches,
ie the 3 rows you already did, do the same for the next 3 rows, and so on.
And just merge the output.jpg files together in the design app you mentioned, much faster.