-- Prospector menu-bar (tray) app. Run via osascript by the app bundle's bin -- (MacOS/Prospector, a small featureful dispatcher; default = tray UI). -- The bin passes the bundle Resources dir as the first argument to the script. -- Creates a status item with a small menu (Open / Stop / Quit) and keeps the -- run loop alive. The bin also supports CLI features: launch/stop/help. use framework "Foundation" use framework "AppKit" use scripting additions property statusItem : missing value property resourcesDir : "" on run argv set resourcesDir to (item 1 of argv) as text set theApp to current application's NSApplication's sharedApplication() theApp's setActivationPolicy:(current application's NSApplicationActivationPolicyAccessory) set bar to current application's NSStatusBar's systemStatusBar() set statusItem to bar's statusItemWithLength:(current application's NSVariableStatusItemLength) set iconFile to resourcesDir & "/tray.png" set img to current application's NSImage's alloc()'s initWithContentsOfFile:iconFile if img is not missing value then img's setTemplate:false (statusItem's button()'s setImage:img) else (statusItem's button()'s setTitle:"❖") end if (statusItem's button()'s setToolTip:"Prospector") set theMenu to current application's NSMenu's alloc()'s initWithTitle:"Prospector" set openItem to current application's NSMenuItem's alloc()'s initWithTitle:"Open Prospector" action:"openProspector:" keyEquivalent:"" openItem's setTarget:me theMenu's addItem:openItem theMenu's addItem:(current application's NSMenuItem's separatorItem()) set stopItem to current application's NSMenuItem's alloc()'s initWithTitle:"Stop services" action:"stopServices:" keyEquivalent:"" stopItem's setTarget:me theMenu's addItem:stopItem set quitItem to current application's NSMenuItem's alloc()'s initWithTitle:"Quit Prospector" action:"quitTray:" keyEquivalent:"q" quitItem's setTarget:me theMenu's addItem:quitItem statusItem's setMenu:theMenu theApp's |run|() end run on openProspector:sender do shell script "/bin/bash " & quoted form of (resourcesDir & "/launch.sh") & " > /dev/null 2>&1 &" end openProspector: on stopServices:sender do shell script "/bin/bash " & quoted form of (resourcesDir & "/stop.sh") & " > /dev/null 2>&1" end stopServices: on quitTray:sender current application's NSApplication's sharedApplication()'s terminate:me end quitTray: