如何将Apple脚本的输出返回到macOS的状态栏? [英] How do I return the output of my Apple Script to the Status Bar in macOS?
问题描述
我正在处理一个脚本,该脚本在一个应用程序中查找您花费在执行某项活动上的时间,然后将该数字显示在Mac的状态栏中,就像时钟在计算机中不断计数一样右上角.我见过其他类似的人可以在同一区域为您显示IP,这与我要完成的工作很接近.
I am working on a script that looks in one app for the amount of time you've spent doing a certain activity, then displays that number in the status bar of the Mac, just like the clock that continuously counts up in the upper right corner. I've seen others like it that can show you your IP in the same area, which is close to what I'm trying to accomplish.
我认为我的脚本可以一直运行,直到我正在工作的应用程序完全退出为止,但是,我不确定如何在状态栏的顶部显示该数字.无需打开该应用程序即可看到.
I think I have the script functioning to where it will run continuously until the application where I'm working is fully quit, however, I'm unsure of how to display that number up top in the status bar where it can be seen without needing to open said application.
我一直在寻找AppleScriptObjC作为一种选择,这对我来说是一个新天地,我想知道在完全学习之前是否应该使用它.
I've been looking into AppleScriptObjC as an option, however, that is new ground for me, and I'd like to know if that is what should be used before I completely dive in.
我用Python创建了一个菜单栏小应用程序,但是,我了解到根本不需要使用Python,而且我不确定如何将AppleScript与我在Python中创建的内容结合起来.
I created a menu bar applet with Python, however, I learned that using Python might not be needed at all, and I wasn't sure how I would combine the AppleScript with what I created in Python.
tell application "System Events"
set appName to "App I'm Using"
tell process "App I'm Using"
set activityState to value of menu button 1 of group 1 of group 4 of toolbar 1 of window of application process "App I'm Using" of application "System Events" as list
return first item of activityState as string
end tell
end tell
repeat
tell application "System Events"
if "App I'm Using" is not in (name of application processes) then exit repeat
end tell
delay 5
end repeat
到目前为止,我没有遇到任何错误消息;我只是不知道如何继续在顶部的状态栏中返回脚本的连续输出.
As of now, I'm not encountering any error messages; I just don't know how to move forward with returning the continuous output of the script in the status bar up top.
推荐答案
如果您想要一个显示文本的简单状态项,此脚本(保存为保持打开状态的脚本应用程序)应该可以解决问题.这样做:
If you want a simple status item that displays text, this script (saved as a stay-open script application) should do the trick. Do this:
- 将下面的脚本复制到脚本编辑器中
- 保存它,从 File Format 下拉菜单中选择"Application",然后单击运行处理程序后保持打开状态"复选框.
- 像平常一样运行applet(您必须授予它控制应用程序的权限).
- Copy the script below into Script Editor
- Save it, choosing 'Application' from the File Format pull popup, and click the 'Stay open after run handler' checkbox.
- Run the applet like normal (you'll have to give it authorization to control apps).
use framework "AppKit"
use scripting additions
property ca : current application
property NSStatusBar : class "NSStatusBar"
property appName : "App Name"
global statusItem
on run
set statusItem to NSStatusBar's systemStatusBar's statusItemWithLength:(ca's NSVariableStatusItemLength)
set statusItem's button's title to "Initializing"
end run
on idle
-- Update the status item's text here.
tell application "System Events"
if not (exists process appName) then
display alert "Application " & appName & " is not running" as warning giving up after 6
quit me
end if
tell process appName
tell first window's first toolbar's fourth group's first group's first menu button
set activityState to first item of (value as list) as text
end tell
end tell
end tell
set statusItem's button's title to activityState
(*
The return value gives the idle time, so if you want the menu item
to update (say) every half second, use 'return .5'
*)
return 1
end idle
on quit
-- remove status item and quit
NSStatusBar's systemStatusBar's removeStatusItem:statusItem
continue quit
end quit
我想,如果您想要更复杂的行为(例如状态项的功能菜单或可点击的项),则必须转向可可苹果脚本应用程序.
If you want more complex behavior — like a functional menu for the status item, or a clickable item — you'll have to shift to a cocoa-applescript application, I think.
这篇关于如何将Apple脚本的输出返回到macOS的状态栏?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!