I have been using ‘window 1’ or ‘front window’ from Finder to know what was open on Finder so that I could do something there from any script. But finally I have managed to find what is the focus window, the really active window, not the window 1 nor the front window, but the window that really has the focus which can be the Desktop Folder, or perhaps an open AND focussed window. Then the script:
osascript <<END
tell application "Finder"
try
set thePath to get the POSIX path of (folder of the front window as string)
on error
set thePath to get the POSIX path of (path to desktop folder as string)
end try
end tell
END
has been reduced to:
osascript -e 'tell application "Finder" to get the POSIX path of (insertion location as alias)'
and, for example, this Automator script, able to create a text (SublimeTex) file on the really active window is possible and so simple as:
<Exec AppleScript>
on run {input, parameters}
set fname to do shell script "date '+%Y-%m-%d.%H-%M'.txt"
set theName to the text returned of (display dialog "Name for the new file?" default answer fname)
return theName
end run
<Exec Shell Script>
theFinderPath=`osascript -e 'tell application "Finder" to get the POSIX path of (insertion location as alias)'`
/Users/myname/bin/subl -n "$theFinderPath/$@"