I want to compile an AppleScript as an app for my dock to do the Expose F12 (show all the application windows). The thing is, when I click on the applescript app it’s the frontmost applciation. So, somehow I need to determine what the frontmost application was before I click on the app.
I have a script that does this. It involves hiding and showing apps and is not particularly pretty due to this, but there you go.
--Get name of second to front app
tell application "System Events"
set foundName to "Finder"
set myName to (name of (processes whose frontmost is true)) as text
keystroke "h" using command down
delay 0.1
set oldNames to name of (processes whose visible is true)
keystroke "h" using command down
delay 0.1
set newNames to name of (processes whose visible is true)
repeat with aName in oldNames
if newNames does not contain aName then
set foundName to text of aName
exit repeat
end if
end repeat
set visible of every process to true
end tell
tell application foundName to activate
tell application myName to activate
display dialog foundName
I’m not really sure why you don’t press F9/F10 instead of clicking an app though.
I believe that post Jaguar they fixed the Scipts menu where if you run the script from there, you can get the frontmost process instead of System Events. Note that compiled scripts aren’t applications. If you’re running Jaguar, then I got a slightly complicated way of getting the last frontmost process before the script app process.