applescript - 在浮动窗口中显示来自重复循环的变量
问题描述
我有一个applescript,它在另一个应用程序中捕获一个计数器。这工作正常,但我想将结果输出到另一个浮动窗口并让它随着每个循环更新。有谁知道这样做的方法?完成新手。
谢谢
编辑:
我的代码是:
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
(不知道为什么最后一个结束告诉不断突破代码块!)
所以我想在另一个窗口中实时反映它的 barCount
解决方案
在脚本编辑器中,您可以使用一些 AppleScriptObjC 以编程方式创建具有可更新文本字段的非模式窗口。在下面的示例中,我使用重复计时器而不是 AppleScript 重复语句,因为这样的紧密循环会阻塞用户界面。将脚本另存为应用程序,并将选项设置为保持打开状态。
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
推荐阅读
- matlab - 线性代数:删除行/行并使用减少的雅可比进行项目等价于首先使用完整的雅可比进行项目并在行/行之后删除?
- python - 如何处理 xml 中的 nillable 值?
- tensorflow - 拟合 tensorflow.keras 模型的问题
- python - 抓取市场数据
- r - ggplot2-legend 的两种颜色
- html - 如何在托管的 wasm 应用程序中动态更改应用程序基本路径?
- r - 在 R (REST API) 中发出 GET 请求
- reactjs - 如何映射对象列表
- vue.js - 在vue图表js中重新渲染多个图表
- flutter - Flutter 将单选按钮逻辑应用于选择芯片