18

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

William
  • 1,658
  • Um, if the application is already active, what do you aim to accomplish by activating it again? – Kent Feb 13 '15 at 12:24
  • http://stackoverflow.com/questions/3718520/how-do-i-write-applescript-that-will-give-me-an-hourly-popup-alert idle is relevant although doesn't solve the problem. – William Feb 16 '15 at 01: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 '15 at 16:24
  • @Tetsujin updated – William Feb 16 '15 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 '15 at 17:55
  • @Tetsujin That's the issue I'm running in to. – William Feb 16 '15 at 18:02
  • Can I ask why you need to periodically check if the Finder is/was frontmost? It would seem to be a whole lot easier to just fire an Applescript to bring it to the front & quit… or just click on it. – Tetsujin Feb 16 '15 at 18:21
  • @Tetsujin I would like an icon on the dock that does a "PageDown" when clicked on. key code (insert code here) works but you can't know ahead of time what application is focused. – William Feb 16 '15 at 19:32
  • @Tetsujin You have put forth enough effort. I'm convinced what I'm after is possible in AppleScript. – William Feb 16 '15 at 19:41
  • tell application "whichever" to activate \n tell application "System Events" to key code… end tell etc... & ty for the points on the Answer. – Tetsujin Feb 16 '15 at 20:37
  • @Tetsujin That's the point I don't what application it is. I would the PageDown icon to work in any application. – William Feb 16 '15 at 23:28
  • then all you need is to tell application secondFrontmost… Whichever it is, as my Applescript will have backgrounded – Tetsujin Feb 17 '15 at 07:34
  • @Tetsujin There appears to be no way to get the secondfrontmost application. Same issue here http://stackoverflow.com/questions/25411174/applescript-open-frontmost-file-with-another-application – William Feb 17 '15 at 16:04
  • my answer already finds the second frontmost, by virtue of itself being frontmost, then hiding. There is no programmatic way to interrogate the launch services to extract that information. Best you can do is by launch order. If all you need is a Page Down button press, why not look at the other myriad ways that can be achieved? – Tetsujin Feb 17 '15 at 17:33
  • @Tetsujin question accepted http://apple.stackexchange.com/questions/173298/dock-icon-that-activates-pagedown?noredirect=1#comment203832_173298 – William Feb 17 '15 at 17:36

1 Answers1

26

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")
    else
        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")
    else
        display dialog ("test2")
    end if
end tell
Tetsujin
  • 115,663
  • +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 '15 at 09:31
  • a rethink… edited answer – Tetsujin Feb 14 '15 at 17:52
  • complete rewrite, now I'm back at my desktop - this now does exactly what you asked. – Tetsujin Feb 15 '15 at 12:29
  • Hmm.. If I add the applescript as an app to to the dock it only displays "test2" no matter what application I have originally focused. – William Feb 15 '15 at 14:08
  • probably because the Applescript app is frontmost. Test by adding a 3s delay, then clicking the desktop between launch & trigger [that's how I tested it here] – Tetsujin Feb 15 '15 at 15:05
  • 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 '15 at 04: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). – BallpointBen Jul 24 '18 at 04:51
  • Also, be careful if you have different versions of the same app installed (e.g. Firefox)! It may (probably will) launch the wrong one. – SilverWolf Oct 21 '18 at 21:49
  • Still works in 2019 on Mojave – ttt Oct 27 '19 at 16:02