I very much agree AppleScript can be very difficult that way.
For example you would think you could just change "first" to "every" and it would work fine from reading it (or "all items").
set theMacro to first item of (get selectedMacros)
set theMacro to every item of (get selectedMacros)
For what it is worth if you wanted to change every selected macro you can do it like this.
(Add Text at End for All Selected Macros)
tell application "Keyboard Maestro"
set selectedMacrosList to get selectedMacros
repeat with theMacro in selectedMacrosList
set macroName to name of macro id theMacro
if macroName does not end with "(available via Raycast)" then
set name of macro id theMacro to (macroName & " (available via Raycast)")
end if
end repeat
end tell
If you wanted to toggle that from every selected macro you could do it like this.
(Toggle Text at End for All Selected Macros)
tell application "Keyboard Maestro"
set selectedMacrosList to get selectedMacros
repeat with theMacro in selectedMacrosList
set macroName to name of macro id theMacro
set searchText to " (available via Raycast)"
if macroName ends with searchText then
-- Remove the text if it is present
set name of macro id theMacro to text 1 thru ((length of macroName) - (length of searchText)) of macroName
else
-- Add the text if it is not present
set name of macro id theMacro to (macroName & searchText)
end if
end repeat
end tell
And to round it out if you wanted the text at the begnining of the macro names.
(Toggle Text at Start for All Selected Macros)
tell application "Keyboard Maestro"
set selectedMacrosList to get selectedMacros
set searchText to "(available via Raycast) " -- Now at the beginning
repeat with theMacro in selectedMacrosList
set macroName to name of macro id theMacro
if macroName starts with searchText then
-- Remove the text if it is already at the beginning
set name of macro id theMacro to text ((length of searchText) + 1) thru -1 of macroName
else
-- Add the text at the beginning if it’s not already there
set name of macro id theMacro to (searchText & macroName)
end if
end repeat
end tell
A of couple macros that you can use your own text via a user prompt to the start and end several macros.
(With User Prompt)
**AAA - Automation - Keyboard Maestro Macros (v11.0.3)
**AAA - Automation - Keyboard Maestro Macros.kmmacros (62 KB)
And here you can replace any text you want in the selected macros.
(Search and Replace)

Text Replacement - Replace Text for All Selected Macros with Prompt Macro (v11.0.3)
Text Replacement - Replace Text for All Selected Macros with Prompt.kmmacros (32 KB)
Okay you have me on a roll, I thought I was done after the first two macros. It got me thinking I often sort my macros with the hidden numbers. This macro will add numbers to the start of the selected macros in Keyboard Maestro editor so if you don't want them to show up alphabetically in the conflict pallet you can sort by some other methode like date created, date modified, or date used to then quickly add numbers for them to show up in that order in the conflict pallet (I absolutely love Keyboard Maestro's conflict pallet so much better than how you had to set that up back in the day in QuicKeys, thank you Peter!)
(Add Sequential Numbering at the Start of Each Macro in Keyboard Maestro Current Sort Order)

Text Replacement - Add Sequential Numbering at the Start of Each Macro in Keyboard Maestro Current Sort Order.kmmacros (30 KB)
This one was a bit more of a bear but what I prefer and do quite often for the conflict pallet. I like to use the numbers rather than the letters if I can to quickly get to a macro on the conflict pallet. This macro makes that pretty quick to make. Also note that there is logic in the numbering if there is less than 11 selected.
(Add Numbers to Name 0 for 10th if Only 10)

Text Replacement - Add Sequential Numbering at the Start of Each Macro in Keyboard Maestro Current Sort Order and Add Numbers in Conflict Pallet Display Name (If Just 10 the 10th is "0").kmmacros (32 KB)
If you don't like the 10th one being a zero if there is only 10 then here you go it will always add them. Also note that there is logic in the numbering if there is less than 10 selected.
(Add a Number in the Name and Start 10 and Not 0 For 10th)

Text Replacement - Add Sequential Numbering at the Start of Each Macro in Keyboard Maestro Current Sort Order and Add Numbers in Conflict Pallet Display Name.kmmacros (32 KB)
This macro allows you to remove numbers at the start of selected macros if you want to start over and renumber them in a different order. Use it in conjunction with "Add Sequential Numbering at the start of Each Macro"
Remove Sequential Numbering at the Start of Each Macro in Keyboard Maestro Macro
02)Text Replacement - (2) Remove Sequential Numbering at the Start of Each Macro in Keyboard Maestro.kmmacros (30 KB)
If you have new macros you have made like this one and want to quickly insert another action in the macro pallet quickly this is a great way to do it. Maybe when I am done I will compile all of these into one download.
Increment or Decrement All Numbers in Selected Macros By One

**AAA - Automation - Keyboard Maestro Macros.kmmacros (63 KB)
If you want to keep your numbers safe if you have a sequential list and just add text to the start.
Toggle Text at Start for All Selected Macros with Prompt (Sequential Number Safe)

12)Text Replacement - (12) Toggle Text at Start for All Selected Macros with Prompt (Sequential Number Safe).kmmacros (32 KB)
Thanks for your AppleScript post @Nige_S