Search code examples
macosfinderapplescript

Applescript to Expose all Finder Windows?


I'm trying to figure out how to write an Applescript that will Exposé all Finder Windows.

Application Logic

I think the script needs to have this application logic:

  • Check a residual setting and get the name of the last "Frontmost Application" (perhaps use a text file ~/last-application.txt to store this?)
  • Grab the name of the current Frontmost Application
  • If the name of the current of the Frontmost Application is Expose, then activate the previous frontmost application
  • Else, activate finder, and then activate expose for just finder windows

Desired Behavior

  • When the script is activated all the finder windows (and only the finder windows) will be shown in Exposé
  • If the script is then run again (and no finder window was selected) the script will just switch back to the last frontmost application

I'm not sure how to get this working though. If there is another utility that does this automatically that'd be great, too.

enter image description here


Solution

  • set f to "/s/x/finderexpose"
    set prev to do shell script "touch " & f & "; cat " & f
    if prev is not "" then
        delay 0.5 -- time to release modifier keys used in a shortcut
        tell application "System Events" to key code 53 -- esc, if Exposé is open
        delay 0.3 -- for the Exposé animation?
        activate application prev
        do shell script "echo '' > " & f
    else
        do shell script "echo " & quoted form of (path to frontmost application as text) & " > " & f
        activate application "Finder"
        delay 0.05
        tell application "System Events" to key code 125 using {control down} -- ⌃↓
    end if
    

    It'd be less ugly if the part for switching to the previous application was left out:

    activate application "Finder"
    delay 0.05
    tell application "System Events" to key code 125 using {control down}