New file in insertion location

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/$@"

Hi juanfal,

A quick look sees that the posix path you use is for a window. But, posix path is only for files and folders. A reference to a window does not use posix path.

gl,
kel

Hello Juanfal.

I like your way of getting the focused window, since getting the path of the insertion location is non destructive.

There is just IMHO one more thing to test for, and that is if the insertion location resides in the current space: This also implies that there may be one insertion location for every space, but I haven’t tested that part out.

I’d really try to see if the target of the front finder window is the same as the container of the insertion location, to be sure if the insertion location resides in the current space, if you have more than one space open of course.