我运行一个屏幕,在菜单栏上显示一些数据
变量取自"〜/ 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()
答案 0 :(得分:1)
使用空闲处理程序。
on idle
-- do your stuff
return 5 -- idle handler will be executed again in 5 seconds.
end
因此,您需要将脚本另存为应用程序。这是essential part from apples docs:
空闲并退出保持开放式应用程序的处理程序
默认情况下,接收运行或打开命令的脚本应用程序 处理该单个命令然后退出。相比之下,保持开放 脚本应用程序(保存为在脚本编辑器中保持打开状态)保留 它启动后打开。
保持打开的脚本应用程序可能有用,原因如下:
保持打开的脚本应用程序可以接收和处理其他命令 除了跑步和开放。这允许您使用脚本应用程序 作为脚本服务器,当它运行时,提供一个集合 可以由任何其他脚本调用的处理程序。
保持打开的脚本应用程序可以执行定期操作,即使在 后台,只要脚本应用程序正在运行。
经常保留开放脚本应用程序的两个特定处理程序 提供了一个空闲的处理程序和一个退出处理程序。
空闲处理程序
如果保持打开的脚本应用程序包含空闲处理程序, AppleScript定期发送脚本应用程序空闲命令 默认值,每30秒 - 允许它执行后台任务 它没有执行其他操作。
如果空闲处理程序返回正数,则该数字成为 调用处理程序的速率(以秒为单位)。如果处理程序 返回非数字值,不更改速率。你可以返回0 保持默认延迟30秒。
例如,当保存为保持打开的应用程序时,以下内容 脚本每5秒发出一声哔声:
闲置时 哔
返回5
结束闲置从处理程序返回的结果只是最后一个语句的结果,即使它没有明确包含单词return。