Jump to content

Close a window...and trying to figure out how to quit an app


Recommended Posts

After a few minutes of using Alfred Remote to launch apps on my Mac, I quickly realized I'd also want a way to close windows and/or quit apps.

 

Closing windows proved to be easy:

 

Alfred Preferences > Remote > System Commands (or wherever you want on your remote) > + > Run Script > language: /usr/bin/osascript

 

then paste in the following short Applescript:

tell application "System Events"
	keystroke "w" using {command down}
end tell

I then added this icon as well: http://icons.iconarchive.com/icons/oxygen-icons.org/oxygen/256/Actions-window-close-icon.png

 

Once I had that working, I used the same steps to try to quit the frontmost app instead of closing it.

 

I tried this script:

tell application "System Events"
	keystroke "q" using {command down}
end tell

and this one:

tell application "System Events"
	set theName to name of the first process whose frontmost is true
	tell application theName
		keystroke "q" using {command down}
	end tell
end tell

without success.

 

neither does changing the keystroke in the second block to quit.

 

I realize this is more of an Applescript question....but it raises Alfred Remote script issues that I'll bring up in another thread.

 

 

Link to comment

Brilliant.  I had tried that but I had the second tell command inside the first one.  With the code reordered as you suggest it works perfectly.

 

Interestingly it lets you quit the Finder, rather than restart it.

Edited by dfay
Link to comment

Brilliant.  I had tried that but I had the second tell command inside the first one.  With the code reordered as you suggest it works perfectly.

 

Interestingly it lets you quit the Finder, rather than restart it.

 

That means that you probably want to add in something to make sure that theName isn't "Finder."

 

AppleScript's scopes are always a bit weird. My primary "goto" for debugging AppleScript seems to be moving bits of code in and out of tell application "System Events" code blocks.

 

On another note, I'm glad you found the "keycode" option (in another thread). You could actually build a little keyboard with Alfred Remote using those (although the layout is not optimal at all for this).

Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...