Parse menubar item text

I need a macro that will check a menu item text, which lists how long it has been since an app (Daylite) has synced. If the duration is more than, say 30 minutes, then KM would kill Daylite Helper (I already have a macro for that), and it would sync. Does anyone know how to get and parse this "time-since-sync" information and parse it? I could have a regularly timed KM macro run and check. Here's what the menu looks like for reference.

Just wanted to ask again to see if anyone had any suggestions on this question. Thank you in advance!

You could use the OCR image action after a show the menu action. I'm not sure it's the best way by any means, but I have a hunch that text might not be accessible many other ways. I'd be happy to be wrong though.

Just FYI - I was able to figure out how to read that menubar item text with AppleScript, in case anyone else needs it:

    activate application "Daylite"
    tell application "System Events"
    	tell process "Daylite"
    		set xxx to name of every menu item of menu 1 of menu bar item "File" of menu bar 1
    	end tell
    end tell
    set lastsync to item 20 of xxx
    lastsync contains "hour"

Excellent! Thanks for sharing. :+1:

I made a minor mod to allow for the "Last Sync" menu item to be in a different row in the menu list. For some reason, it was in row 19 on my system.

(*
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

REF:  The following were used in some way in the writing of this script.

  1.  2019-11-10 10:36, egordin, Keyboard Maestro Discourse
      Parse menubar item text
      https://forum.keyboardmaestro.com/t/parse-menubar-item-text/15839/4?u=jmichaeltx

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*)
activate application "Daylite"
tell application "System Events"
  tell process "Daylite"
    set menuList to name of every menu item of menu 1 of menu bar item "File" of menu bar 1
  end tell
end tell
### set lastsync to item 20 of menuList ###

--- Get Menu Item that Starts With "Last Sync" ---
set lastsync to getItem(menuList, "Last sync", "starts with")
return (lastsync contains "hour")

--~~~~~~~~~~~~~~~~~~~~~~~ END OF MAIN SCRIPT ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~



on getItem(pList, pMatch, pMatchAction)
  
  set pMatchStr to pMatch
  set foundItem to ""
  
  if (pMatchAction = "starts with") then
    repeat with oItem in pList
      if (oItem starts with pMatchStr) then
        set foundItem to contents of oItem
        exit repeat
      end if
    end repeat
    
  else if (pMatchAction = "ends with") then
    repeat with oItem in pList
      if (oItem ends with pMatchStr) then
        set foundItem to contents of oItem
        exit repeat
      end if
    end repeat
    
  else if (pMatchAction = "contains") then
    repeat with oItem in pList
      if (oItem contains pMatchStr) then
        set foundItem to contents of oItem
        exit repeat
      end if
    end repeat
  end if
  
  return foundItem
end getItem


This is fantastic. The result is so much better when someone who knows what they're doing takes hold of what I did haha.

Thanks again!

1 Like