2017-07-29 90 views
0

我运行一个屏幕,该屏幕显示菜单栏AppleScript的阅读每隔几分钟

的变量从“〜/桌面/ _MyData.plist”采取的一些数据资料

一切工作正常,但是当在_MyData.plist上更改数据

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

这里是只得到数据的部分:

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

。利用空闲处理程序。

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

因此您需要将脚本另存为应用程序。继承人从苹果文档

空闲的重要组成部分,并退出处理程序保持开申请

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

入住开脚本的应用程序可以有几个原因是有益的:

保持开放脚本的应用程序可以接收和 除了处理其他的命令来运行和开放。这允许您使用脚本应用程序 作为脚本服务器,该脚本服务器在运行时提供一组可由任何其他脚本调用的处理程序集合。

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

两个特定的处理程序,即开放式脚本应用程序通常提供的是 是一个idlehandler和一个退出处理程序。

空闲处理程序

如果逗留开脚本的应用程序包括空闲处理器, 的AppleScript发送脚本应用周期性闲置命令,由 默认情况下,每隔30秒,允许它执行后台任务时 是不执行其他行动。

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

例如,当保存为一个留打开的应用程序,以下 脚本蜂鸣声每5秒:在空闲 哔

回报5
结束闲置