Get Info on Window, do script, return focus

Want my applescript to start by getting info on the window which is currently in focus, execute the script (involving other applications) and then returning focus back to the original window. Any help would be appreciated. thanks.

Are you talking about a Finder window? What info do you want? If you just want to know the window’s target, is this script the sort of thing you mean?

Best

John M

-- make sure you have an open Finder window before running this script
tell application "Finder"
	activate
	try
		set windowData to get target of Finder window 1
	on error
		display dialog "Finder has no open windows"
		return
	end try
end tell

--do stuff
-- like (for example)
tell application "Mail"
	activate
	make new outgoing message with properties {subject:"Finder front window", content:"The Finder's front window is: " & return & (windowData as string), visible:true}
end tell
--end do stuff	

tell application "Finder"
	activate
	set target of Finder window 1 to windowData
end tell

It could be any application. I have script which I am running using Key Xing and a keyboard command. However i could be in any application when I run it say Word or AIM. I want to have the script run and then go back to the window I was working on so that don’t to reach for the mouse and click the window to bring it back to the front. As it stands now, when I run a script it will keep whatever application it was using at the front. I would like it to return back to the window that was in the front before the script ran.

It’s hard to say what you are asking for, but this may be it:


-- save the current app in variable x
set x to name of (info for (path to frontmost application))

-- Do what you want here.

--return focus to the original app
tell application x
	activate
end tell

That worked perfectly. Thank you so much. It was the “(info for (path to frontmost application))” that I couldn’t get right before. thanks again.