close current window via applescript

3mors

HampCake Studios
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
 
The easiest way would be to simulate command-W. You can do this in AppleScript using System Events, like so:
Code:
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")
 
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
 
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:
Code:
	<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.
 
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.
 

Attachments

  • Close window.app.zip
    22 KB · Views: 42
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?
 
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 :(
 
sorry, but i can't find a solution for this stupid simple applescript. who could help me?
 
Back
Top