0

我运行一个屏幕,在菜单栏上显示一些数据

变量取自“~/Desktop/_MyData.plist”

一切正常,但是当 _MyData.plist 上的数据发生变化时

如何让脚本获取新数据?我想我们不能指望 AppleScript 检测文件更改然后运行脚本,但是有没有办法让 plist 数据处于空闲状态并继续运行整个脚本。

这是仅获取数据的部分:

property theAccountNumberFromPlist : ""
property SNNumber : ""
property appName : ""
property devicesID : ""
property domainEMAIL : ""
property fullEmail : ""
property purchaseDate : ""
property thename : ""
property theList : ""

set the plistfile_path to "~/Desktop/_MyData.plist.plist"
tell application "System Events"
    set p_list to property list file (plistfile_path)
    -- read the plist data

set theAccountNumberFromPlist to value of property list item "AccountNumber" of p_list as text
set SNNumber to value of property list item "SNNUMBER" of p_list as text
set appName to value of property list item "appName" of p_list as text
set devicesID to value of property list item "devicesID" of p_list as text
set domainEMAIL to value of property list item "domainEMAIL" of p_list as text
set fullEmail to value of property list item "fullEmail" of p_list as text
set purchaseDate to value of property list item "purchaseDate" of p_list as text
set thename to value of property list item "thename" of p_list as text

结束告诉

这是整个脚本:

  use AppleScript version "2.4"
use scripting additions
use framework "Foundation"
use framework "AppKit"

property StatusItem : missing value
property selectedMenu : ""
property theDisplay : ""
property defaults : class "NSUserDefaults"
property internalMenuItem : class "NSMenuItem"
property externalMenuItem : class "NSMenuItem"
property newMenu : class "NSMenu"
property theAccountNumberFromPlist : ""
property SNNumber : ""
property appName : ""
property devicesID : ""
property domainEMAIL : ""
property fullEmail : ""
property purchaseDate : ""
property thename : ""
property theList : ""




set the plistfile_path to "~/Desktop/_MyData.plist.plist"
tell application "System Events"
    set p_list to property list file (plistfile_path)
    -- read the plist data

    set theAccountNumberFromPlist to value of property list item "AccountNumber" of p_list as text
    set SNNumber to value of property list item "SNNUMBER" of p_list as text
    set appName to value of property list item "appName" of p_list as text
    set devicesID to value of property list item "devicesID" of p_list as text
    set domainEMAIL to value of property list item "domainEMAIL" of p_list as text
    set fullEmail to value of property list item "fullEmail" of p_list as text
    set purchaseDate to value of property list item "purchaseDate" of p_list as text
    set thename to value of property list item "thename" of p_list as text
end tell
if not (current application's NSThread's isMainThread()) as boolean then
    display alert "This script must be run from the main thread." buttons {"Cancel"} as critical
    error number -128
end if

on menuNeedsUpdate:(menu)

    my makeMenus()
end menuNeedsUpdate:

on makeMenus()

    newMenu's removeAllItems()

    repeat with i from 1 to number of items in someListInstances
        set this_item to item i of someListInstances
        set thisMenuItem to (current application's NSMenuItem's alloc()'s initWithTitle:this_item action:"someAction:" keyEquivalent:"")

        (newMenu's addItem:thisMenuItem)

        (thisMenuItem's setTarget:me) -- required for enabling the menu item
        if i is equal to 3 then
            (newMenu's addItem:(current application's NSMenuItem's separatorItem)) -- add a seperator
        end if
    end repeat

end makeMenus



on someAction:sender
    --MenuItem 
end someAction:

-- create an NSStatusBar
on makeStatusBar()
    set bar to current application's NSStatusBar's systemStatusBar

    set StatusItem to bar's statusItemWithLength:-1.0

    -- set up the initial NSStatusBars title
    StatusItem's setTitle:(theAccountNumberFromPlist & " " & thename & " " & fullEmail & " " & SNNumber & " " & appName)
    -- set up the initial NSMenu of the statusbar
    set newMenu to current application's NSMenu's alloc()'s initWithTitle:"Custom"

    newMenu's setDelegate:me (*


    *)

    StatusItem's setMenu:newMenu

end makeStatusBar


my makeStatusBar()
4

1 回答 1

1

使用空闲处理程序。

on idle
-- do your stuff
return 5 -- idle handler will be executed again in 5 seconds.
end 

因此,您需要将脚本保存为应用程序。这是apples docs 的重要部分

保持打开应用程序的空闲和退出处理程序

默认情况下,接收运行或打开命令的脚本应用程序会处理该单个命令,然后退出。相比之下,保持打开的脚本应用程序(在脚本编辑器中保存为保持打开)在启动后保持打开状态。

保持打开的脚本应用程序很有用,原因如下:

  • 保持打开脚本应用程序除了运行和打开之外,还可以接收和处理其他命令。这允许您将脚本应用程序用作脚本服务器,当它运行时,它会提供一组可以由任何其他脚本调用的处理程序。

  • 保持打开的脚本应用程序可以执行定期操作,即使在后台,只要脚本应用程序正在运行。

保持打开脚本应用程序通常提供的两个特定处理程序是空闲处理程序和退出处理程序。

空闲处理程序

如果保持打开的脚本应用程序包含空闲处理程序,AppleScript 会定期向脚本应用程序发送空闲命令(默认情况下,每 30 秒一次),允许它在不执行其他操作时执行后台任务。

如果空闲处理程序返回一个正数,则该数字将成为调用处理程序的速率(以秒为单位)。如果处理程序返回非数字值,则速率不会更改。您可以返回 0 以维持 30 秒的默认延迟。

例如,当保存为保持打开的应用程序时,以下脚本每 5 秒发出一次哔声:

空闲时
    蜂鸣
    返回 5
结束空闲

从处理程序返回的结果只是最后一条语句的结果,即使它没有明确包含 return 一词。

于 2017-07-30T09:14:20.693 回答