dfay Posted January 27, 2015 Share Posted January 27, 2015 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
rice.shawn Posted January 27, 2015 Share Posted January 27, 2015 You could try something like: tell application "System Preferences" to quit Link to comment
dfay Posted January 27, 2015 Author Share Posted January 27, 2015 Yeah....but I was hoping to do it without needing to name the app. The first script works on the frontmost app regardless of what it is. Creating a quit script for every app I might run is a little impractical Link to comment
rice.shawn Posted January 27, 2015 Share Posted January 27, 2015 Well, then just combine them: tell application "System Events" set theName to name of the first application process whose frontmost is true end tell tell application theName to quit Link to comment
dfay Posted January 27, 2015 Author Share Posted January 27, 2015 (edited) 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 January 27, 2015 by dfay Link to comment
rice.shawn Posted January 27, 2015 Share Posted January 27, 2015 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
mjkhawtin Posted January 28, 2015 Share Posted January 28, 2015 Hi there. I'm pretty new to this and had the exact same thought as soon as I purchased Alfred remote. However following your suggestions the 'Run Script' options is greyed out. Do I need to purchase power pack for this feature? Many thanks Link to comment
rice.shawn Posted January 28, 2015 Share Posted January 28, 2015 Pretty sure that you do, but, honestly, it's worth it. If you purchase the PowerPack, then you have access to Workflows in general (see http://www.packal.org for a selection). Link to comment
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now