Mac Numbers app : KM macro or AppleScript to create a link to an image in the image gallery

Hello,

After years of using the Mac Numbers app, I am now realizing how useful the image gallery is.
In any Numbers table, it is possible to create an image gallery which is a collection of images as shown below. Note the right and left arrows which allow the user to navigate images.

It's extremely useful because I can have a technical note in a cell and put a supporting annotated image in the image gallery.

My problem is how to link the cell and the image. If I read the content of a cell, how do I quickly view the associated image among perhaps one hundred images in the image gallery.

It's possible to create multiple image galleries, but this creates even more complex problems to manage.

Each image has a caption and a unique file name

thanks in advance for your time and help

I'm aware of the image gallery feature, but how are you associating a given image from the gallery with a cell's content? I thought the image gallery was a standalone thing, not something where you could say "assign image six from this gallery to cell A14?"

-rob.

1 Like

Thank you for your reply.
I have been working on the issue for a few hours, and these are some ideas.

  • images have captions which I can find (and the image simultaneously displayed) using Edit→ Find
    ie simply ⌘F.
  • I had the idea of always inserting a unique identifier in the caption, for example $SheetKBMCellA6. The caption would be entitled "displaying the status menu $SheetKBMCellA6"
  • I would finally use a Keyboard Maestro macro or apple script (which I don't know how to do) to trigger find and include the unique identifier.
  • ideally the KM macro would run on the selected cell.
    So in the end, the trick would be to
  • obtain the coordinates of the selected cell, example $"sheetname""cell coordinates". I just added $ to make it unique.
  • run ⌘F using the cell coordinates which I will have inserted into the image caption.

Here's a functioning solution—I'm not sure it's ideal, but it seems to work. Galleries are attached to sheets, so we don't need to worry about sheet names at all. First, here's the macro:

Download Macro(s): Get Image from Gallery.kmmacros (7.7 KB)

Macro screenshot

Macro notes
  • Macros are always disabled when imported into the Keyboard Maestro Editor.
    • The user must ensure the macro is enabled.
    • The user must also ensure the macro's parent macro-group is enabled.
System information
  • macOS 14.4.1
  • Keyboard Maestro v11.0.2

To use, you need to caption the images with this format:

•cell_address•

So •c4• or •AA153• or whatever. If you don't want to use the '•' symbol, you can change it to anything you like. You just have to change the regex search/replace function in the macro:

image

Change the '•' to whatever you like—and you could use just one in front, none in back, more than one, etc. Whatever you put here, though, is what you need to use in your formatted captions.

Once you have your captions set up, select a sell in Numbers and press Shift-Control-I (or whatever you changed the activation keys to). This should run a find and select the proper image in the gallery.

-rob.

2 Likes

absolutely magnificent. Superb. Thank you so much ! Works perfectly.

Glad it works for you!

-rob.

1 Like

I ran into a problem when I started using your excellent macro. I discovered that with Numbers, Find as in ⌘F searches captions across all sheets. There are therefore as many A14 cells as there are sheets.

I found the variable for the name of current sheet, I tried the following in AppleScript

set sheet_name to name

and it works. I tried to modify the script so that I end up with a composite such as sheetname:cell, like
"covid vaccination":A14 but I am a real bungler and was unable to do so.

Would it be complicated to modify the script accordingly ?

thanks again very much

Ironically, I had originally written it to use the workbook name too, but decided it was irrelevant. So, no, not tricky to modify at all:

Download Macro(s): Get Image from Gallery.kmmacros (7.8 KB)

Macro screenshot

Macro notes
  • Macros are always disabled when imported into the Keyboard Maestro Editor.
    • The user must ensure the macro is enabled.
    • The user must also ensure the macro's parent macro-group is enabled.
System information
  • macOS 14.4.1
  • Keyboard Maestro v11.0.2

In this version, the format for the captions is simply:

[workbook name:cell address]

Don't use the extension on the filename, so you just need captions like this:

[Cost of living:D3] or [Housing costs:F22]

Again, if you don't like that format, you can change it to whatever you like. If you do, though, change the "and replace with" in the regex search/replace to match your chosen format:

Make sure $1 (which is the workbook name) and $2 (cell address) are still both in there, but change anything else you like. Just make sure your actual caption names then match the new format.

-rob.

I think that there is a misunderstanding. It's the sheet name, not the document name which I would like to specify.

the end result should look like [sheet name:cell address]

I tried to modify the script myself, but failed.

Here is what I tried
The variable name for the sheet name appears to simply be "name" according to this site
AppleScript and Numbers: Sheets

I modified the script as following

tell application "Numbers"
	set frontDoc to front document
	set sheetName to name
	try
		tell table 1 of active sheet of frontDoc
			set currentcell to the first cell of the selection range
		end tell
	on error
		set currentcell to "No cell selected"
	end try
end tell

return {sheetName, currentcell}

instead of

tell application "Numbers"
	set frontDoc to front document
	set workbookName to name of frontDoc
	try
		tell table 1 of active sheet of frontDoc
			set currentcell to the first cell of the selection range
		end tell
	on error
		set currentcell to "No cell selected"
	end try
end tell

return {workbookName, currentcell}

The outcome before the regex ie

Numbers, cell C19 of table 1 of sheet 5 of document id 16D2F204-0AA9-4AC3-B4D0-E7ACB91967E1

Note an additional problem. Instead of giving the actual name of the sheet, I only get the number of the sheet starting with 1 on the left which is not practical.

thanks very much for your patience and efforts.

You aren't telling Numbers what you want the name of, so it gives you the name of whatever the tell block is aimed at -- Numbers itself. Compare that to the line you replaced:

      set workbookName to name of frontDoc

Since you probably want the name of the active sheet, since that's what you are getting the cell from, try:

      set sheetName to name of active sheet

Sorry for the confusion; this version returns the sheet name, and should work as you expect if you caption images [Sheet name:cell address].

Download Macro(s): Get Image from Gallery [Numbers].kmmacros (9.3 KB)

Macro screenshot

Macro notes
  • Macros are always disabled when imported into the Keyboard Maestro Editor.
    • The user must ensure the macro is enabled.
    • The user must also ensure the macro's parent macro-group is enabled.
System information
  • macOS 14.4.1
  • Keyboard Maestro v11.0.2

-rob.

1 Like

works perfectly. I understand the importance of the regex to create the final product. Thanks VERY much !