Script kidding

Yesterday I wrote a little bit of code and learned a bit more about the current state of OS X automation. Today I took another small step on my personal automation, itch-finding and itch-scratching journey.

Like many second steps, it was even simpler than the first. I created a service which takes the selected text, fetches the URL of the currently active page in Safari and creates a Markdown link of the selection.

Here is the gist, copious documentation included.

Again, extremely simple, but still enough to make the limitations of Apple's scripting tools clear. Namely that testing is difficult. The script editor - more than sparse enough in its assitance for any purpose - provides no guidance as to how a script for use in Automator workflows should be shaped. Without knowing anything about the shape of incoming data it is pretty hard to know what to prepare for. I just wrote my code to fetch the URL from Safari, then moved on to Automator. Once there, things improved in that adding a Javascript action provided a function to paste code into. Turns out you get "input" and "parameters", and input was the straightforward string of text I was hoping for. I pasted in my URL fetching and simply returned a new string created by pasting URL and text together with the appropriate Markdown link syntax around.

So far so good, all I wondered was how I would test it without saving, restarting applications and flailing wildly. Happily, Automator told me when I tried clicking the run button. Politely, it informed me that the service would not be able to get any input inside Automator, but that I could add an action to simulate this. And I could, dragging in "Get specified text" at the start of my workflow let me specify input for my function. There was even a "View results" action to clearly display output afterward.

The one remaining question, I guess, is why those two actions are not automatically active (when needed) inside Automator and then removed in the finished workflow. The dialog which told me about "Get specified text" was careful in reminding me to remove the action after using it, but it sure seems like a thing a decent computer could manage for me.

If you want to write scripts for inclusion in Automator workflows, it seems to me you should be doing your writing in Automator, rather than in Script editor.

What I would really want, though, is to be able to write in Safari and have access to some real developer tools. Auto completion, breakpoints, a REPL, you name it. Surely there is a way of simulating the environment of a script in there too and let us have the good tools for everything?