Chris Barrett
Chris Barrett

Reputation: 601

Display Variable from Repeat loop in a Floating Window

I have an applescript that captures a counter in another application. This works fine but I'd like to output the results to another floating window and have it update with each loop. Does anyone know of a way to do this? Complete newb.

Thanks

EDIT:

My code is:

tell application "System Events"

tell process "MIDI Editor"
    with timeout of 0 seconds
        repeat
            set barCount to value of text field "Main Counter" of group "Counter Display Cluster" of window "Edit: kjhsdf" of application process "MIDI Editor" of application "System Events"
            delay 0.01
        end repeat
    end timeout
end tell

end tell

(Not sure why that last end tell keeps breaking out of the code block!)

So its barCount that I want to mirror in real time in another window

Upvotes: 0

Views: 196

Answers (1)

red_menace
red_menace

Reputation: 3422

From the Script Editor you can use some AppleScriptObjC to programmatically create a non-modal window with a text field that can be updated. In the example below I am using a repeating timer instead of an AppleScript repeat statement, as tight loops like that will block the user interface. Save the script as an application, with the option set to stay open.

use AppleScript version "2.4" -- Yosemite (10.10) or later
use framework "Cocoa"
use scripting additions

property WindowFrame : {{200, 600}, {150, 50}} -- window location and size
property TextFrame : {{10, 10}, {130, 30}} -- window size minus 20
property mainWindow : missing value
property textField : missing value
property timer : missing value

on run -- example
  setup()
  update()
  set my timer to current application's NSTimer's timerWithTimeInterval:0.25 target:me selector:"update" userInfo:(missing value) repeats:true
  current application's NSRunLoop's mainRunLoop's addTimer:timer forMode:(current application's NSDefaultRunLoopMode)
end run

to update() -- update text field
  set barCount to ""
  with timeout of 0.5 seconds
    tell application "System Events" to tell process "MIDI Editor"
      set barCount to value of text field "Main Counter" of group "Counter Display Cluster" of window "Edit: kjhsdf"
    end tell
  end timeout
  textField's setStringValue:(barCount as text)
end update

to setup() -- create UI objects
  tell (current application's NSTextField's alloc's initWithFrame:TextFrame)
    set my textField to it
    its setFont:(current application's NSFont's fontWithName:"Menlo" |size|:18)
    its setBordered:false
    its setDrawsBackground:false
    its setSelectable:false
  end tell
  tell (current application's NSWindow's alloc's initWithContentRect:WindowFrame styleMask:1 backing:(current application's NSBackingStoreBuffered) defer:true)
    set my mainWindow to it
    its setAllowsConcurrentViewDrawing:true
    its setHasShadow:true
    its setTitle:"Progress"
    its setLevel:(current application's NSFloatingWindowLevel)
    its (contentView's addSubview:textField)
    its setFrameAutosaveName:"Update Window" -- keep window position
    its setFrameUsingName:"Update Window"
    its makeKeyAndOrderFront:me
  end tell
end setup

Upvotes: 2

Related Questions