How might I run code(for example display dialog("test")) using AppleScript only if the "Finder" Application is currently in focus/active.

  • Um, if the application is already active, what do you aim to accomplish by activating it again?
    – Kent
    Feb 13, 2015 at 12:24
  • stackoverflow.com/questions/3718520/… idle is relevant although doesn't solve the problem.
    – William
    Feb 16, 2015 at 1:58
  • You need to clarify your question. The script in my answer works, for a given definition of 'works' i.e. it reports accurately whether or not Finder is frontmost.
    – Tetsujin
    Feb 16, 2015 at 16:24
  • @Tetsujin updated
    – William
    Feb 16, 2015 at 17:29
  • How are you going to be launching the Applescript app? Double clicking it will, of course, always show the Finder as the '2nd to last' frontmost. Changing answer - this will work if called from Script Editor, but 'fails' if double-clicked in Finder, as it gives a false-positive
    – Tetsujin
    Feb 16, 2015 at 17:55

1 Answer 1


This will work if the script is called from Script Editor, as it 'gets out of the way' to check the next app in line, but will fail if double clicked from Finder, as Finder will then always be last in line.

tell application "System Events"
    set frontmostProcess to first process where it is frontmost
    set visible of frontmostProcess to false
    repeat while (frontmostProcess is frontmost)
        delay 0.2
    end repeat
    set secondFrontmost to name of first process where it is frontmost
    set frontmost of frontmostProcess to true
end tell

tell application (path to frontmost application as text)
    if "Finder" is in secondFrontmost then
        display dialog ("Finder was last in front")
        display dialog (secondFrontmost & " was last in front")
    end if
end tell

Leaving previous answer here for posterity

Rejigged entire answer after having not read the question properly initially ;-)

tell application "System Events"
    set activeApp to name of first application process whose frontmost is true
    if "Finder" is in activeApp then
        display dialog ("test")
        display dialog ("test2")
    end if
end tell
  • +1 Although your proposed solution doesn't seem to work as is. I get "test" as long as the application is open and not necessarily if it is active or not.
    – William
    Feb 13, 2015 at 9:31
  • a rethink… edited answer
    – Tetsujin
    Feb 14, 2015 at 17:52
  • complete rewrite, now I'm back at my desktop - this now does exactly what you asked.
    – Tetsujin
    Feb 15, 2015 at 12:29
  • 2
    you can also replace the second dialog command with display dialog (activeApp) to confirm exactly what the script thinks is the frontmost.
    – Kent
    Feb 16, 2015 at 4:35
  • 1
    Be careful with this -- some apps's app names (e.g., tell application "app_name") differ from their process names (e.g., set frontmost of process "app_process" to true). Jul 24, 2018 at 4:51

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .