Command Tab AppleScript

Hey All,


I would like an applescript that would get the command tab (application switcher) up and hold it there until the mouse was clicked. Currently i have the below script but it ends at a set amount of time not a click. I would be very pleased if some one could help me out.


tell application "System Events"

key down command

keystroke tab

key code 123

delay 2

key up command

end tell


Thanks

Mac OS X (10.7.2)

Posted on Jan 27, 2012 6:43 PM

Reply
15 replies

Sep 7, 2017 1:22 PM in response to Pierre L.

YES! Why does this work? Does the "exists" command trigger creation of "list 2 of process 'Dock'", but only upon its first call?


How can I learn more about "list 2 of process 'Dock'"? It's pretty mysterious to me, but it seems like a very useful listener to piggyback on for scripts. Do standard mouse configuration tools piggyback on this process? How can I see all the "lists" of a process?


Before your response, I tried "list 1" and "list 3", and the results were so bad that I had to restart my computer or delete my com.apple.dock .plist file(s).


Can you please help me understand this a bit more?


Thank you so much.

Sep 7, 2017 1:39 PM in response to Pierre L.

Upon further exploration of the issues I was facing, I noticed that if this script is executed even one time WITHOUT having "allowed" it in accessibility options in security preferences, it never terminates. That is quite bad, and required a computer restart for anyone trying to first set up this script (or run it for the first time after compiling it as an application). What is the explanation for that? Is there a way to edit it so that it is a bit "safer" to share around the internet? What resources can I explore to be able to generate a script like this without Googling into the abyss?

Sep 7, 2017 1:49 PM in response to zsiegel92

As I understand it, list 1 refers to the list of apps in the Dock, while list 2 refers to the list of opened apps displayed by Switcher when you press Command-Tab. The first repeat block just makes sure that second list exists before pressing the left arrow key (key code 123). I don’t know why it didn’t seem to be necessary four years ago. The second repeat loop is in order to wait until you click the mouse or tap on the trackpad.

Sep 7, 2017 2:00 PM in response to Pierre L.

Great. I propose the following change, so that this script times out (after 10 seconds) no matter what. That way, even if someone screws up in providing permissions, they won't have an orphaned script anywhere:


set aCounter to 0

tell application "System Events"


key downcommand


keystroketab

repeat until (existslist 2 of process "Dock") and (aCounter < 100)


delay 0.1

set aCounter to aCounter + 1

end repeat


key code 123

repeat while (existslist 2 of process "Dock") and (aCounter < 100)


delay 0.1

set aCounter to aCounter + 1

end repeat


key upcommand

end tell

Sep 7, 2017 2:13 PM in response to Pierre L.

Also, for anyone else who wants this functionality, but wants the process to run in the background (don't show up in dock or application switcher):


save this script as an application, right-click the application icon, select "show package contents", enter "contents", and find the info.plist file. Add to the bottom (before the final "</dict>" tag):


<key>NSUIElement</key>

<string>1</string>


Then it will run in the background!

Feb 17, 2013 8:33 AM in response to Pierre L.

I had the same exact problem and this solution works for me too. One problem though... I save it as an application to be ran with a shortcut from my graphics tablet and the first time I ran it, the applescript application didn't quit itself properly and continued to hold down command. I had to force quit it. Is there a reason that would have happened? Thank you so much for providing the script, other than that issue which maybe was a fluke, it seems to do exactly what I needed.


Oh one more thing.... is there a way to make it so the applescript application doesn't show up in the switcher?

Feb 17, 2013 9:14 AM in response to sterlingfive

Even when saved as an application, the script should work as expected. Of course, don't save it as a stay-open application. Also, be sure to have enabled access for assistive devices in the Accessibility System Preference pane.


On the other hand, it's not necessary to save the script as an application in order to run it with a shortcut, you could use FastScripts (free for up to 10 keyboard shortcuts) for that.

Feb 17, 2013 9:52 AM in response to Pierre L.

I just checked out fastscripts. Seems to work pretty well. I guess it solves the issue of needing to hide the application from the dock and application switcher. I did have one more instance of the command key getting stuck down. It happened after I ran the script several times in short succession. I was just testing it and then command was stuck down. I will keep an eye on it and se if there is any sort of pattern to the problem. Hopefully, it doesn't happen again at all. Or it could be a conflict with something else, just not sure what yet. Anyway, it's working better than my last solution of using a delay in the script and the fastscripts app is really cool too, I'm sure I'll be using it for more of my scripts. Thanks for all the tips, Pierre.

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

Command Tab AppleScript

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.