Several weeks ago I wrote about using a personal Slack team for notifications and other personal info. Since then, I’ve continued to experiment with adding channels and connecting services, trying to find the right mix of utility and centralized information gathering for me.
One of the things I really love is using a #clipboard channel, which allows me to instantly synchronize anything between my Mac and iOS devices. Some people use Slackbot for this, and yes, you could also use AirDrop or any of the myriad clipboard syncing apps, but I’ve tried almost all of them, and they all disappoint in some way. Nothing beats Slack for sheer speed and reliability in this arena, believe it or not.
Given that I’m always looking for ways to
waste time streamline workflows for maximum productivity power ups, I was curious to know if I could make the copying of text (my main use case) faster within this process. I asked the following question:
and was met with a chasm of silence. And a few people who wanted me to let them know if I figured out a way to do it.
Slack’s Mac app is essentially a web wrapper for the site itself. Which isn’t a horrible thing, but does make for some less-than-stellar integration points. It’s actually easier to automate this kind of thing on iOS, believe it or not, due to the extension system provided to developers beginning with iOS 8. But I had a little time this weekend and was determined to figure it out, so I decided to get my hands dirty with Automator like any other rational person. I was able to get highlighted input text copied to the clipboard and sent to the right channel on the web easily, but that meant I needed to open a web page every time and wait for it to load… like an animal. So I began thinking about UI scripting and decided to try it with the Mac app using AppleScript.
I will admit this right now: I’ve never really used AppleScript before, and I’ve used Automator only peripherally over the years, so the result of this little experiment is a bit of a dumpster fire. But it actually does what I want it to, which is kind of cool.
First, I needed to understand at least basic AS stuff. I did a little reading and managed to get a handle on it fairly quickly. It’s quite a bit of fun, and I’m actually sorry I waited this long to play with it. Once I felt comfortable, I put together as much as I could in Automator to get the app focused and ready. Then I began assembling the script. In order to really win, once Automator launched the app, it needed to do the following:
- from whatever team I last left the app in, switch to my personal team (Cmd-1)
- activate the quick launch dialogue (Cmd-K)
- enter “cl” to highlight my #clipboard channel
- simulate a press of the return/enter key
- paste the text into the field (Cmd-V)
- simulate another press of the return/enter key
- switch back to the app I was working in (Cmd-Tab)
All of this is totally possible, and not even that hard in AS. I found this awesome app that displays the key codes and other info for keys as you press them, so I was able to keep the script a little bit tidier. The tricky part–the part that drove me nuts and took me the longest to figure out because it wasn’t instantly apparent–was that I needed to build tiny delays into the script to allow the Slack app to catch up to the simulated inputs. I added a few tenths of a second between the steps in the script, and the result was that the app looked like I was just typing super fast, pasting, and tabbing back to the previous app, as opposed to just farting out the failed alert sound your computer makes when you do a thing it doesn’t want you to (which happened a lot as I was tripping through figuring this out). The finished result looks like this (seasoned AS users, avert your gaze; don’t look upon this atrocity):
tell application "System Events"
key code 18 using command down
key code 40 using command down
key code 36
key code 9 using command down
key code 36
Like I said, not the prettiest piece of work. A second smaller AS block just handles the Cmd-Tab after this is done. But it works, and I’m pleased as punch that it does. I mapped a keyboard shortcut to the Services menu entry in System Preferences, so now I can highlight text with my right hand, and with my left, hit a key combo and instantly run the action, returning to the current app in about 3 seconds. And the pasted snippet is available on all my devices, instantly.
So yeah. There you go. Waste of time? Probably. Useful? Actually, yeah, very. And I learned a little something. So let’s just move on.