MacOS – What hooks exist into Notification Center / twitter so that I can tweet programmatically

macosnotificationstwitter

I'm specifically looking to design a custom action for LaunchBar so that I can initiate a tweet from that utility. Since Notification Center has a "Click to tweet" button, I wondered if Notification Center has any hooks that would allow me to script this without waiting for the developer of the program add a function to perform this action.

Best Answer

Apps can hook into the sharing options with the new NSSharingService API. It sounds like custom LaunchBar actions can be made with any UNIX executable file, so you could probably write a small command line tool (or you may need to build an actual app — you'll have to test it out) which activates this API (using NSSharingServiceNamePostOnTwitter), and that should display the tweet dialog.

Update: to initiate a tweet from AppleScript, you can do the following:

tell application "System Events"
    tell process "Notification Center"
        -- activate notification center
        if (count of UI elements) is 1 then click first menu bar's first menu bar item
        -- click the tweet button
        click button 1 of UI element 1 of row 2 of table 1 of scroll area 1 of window "Window"
    end tell
end tell

Furthermore, you can toggle the "Show Alerts and Banners" / do not disturb mode:

tell application "System Events"
    tell process "Notification Center"
        key down option
        click first menu bar's first menu bar item
        key up option
    end tell
end tell

(This is all very specific to the current window layout of Notification Center and is likely to break with future OS X updates — but there will probably be easy fixes.)