Activate last app that was frontmost

Hi all
I am writing an AppleScript that changes settings in System Preferences using UI elements.
As I often need to switch back and forth between settings, I don’t want to quit System Preferences.
Just hiding System Preferences brings me back to the application I had been using, but it’s not really active.
I have to switch to any other application and then back to the one I am actually using for it to become truly active.
This is what I have so far:

set lastApp to (frontmost application) -- this  is  the  tricky  part for me

tell application "System Preferences"
	activate
end tell

tell application "System Events"
	if UI elements enabled then
		-- do whatever
		set visible of process "System Preferences" to false
	end if
end tell

tell application lastApp
	activate
end tell

Even if I set “lastApp” to a running application, that app doesn’t become truly active. I could live with the System Preferences Window
just being left there, but having my last app come to the front.

How do I get the first line to work so that the last tell block works?

–Luke

When you’re in any application you can get to the previous one using the keystoke command-tab, so you can run these keystokes in applescript as follows:


-- this gets the previous frontmost application
tell application "System Events"
	try
		key down command
		keystroke tab
		key up command
	on error
		key up {control, shift, option, command}
	end try
end tell

So in your case I would inocrporate it like this…


tell application "System Preferences"
	activate
end tell

tell application "System Events"
	if UI elements enabled then
		-- do whatever
		-- set visible of process "System Preferences" to false  <-- you don't need this line
		
		-- switch to previous application
		try
			key down command
			keystroke tab
			key up command
		on error
			key up {control, shift, option, command}
		end try
	end if
end tell

Thanks, that works.
Unfortunately it works as well as when “hiding” System Preferences.
I do find myself in the previous app again, but the title bar of the windows are dimmed. I have to switch to another app first and then back again.
Now that I know how to send the ‘command-tab’, I Can mimic this behavior…
Unfortunately I still have dimmed windows, no matter how often I change between the apps.

I’m thinking: clicking on a windows title bar might help.
How would I do that?

I’m still hoping to find a way of “capturing” the original applications name in a variable, as this worked when I knew which app to return to.

added: Just tested clicking on titles manually, it doesn’t help. So I’m afraid a scripted version won’t help either.

You need to post more of your code then. The command-tab keystoke brings the previous application to the front on my machine. I think there’s something in your code that is putting processes in the background.

Hi,

Tyr this:


tell application "System Preferences"
	activate
end tell
tell application "Finder"
	set visible of process "System Preferences" to false
end tell
set front_app to (path to frontmost application as string)
tell application front_app to activate

Hi you two helpers,
I’ve tried both of Your scripts without any of my code and get the same result in both cases.
I return to the originating app, Script Editor in this case, but all the windows are inactive. They can be moved around but that’s all. I can’t edit Or click on anything inside the windows.
The only cure I have found so far is what I have described above: Change to another app “by hand” and return to the original app. It doesn’t seam to matter how I change to the other app as long as it isn’t by script.

Strange, it must have something to do with my system as both of You seem to have no problems.

After changing Kel’s idea a little I got something that might help me along:

set front_app to (path to frontmost application as string)
tell application "System Preferences"
	activate
end tell

tell application front_app to activate

Here’s One of the implementations I’ve bee working on:

property paneName : "com.wacom.PenTabletSettingsPrefPane"

set lastApp to (path to frontmost application as string)

tell application "System Preferences"
	activate
	set current pane to pane paneName
end tell

tell application "System Events"
	if UI elements enabled then
		tell process "System Preferences"
			set PenModesDetails to button 1 of group 3 of tab group 1 of window 1
			click PenModesDetails
			
			set ForceProportions to checkbox 1 of group 2 of sheet 1 of window 1
			set OKbutton to button "OK" of sheet 1 of window 1
			click ForceProportions
			click OKbutton
			--set visible to false -- this creates funky behaviour
		end tell
		-- so does the following try block
		(*
		try
			key down command
			keystroke tab
			key up command
		on error
			key up {control, shift, option, command}
		end try
		*)
	end if
end tell

tell application lastApp to activate

-- hide System Preferences from Finder AFTER switching back to original application
tell application "Finder"
   set visible of process "System Preferences" to false
end tell

It works! Thank you all very much! :smiley:
I hope someday I’ll be able to pay-back :slight_smile: