Pass output of zenity to variable over X forwarding

I have XQuartz installed on a mac. I’m trying to run a script that brings up the zenity file/directory selection on a remote Centos 7 machine (using X forwarding), so that it captures that output as a variable that I can then use for something else. I have a similar script that does something locally on a different machine; this works by putting the zenity call into a subshell like so: directory=$(zenity --file-selection --directory --title ="Please select folder") - this works fine on the local machine.

When I try to run a similar command from the mac, it’s ssh -X user@server directory=$(zenity --file-selection --directory --title ="Please select folder")

Putting the zenity command into the subshell stops the window from appearing on the machine where the script is run. If I take the zenity command out of the subshell in which I’m turning it into a variable, and just run it as a standalone command, the dialog window pops up in the expected manner, but that means I’m not able to capture the selection as a variable. I’m guessing this is probably to do with the subshell making it invisible to the remote host? Is there a way around this?

The problem was

ssh -X user@server directory=$(zenity --stuff)

But what I needed to do was make the whole thing run inside the subshell:

directory=$(ssh -X user@server zenity --stuff)