AppleScript to target frontmost application

I want to program my mouse button to show/hide the Finder. I wrote the following AppleScript and bound it to my mouse button:


tell application "System Events"
    --When this script is run,
    --  the frontmost application will be this script itself
    --Get the name of this script as it is running and hide it,
    --  so that the previous frontmost application is in front again
    set theName to name of the first process whose frontmost is true
    set visible of process theName to false

    set theName to name of the first process whose frontmost is true
end tell

if theName is "Finder" then

    tell application "System Events"
        set visible of process "Finder" to false
    end tell


else

    tell application "Finder"
        activate
    end tell

end if

This works, but is rather slow. It takes about 2 seconds to run.
I want it to be faster. The first tell block uses System Events to get the name of the script and hide it. Is there an easier/faster way to get the name of the frontmost application before the script starts? (i.e. the application that was active when the script was activated)

Model: Unibody MacBook
Browser: Safari 530.18
Operating System: Mac OS X (10.5)

Here’s what I use to get the name of the front application. So you can combine it to show/hide the Finder. It’s almost identicle to your code but I check the name of the frontApp against the name of the script before I hide anything. Anyway, it’s really not much different although it doesn’t take 2 seconds as you’re saying. It’s maybe half a second at most.

One reason for your slow run times could be if you have an Intel mac. In Script Editor if you save your code as an Application, this will create a PowerPC-only application – it will launch on an Intel mac under Rosetta. However, if you save it as an Application Bundle instead, this will create a Universal binary which runs natively on Intel macs. I’m not sure if the same applies for scripts versus script-bundles, but if you have an Intel mac just use “bundles” to be certain you’re not emulating any code.

tell application "System Events"
	set frontApp to displayed name of first process whose frontmost is true
	set scriptName to displayed name of (path to me)
	if frontApp is scriptName then
		set visible of process scriptName to false
		set frontApp to displayed name of first process whose frontmost is true
	end if
end tell

if frontApp is "Finder" then
	tell application "System Events" to set visible of process "Finder" to false
else
	tell application "Finder" to activate
end if

Thanks for that info. It works great now! I did indeed save my applescript as an Application and not as an Application Bundle. How are you supposed to know that choosing Application makes it PPC-only? That is so stupid.

I agree that’s not something you would think of. The only way I know is if you save it as an application, and then save another version as an application-bundle… then get info on them in the Finder… you’ll see that one kind is “application” and one is “application (universal binary)”. Intel macs need universal binary in order to avoid rosetta. When Intel macs first came out there were lots of talk about needing universal binary so one time I looked and saw this difference between the applescript applications.

Anyway, I’m glad it helped. :slight_smile: