8

Before reading further: I know about butler and similar programs. I am looking for a BUILT-IN way to do this without a third party app.

Anyway, I wrote an AppleScript that runs a shell command. I want to display that output in the menubar at the top... without Butler how can I do this?

  • Since AppleScriptObjC is part of macOS (2014) it is possible to use its "Foundation" framework (incl. NSMenu's methods) to achieve what was probably not possible in 2012. . . . . . See my answer below on How-to-do-it ! – clemsam lang Dec 16 '18 at 16:12
2

As a generality, without a third party program, such as Growl, there is no built in way to do this.

However, you could write a script or other program such as one I found here that provides the menulet service for you. I'm sure that Growl integration would be much easier to accomplish.

1

There isn't a built in way to do this in OS X. However, using Growl, you can have notifications. Here's a sample script for that:

--Make sure Growl is running
tell application "System Events"
    set isRunning to (count of (every process whose bundle identifier is "com.Growl.GrowlHelperApp")) > 0
end tell

if isRunning then
    tell application id "com.Growl.GrowlHelperApp"
        set the allNotificationsList to ¬
            {"Test Notification", "Another Test Notification"}
        --Notifications can be enabled in System Preferences>Growl>Applications>Display Options
        set the enabledNotificationsList to ¬
            {"Test Notification"}
        register as application ¬
            "Growl AppleScript Sample" all notifications allNotificationsList ¬
            default notifications enabledNotificationsList ¬
                    -- Set the icon. You can use any icon from any application
            icon of application "AppleScript Editor"

        notify with name ¬
            "Test Notification" title ¬
            "Test Notification" description ¬
            "This is a test AppleScript notification." application name "Growl AppleScript Sample"

        notify with name ¬
            "Another Test Notification" title ¬
            "Another Test Notification :) " description ¬
            "Alas — you won't see me until you enable me..." application name "Growl AppleScript Sample"

    end tell
end if

That should display this:

And if you have the other notification enabled too:

More advanced techniques are described here.

1

Since AppleScriptObjC is part of macOS it is possible to use its "Foundation" framework (incl. NSMenu's methods) to achieve what was probably not possible in 2012.

I found an interesting script to create custom menus from within AppleScript; from this I extracted suitable code to place text in macOS's menu bar. In fact it uses just a menu's "title" for inserting some content.

In order to demonstrate this I implemented a very basic dialog script that asks users for text input (waiting 6 sec.) which is then displayed in the menu bar temporarily (5 sec.).
Here it is:

use framework "Foundation"
use framework "AppKit"
use scripting additions
property StatusItem : missing value
property newMenu : class "NSMenu"

display dialog "Write something:" default answer "" giving up after 6
set myText to text returned of the result
if myText is "" then set myText to "TOOOOO slow … try again !"
set myText to ">>    " & myText & "    <<"

set bar to current application's NSStatusBar's systemStatusBar
set StatusItem to bar's statusItemWithLength:-1.0
StatusItem's setTitle:myText
set newMenu to current application's NSMenu's alloc()'s initWithTitle:"Custom"
StatusItem's setMenu:newMenu

delay 5
current application's NSStatusBar's systemStatusBar()'s ¬
        removeStatusItem:StatusItem  

This AppleScript code can be used in any script of yours. (Its "dialog" part is optional…)

user3439894 helped with closing my "menu", see last line of the script. Thanks a lot!

  • Do you by chance have a link to the original source code you derived your answer from? It would be nice to see. Thanks. – user3439894 Dec 14 '18 at 16:18
  • 1
    Yes – it's in the last handler of the accepted answer to this question: https://stackoverflow.com/questions/29168474/creating-a-simple-menubar-app-using-applescript . . . . . I am afraid though, that my last line (works half of the time) that I found somewhere else –and adapted– is NOT correct . . . . . . I am not a programmer so I need some more time to understand code. – clemsam lang Dec 14 '18 at 16:26
  • 1
    BTW It's a nice answer and if not for "it crashed a few times – because of "dealloc()" ?" I would have +1. Some food for thought though... if you use this code as a stand alone AppleScriptObjC app, maybe that would be better, then StatusItem's dealloc() is only applicable to the AppleScriptObjC app and not Script Editor and accidentally crashing it. – user3439894 Dec 14 '18 at 16:45
  • FWIW The first set myText to ... line of code, the one before display dialog command really does nothing based on the ensuing code and therefore is not at all needed. In other words, the only used value of myText is that which is assigned after the display dialog command is run and why setting it to anything prior to the the display dialog command is unused coding and not necessary in the context of the code currently presented. – user3439894 Dec 14 '18 at 18:37
  • 1
    Instead of StatusItem's dealloc() or StatusItem's setTitle:"" I'd use: current application's NSStatusBar's systemStatusBar()'s removeStatusItem:StatusItem – user3439894 Dec 15 '18 at 13:55

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .