0

I run a screen which display some data on the menu bar

the variables are taken from "~/Desktop/_MyData.plist"

everything work fine, however when the data change on _MyData.plist

How, can I make the script getting the new data ? I guess we can't expect AppleScript to detect file change and then run the script but is there a way to get the plist data on idle maybe and keeping running the whole script.

Here is the part that only getting the data :

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

Here is the whole script :

  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()
Kevin
  • 1,076
  • 4
  • 22
  • 49

1 Answers1

1

Make use of an idle handler.

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

Therefore you need to save your script as application. Here's the essential part from apples docs:

idle and quit Handlers for Stay-Open Applications

By default, a script application that receives a run or open command handles that single command and then quits. In contrast, a stay-open script application (one saved as Stay Open in Script Editor) stays open after it is launched.

A stay-open script application can be useful for several reasons:

  • Stay-open script applications can receive and handle other commands in addition to run and open. This allows you to use a script application as a script server that, when it is running, provides a collection of handlers that can be invoked by any other script.

  • Stay-open script applications can perform periodic actions, even in the background, as long as the script application is running.

Two particular handlers that stay-open script applications often provide are an idle handler and a quit handler.

idle Handlers

If a stay-open script application includes an idle handler, AppleScript sends the script application periodic idle commands—by default, every 30 seconds—allowing it to perform background tasks when it is not performing other actions.

If an idle handler returns a positive number, that number becomes the rate (in seconds) at which the handler is called. If the handler returns a non-numeric value, the rate is not changed. You can return 0 to maintain the default delay of 30 seconds.

For example, when saved as a stay-open application, the following script beeps every 5 seconds:

on idle
    beep
    return 5
end idle

The result returned from a handler is just the result of the last statement, even if it doesn’t include the word return explicitly.

Community
  • 1
  • 1
Pat_Morita
  • 3,355
  • 3
  • 25
  • 36