I wonder if AppleScript can print a variable in the top menu bar in OSX 10.11 ?
Best Answer
There is a simple way, using AppleScriptObjC as described here:
use framework "Foundation"
use framework "AppKit"
use scripting additions
property StatusItem : missing value
property newMenu : class "NSMenu"
-- myVariable derived from whatever code >>
set myVariable to ">> " & "Output of script: value of variable." & " <<"
set bar to current application's NSStatusBar's systemStatusBar
set StatusItem to bar's statusItemWithLength:-1.0
StatusItem's setTitle: myVariable
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
On the original page I posted code that gets a display-dialog's return & puts it onto the menu bar.
Update: This updated script wouldn't flash through the screen, i.e runs in the backgound. The old version is still valid.
tell application "System Events"
tell process "System Preferences"
activate
end tell
tell application "System Preferences"
set current pane to pane "com.apple.preference.energysaver"
end tell
tell process "System Preferences"
click checkbox 1 of window 1
end tell
tell application "System Preferences"
quit
end tell
end tell
From Oct. 18th:
-- Allow Script Editor in Accessibility <- Privacy <- Security & Privacy <- System Preferences. For testing, at the very least.
tell application "System Preferences"
activate
set current pane to pane "com.apple.preference.energysaver"
end tell
tell application "System Events"
tell process "System Preferences"
tell window 1
click checkbox 1
end tell
end tell
end tell
tell application "System Preferences"
quit
end tell
-- Create an Automator Service with this, so that you can bind the action to a keyboard shortcut.
Here is some code that's working on my elderly MacMini'09. "delay" may have to be adjusted ...
tell application "System Preferences"
activate
tell application "Finder" to set position of window 1 of process ¬
"System Preferences" to {1200, 1000}
set current pane to pane id "com.apple.preference.datetime"
end tell
tell application "System Events" to tell process "System Preferences"
delay 0.5
click checkbox "Datum und Uhrzeit in der Menüleiste anzeigen" of ¬
tab group of window 1
end tell
quit application "System Preferences"
I left German strings in the script to avoid home-brew English "translations" > DIY !
Directly upon System-Preferences' launching, its app window is "hidden" to some corner
There's a delay as otherwise things would not work before the final shutdown ...
... Oh, btw, I would recommend to install a keyboard shortcut via Automator/services (all apps)
Best Answer
There is a simple way, using AppleScriptObjC as described here:
On the original page I posted code that gets a display-dialog's return & puts it onto the menu bar.