Activate background application?

I use this construct at the root level (I don’t know what to call the outermost scope in AS?)

tell application "System Events"
	set frontmostApplicationName to name of 1st process whose frontmost is true
end tell

[activate application B and perform some steps]

tell application frontmostApplicationName
	activate
end tell

and it works as expected, that is, it shows B while some actions are performed and then bring back the application that was frontmost when the script started to the front. However, when I change the scope and put the same code into something like this:

tell application "Safari"
    [do stuff]
	tell document 1 to repeat
        [do stuff]
		tell application "System Events"
			set frontmostApplicationName to name of 1st process whose frontmost is true
		end tell
		activate --activates Safari
		delay 3
		tell application frontmostApplicationName to activate
	end repeat
end tell

and first start the script and then activate some other application C nothing happens, or in fact, it seems it activates Safari despite I have C in front when I encounter these statements. If I log frontmostApplicationName it says C. However, if I don’t use a variable and change the middle section of the code like this:

tell application "Safari"
	tell document 1 to repeat
		tell application "System Events"
			set frontmostApplicationName to name of 1st process whose frontmost is true
		end tell
		activate --activates Safari

        delay 3
        tell application "C" to activate
        delay 3
        tell application frontmostApplicationName to activate

	end repeat
end tell

it first activates C and then Safari (despite frontmostApplicationName in fact says C)

The purpose with this construction is to activate Safari for a few seconds and then bring back whatever application was frontmost before Safari was activated.

Why does tell application frontmostApplicationName to activate not work as expected?

Debugging this kind of thing hurts my brain, but I think I have a working model here.

It’s hard to be sure, but I think it has something to do with the order of your actions, the repeat loop and when you capture the frontmost app.

This reworked snippet seems to work OMM. Maybe you can use it as a guide:

repeat 5 times
	-- grab the current front app
        tell application "System Events"
		set frontApp to name of first application process whose frontmost is true
	end tell

        -- do the Safari thing
	tell application "Safari"
		activate
		-- do your stuff
	end tell

        -- switch back to the previous front app
	tell application frontApp to activate
end repeat

The idea here is that I consciously avoid stacking tell statements. Within the loop I grab the current frontmost app (which is likely to be the script editor app in which case, since I don’t know how you’re triggering the app). It then targets Safari, activates it, and runs whatever you want it to do there.
Then, when its done with Safari, it switches back to the saved app.

HTH

.
You’ve nested the application’s document within its tell block, then the application tell blocks within the document’s tell block, confusing AppleScript interpretator unbelievably.

The activate command does not activate any Safari, because you are sending the command to the document.

Then, you try to activate application frontmostApplicationName again inside the tell block of the document. Application frontmostApplicationName owned by the document does not exist, naturally you will not get anything good.

In general, if you use nested tell blocks (which is already bad), you should at least understand what belongs to what. Just as the head belongs to the person, the document belongs to the application, not vice versa.

The topmost object is the script, it owns the applications, and they own the documents. Therefore, when addressing to some object inside a child object, one must speak with its owner (in this case, with a script). The script is accessed with tell me:
.

tell application "Safari"
	-- [do stuff]
	tell document 1 to repeat
		-- [do stuff]
		tell application "System Events"
			set frontmostApplicationName to name of 1st process whose frontmost is true
		end tell
		tell me to activate application "Safari" -- EDITED
		delay 3
		tell me to tell application frontmostApplicationName to activate -- EDITED
	end repeat
end tell