Get current line / selection via environment variables

The previous method does not work for me (and multiple other users) and based on an old post¹ to alt.comp.lang.applescript: “osascript closes stdin (file descriptor 0) multiple times (I used ktrace && kdump)”. Though a follow-up post does claim to successfully use osascript with stdin (in the same was as was previously done by this script).

¹ https://groups.google.com/forum/#!topic/alt.comp.lang.applescript/1v0s0Tkn1bM