Hello, I'm happy to report that I got this working after much trawling through the source and documentation. The code below produces a pdf with the MediaBox, CropBox, BleedBox and TrimBox all in the places I specified in my original message. I hope it helps illustrate how to set these PDF boxes using Lua. I would much appreciate any feedback on how the Lua code can be cleaned up or made more flexible (the bleed is hard-coded in because I didn't know how to obtain the user-specified bleedoffset, for example), or made better in any other way.

Jack

\setuppapersize[A5][A4]

\setuplayout[location=middle, marking=on]

\setupinteraction
  [title={TestDoc},
   author={Anon}]

\setupbackend
  [format=PDF/X-1a:2003,
   intent={ISO Coated v2 300\letterpercent\space (ECI)}]

\setupinteractionscreen[width=max,height=max]

\startluacode
moduledata.mystuff={}
respecify_pdf_boxes = function()
    local pdfverbose = lpdf.verbose
    local factor = number.dimenfactors.bp
    local f_value = string.formatters["\letterpercent.6N"]

    local function boxvalue(n)
        return pdfverbose(f_value(factor * n))
    end

    local paperwidth = tex.dimen.printpaperwidth
    local paperheight = tex.dimen.printpaperheight
    local pagewidth = tex.dimen.paperwidth
    local pageheight = tex.dimen.paperheight
    local bleedoffset = 3 / number.dimenfactors.mm
    local pdfarray = lpdf.array

    lpdf.addtopageattributes("MediaBox", pdfarray {
        boxvalue(0),
        boxvalue(0),
        boxvalue(paperwidth),
        boxvalue(paperheight),})

    lpdf.addtopageattributes("CropBox", pdfarray {
        boxvalue(0),
        boxvalue(0),
        boxvalue(paperwidth),
        boxvalue(paperheight),})

    lpdf.addtopageattributes("TrimBox", pdfarray {
        boxvalue((paperwidth - pagewidth) / 2),
        boxvalue((paperheight - pageheight) / 2),
        boxvalue((paperwidth + pagewidth) / 2),
        boxvalue((paperheight + pageheight) / 2),})

    lpdf.addtopageattributes("BleedBox", pdfarray {
        boxvalue((paperwidth - pagewidth) / 2 - bleedoffset),
        boxvalue((paperheight - pageheight) / 2 - bleedoffset),
        boxvalue((paperwidth + pagewidth) / 2 + bleedoffset),
        boxvalue((paperheight + pageheight) / 2 + bleedoffset),})
end

moduledata.mystuff.respecify_pdf_boxes = respecify_pdf_boxes

lpdf.registerpagefinalizer(moduledata.mystuff.respecify_pdf_boxes, "respecify pdf boxes")
\stopluacode

\starttext
test
\stoptext

On Sun, 26 Apr 2020 at 02:42, Jack Steyn <steynjack@gmail.com> wrote:
After much searching, my probably laughable attempt consists of combining the answer to the question at https://tex.stackexchange.com/questions/433110/setting-page-attributes-of-every-page-in-a-generated-context-file with what I can see in the source at https://source.contextgarden.net/tex/context/base/mkiv/lpdf-mis.lua:

\appendtoks
  \startluacode
      local formatters = string.formatters

      local pdfverbose = lpdf.verbose
      local pdfarray = lpdf.array

      local factor  = number.dimenfactors.bp
      local f_value = formatters["\letterpercent.6N"]

      local function boxvalue(n)
          return pdfverbose(f_value(factor * n))
      end

      lpdf.addtopageattributes("TrimBox", pdfarray {
          boxvalue(30),
          boxvalue(30),
          boxvalue(180),
          boxvalue(267),})
  \stopluacode
\to \aftereverypage

This code throws no errors, but unfortunately it also has no effect. What am I doing wrong?

Jack

On Sat, 25 Apr 2020 at 22:37, Jack Steyn <steynjack@gmail.com> wrote:
Hi,

I am having trouble setting the PDF boxes to my desired dimensions. Suppose I have \setuppapersize[a][b]. I want CropBox = MediaBox = b. So far, so good: I can just use cropoffset=0mm in \setuplayout. But I want TrimBox = a. However, as far as I can see, I can't achieve this using trimoffset in \setuplayout unless (width of b) – (width of a) = (height of b) – (height of a), which does not hold in my case. So it looks like I need to find another way to set the TrimBox (and the BleedBox, which I want to be 3mm wider and taller than the TrimBox). Does anyone know how to do this?

Best,

Jack