# close current window via applescript



## 3mors (May 19, 2006)

i know that it's so simple, but i can't get to close the current frontmost window using applescript, i'd like to assign this action to a button of my mighty mouse

who could help me?
thanks


----------



## Mikuro (May 19, 2006)

The easiest way would be to simulate command-W. You can do this in AppleScript using System Events, like so:
	
	



```
tell application "System Events" to keystroke "w" using command down
```
For this to work, you need to enable GUI scripting in AppleScript Utility ("/Applications/AppleScript/AppleScript Utility.app")


----------



## 3mors (May 19, 2006)

i've just tried and it doesn't work, no message error shown

[edit]
mmm strange, it works if i run it from script editor, but not as app standalone
i think it's because it tries to send CMD+W keystroke to the script itself and not to the frontmost window


----------



## Mikuro (May 19, 2006)

Yeah, that would be it. Can't you have it just activate a script, rather than an applet? I'm not all that familiar with Apple's driver.

You could use a third-party driver like USB Overdrive to simulate the keystroke itself.

I'm not sure launching an applet would be responsive enough for such a task, but there is a way to get around the problem you have: Make your applet an "invisible" app that doesn't show up in the Dock or get focus. You can do this by saving this script as an Application Bundle (not as just "Application"), and then editing its Info.plist to include this right after the first <dict> tag:
	
	



```
<key>LSUIElement</key>
	<string>1</string>
```
After saving the changes, move the applet to a different folder, and it should work.

But again, I doubt it would be responsive enough.


----------



## 3mors (May 20, 2006)

sorry, but i can't get it to work


----------



## Mikuro (May 20, 2006)

What OS version are you running? I think it ought to work in Panther or even Jaguar, but I'm not sure I ever tested it before Tiger.

Anyway, try the attached applet. Works for me.


----------



## dmetzcher (May 20, 2006)

3mors said:
			
		

> i know that it's so simple, but i can't get to close the current frontmost window using applescript, i'd like to assign this action to a button of my mighty mouse
> 
> who could help me?
> thanks


Current frontmost window of what? Finder? Another app? Or are you looking for something non-specific?


----------



## 3mors (May 30, 2006)

a non-specified frontmost window: i'd like to assign this script to the middle button of my mighty mouse as i've done with my logitech mouse (using the logitech driver) in order to close the frontmost window of the app i'm using

otherwise i need to use a third-part apps such as usboverdrive, but i'd like to avoid it

the app you posted doesn't work


----------



## 3mors (Jun 4, 2006)

sorry, but i can't find a solution for this stupid simple applescript. who could help me?


----------

