Take the 2-minute tour ×
Ask Different is a question and answer site for power users of Apple hardware and software. It's 100% free, no registration required.

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.

share|improve this question
2  
Notification Center has no AppleScript Dictionary, no Automator actions, and sadly little in the way of obvious hooks that show up when poking around the executable bundle. –  Daniel Lawson Jul 27 '12 at 5:14
1  
A strings dump of the suggests there's something called "ShareKit", various sharingService functions, and something called a "Share Widget" — perhaps some subset of that might be helpful somehow. –  Daniel Lawson Jul 27 '12 at 5:20
    
I'll have time to dig into the great answers tomorrow but I wanted to bounty this for any additional exposure it might gain. –  bmike Jul 28 '12 at 17:48
add comment

6 Answers

up vote 7 down vote accepted
+100

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.)

share|improve this answer
2  
And a keystroke command can start the Tweet out with text. Now to programmatically complete the Tweet… –  Daniel Lawson Jul 27 '12 at 6:10
add comment

None that I know of (and in fact I think that having a Twitter/Facebook quick post area inside the notifications area is actually dumb (should be a widget really), and I have turned it off) but you can use the command line to both send a read tweets, as mentioned in this webpage, extracts below:


To display a list of tweets (replace osxdaily with a twitter username of your choice):

curl -s http://twitter.com/osxdaily | grep '' | cut -d">" -f2 | cut -d"<" -f1

To update your twitter status:

curl -u your_user:your_password -d status='This is My update' https://twitter.com/statuses/update.xml
share|improve this answer
    
The text entry area was the bit I was hoping to use. I'll have to dig into the launchbar docs and see if the curl idea has any legs.... I thought they disabled that sending of a password in the clear - so thanks for that! –  bmike Jul 26 '12 at 17:32
    
I think they disabled the use of password sending for clients that use the API (replacing it with the key authentication method), but this is, in effect, using the website not a client, so using the username/password is likely fine. In fact, if you are already logged in with an active session and cookie etc, it may even work without them... (guesswork) –  stuffe Jul 26 '12 at 17:34
    
I don't think cookies are shared between Safari & curl. And they shouldn't be, anyway. –  olivier Jul 27 '12 at 9:20
2  
Twitter fully switched to OAuth-based logins and disabled basic authentication on June 30, 2010. The second command in your answer hasn’t worked since then. –  Mathias Bynens Aug 2 '12 at 17:47
add comment

Taking this all one step further and putting together what we've learned so far, here's a fully programatic tweet:

tell application "System Events"
    tell process "Notification Center"
        click menu bar item 1 of menu bar 1
        click button 1 of UI element 1 of row 2 of table 1 of scroll area 1 of window "window"
        keystroke "Content of the tweet"
        keystroke "D" using {command down, shift down}
    end tell
end tell

Of course this is fragile, but for now, it works. I'd love to find a real hook, but UI Scripting is a workaround.

share|improve this answer
    
Oh, neat. It's logical that ⇧⌘D would send the tweet (that's the shortcut for Send in Mail). –  jtbandes Jul 28 '12 at 3:51
1  
Found it by mistake. Muscle memory is powerful. –  Daniel Lawson Jul 28 '12 at 3:52
add comment

Brilliant command shift D.

Adding:

display dialog "Tweet?" default answer "" buttons {"OK"} default button 1
set mytweet to text returned of result

tell application "System Events"
    tell process "Notification Center"
        click menu bar item 1 of menu bar 1
        click button 1 of UI element 1 of row 2 of table 1 of scroll area 1 of window "window"
        keystroke mytweet
        keystroke "D" using {command down, shift down}
        keystroke space
    end tell
end tell
share|improve this answer
add comment

I wrote another script that fixes some issues in the script posted by Ewwis:

  • There was no way to close the dialog at the start.
  • The second click action didn't work if Notification Center hadn't been shown after the last login.
  • The script didn't work when there was a delay before the view for composing a tweet was shown. If it already contained some text, it wasn't cleared.
  • The keystroke command only works for inserting characters that can be entered with the current input method.
  • The Notification Center sidebar wasn't closed at the end.

It doesn't work when the Notification Center sidebar is open though.

set answer to text returned of (display dialog "" default answer "")
try
    set old to the clipboard as record
end try
try
    set text item delimiters to linefeed
    set the clipboard to paragraphs of answer as text
    tell application "System Events"
        tell process "Notification Center"
            click menu bar item 1 of menu bar 1
            try
                windows
            on error
                click menu bar item 1 of menu bar 1
                click menu bar item 1 of menu bar 1
            end try
            click button 1 of UI element 1 of row 2 of table 1 of scroll area 1 of window 1
            delay 0.1
            keystroke "av" using command down
            keystroke "d" using {shift down, command down}
            repeat 100 times
                try
                    delay 0.1
                    click menu bar item 1 of menu bar 1
                    exit repeat
                end try
            end repeat
        end tell
    end tell
end try
try
    set the clipboard to old
end try

It would be easier to just use the API.

share|improve this answer
    
Hmm - I'll have to look into the API. That plus my launcher, Launchbar would be much better than UI scripting. +1 and possibly a new best answer. –  bmike Dec 5 '12 at 19:55
add comment

Fantastic! Thanks for showing the world another way.

My solution worked for ME, but so does yours.

I'm not an Applescript expert by FAR, but I do love fiddling with it.

Thanks!

Using what I've learned from you, here is another way that works for me. This doesn't address some of your concerns about alternate keyboards or errors, but maybe it will shed a light for someone dabbling in AS.

display dialog "Tweet?" default answer "" buttons {"OK"} default button 1 with icon 2
set mytweet to text returned of result

tell application "System Events"
    tell process "Notification Center"
        click menu bar item 1 of menu bar 1
        click button 1 of UI element 1 of row 2 of table 1 of scroll area 1 of window "window"
        keystroke mytweet
        keystroke "D" using {command down, shift down}
        repeat 100 times
            try
                delay 0.1
                click menu bar item 1 of menu bar 1
                exit repeat
            end try
        end repeat
    end tell
end tell
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.