如何将 Apple 脚本的输出返回到 macOS 中的状态栏? [英] How do I return the output of my Apple Script to the Status Bar in macOS?

查看:15
本文介绍了如何将 Apple 脚本的输出返回到 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:

  1. 将下面的脚本复制到脚本编辑器中
  2. 保存它,从文件格式下拉弹出窗口中选择应用程序",然后单击运行处理程序后保持打开状态"复选框.
  3. 像往常一样运行小程序(您必须授权它控制应用程序).
  1. Copy the script below into Script Editor
  2. Save it, choosing 'Application' from the File Format pull popup, and click the 'Stay open after run handler' checkbox.
  3. 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆