AppleScript每隔几分钟读取一次数据

AppleScript每隔几分钟读取一次数据,applescript,handle,idle-timer,Applescript,Handle,Idle Timer,我运行一个屏幕,在菜单栏上显示一些数据 变量取自“~/Desktop/\u MyData.plist” 但是,当_MyData.plist上的数据发生更改时,一切正常 如何使脚本获取新数据?我想我们不能期望AppleScript检测到文件更改,然后运行脚本,但是有没有办法在空闲时获取plist数据并保持整个脚本的运行 以下是仅获取数据的部分: property theAccountNumberFromPlist : "" property SNNumber : "" property appNa

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

变量取自“~/Desktop/\u 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()

使用空闲处理程序

on idle
-- do your stuff
return 5 -- idle handler will be executed again in 5 seconds.
end 
因此,您需要将脚本另存为应用程序。以下是:

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

默认情况下,接收运行或打开命令的脚本应用程序 处理单个命令,然后退出。相比之下,一家银行则保持营业 脚本应用程序(另存为“在脚本编辑器中保持打开状态”)保持不变 启动后打开

保持打开的脚本应用程序之所以有用,有以下几个原因:

  • 保持打开脚本应用程序可以在中接收和处理其他命令 添加到运行和打开。这允许您使用脚本应用程序 作为脚本服务器,在运行时提供 可由任何其他脚本调用的处理程序

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

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

空闲处理程序

如果保持打开脚本应用程序包含空闲处理程序, AppleScript通过以下方式发送脚本应用程序周期性空闲命令: 默认情况下,每30秒允许它在以下情况下执行后台任务 它不执行其他操作

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

例如,当保存为“保持打开”应用程序时,以下 脚本每5秒蜂鸣一次:

空闲时
嘟嘟声
返回5
结束空闲

处理程序返回的结果只是最后一条语句的结果,即使它没有显式地包含return一词