Thank you lethosor, I have a feeling that we are getting close, though we are not there yet. With your changes I could get farther, then the game froze for about a minute, and got this error:
...8.5/Macnewbie/Dwarf Fortress/hack/scripts/emigration.lua:23: stack overflow
stack traceback:
...8.5/Macnewbie/Dwarf Fortress/hack/scripts/emigration.lua:23: in function 'desert'
...8.5/Macnewbie/Dwarf Fortress/hack/scripts/emigration.lua:52: in function 'desert'
...8.5/Macnewbie/Dwarf Fortress/hack/scripts/emigration.lua:52: in function 'desert'
...8.5/Macnewbie/Dwarf Fortress/hack/scripts/emigration.lua:52: in function 'desert'
...8.5/Macnewbie/Dwarf Fortress/hack/scripts/emigration.lua:52: in function 'desert'
...8.5/Macnewbie/Dwarf Fortress/hack/scripts/emigration.lua:52: in function 'desert'
...8.5/Macnewbie/Dwarf Fortress/hack/scripts/emigration.lua:52: in function 'desert'
...8.5/Macnewbie/Dwarf Fortress/hack/scripts/emigration.lua:52: in function 'desert'
...8.5/Macnewbie/Dwarf Fortress/hack/scripts/emigration.lua:52: in function 'desert'
...8.5/Macnewbie/Dwarf Fortress/hack/scripts/emigration.lua:52: in function 'desert'
...
...8.5/Macnewbie/Dwarf Fortress/hack/scripts/emigration.lua:52: in function 'desert'
...8.5/Macnewbie/Dwarf Fortress/hack/scripts/emigration.lua:52: in function 'desert'
...8.5/Macnewbie/Dwarf Fortress/hack/scripts/emigration.lua:52: in function 'desert'
...8.5/Macnewbie/Dwarf Fortress/hack/scripts/emigration.lua:52: in function 'desert'
...8.5/Macnewbie/Dwarf Fortress/hack/scripts/emigration.lua:52: in function 'desert'
...8.5/Macnewbie/Dwarf Fortress/hack/scripts/emigration.lua:52: in function 'desert'
...8.5/Macnewbie/Dwarf Fortress/hack/scripts/emigration.lua:52: in function 'desert'
...8.5/Macnewbie/Dwarf Fortress/hack/scripts/emigration.lua:52: in function 'desert'
...8.5/Macnewbie/Dwarf Fortress/hack/scripts/emigration.lua:52: in function 'desert'
...8.5/Macnewbie/Dwarf Fortress/hack/scripts/emigration.lua:207: in function 'checkForDeserters'
...8.5/Macnewbie/Dwarf Fortress/hack/scripts/emigration.lua:254: in function <...8.5/Macnewbie/Dwarf Fortress/hack/scripts/emigration.lua:220>
In this test fort I have 21 unhappy/miserable/tantruming dwarves, so I suppose the 21 errors in line 52 correspond to those, and the game stalled when the script tried to calculate their desire to emigrate. Maybe just one more fix?

EDIT: It seem to be working somewhat, some of my dwarves do join the merchants, but I keep getting this message in the terminal. So let's say it's working 10% as intended... Tested on mac, both r3 and r4.