如何用AppleScript获取终端脚本输出并显示在Scrollable Text View中?
用AppleScript实现Shell命令输出到Scrollable Text View
完全可以实现这个需求。纯AppleScript本身没有原生UI控件,需要借助AppleScriptObjC调用Cocoa框架创建包含Scrollable Text View的窗口,同时捕获Shell命令的输出并展示。下面分两种场景给出实现方案:
场景1:命令执行完毕后一次性展示全部输出
适合不需要实时查看进度的命令,用do shell script同步捕获输出:
use framework "Cocoa" use scripting additions -- 创建基础窗口 set mainWindow to current application's NSWindow's alloc()'s initWithContentRect:{{100, 100}, {600, 400}} ¬ styleMask:(current application's NSTitledWindowMask + current application's NSClosableWindowMask + current application's NSMiniaturizableWindowMask) ¬ backing:(current application's NSBackingStoreBuffered) defer:true mainWindow's setTitle:"Shell Output Viewer" -- 创建滚动文本视图 set scrollView to current application's NSScrollView's alloc()'s initWithFrame:(mainWindow's contentView()'s bounds()) scrollView's setHasVerticalScroller:true scrollView's setHasHorizontalScroller:true scrollView's setAutoresizingMask:(current application's NSViewWidthSizable + current application's NSViewHeightSizable) set outputTextView to current application's NSTextView's alloc()'s initWithFrame:(scrollView's bounds()) outputTextView's setEditable:false outputTextView's setAutoresizingMask:(current application's NSViewWidthSizable + current application's NSViewHeightSizable) scrollView's setDocumentView:outputTextView mainWindow's contentView()'s addSubview:scrollView -- 执行Shell命令并捕获输出(包含错误信息) try set commandOutput to do shell script "git clone https://github.com/torvalds/linux.git" on error errMsg number errCode set commandOutput to "执行错误 (" & errCode & "): " & errMsg end try -- 将输出写入文本视图 outputTextView's setString:commandOutput -- 显示窗口并启动应用循环 mainWindow's makeKeyAndOrderFront:me current application's NSApp's run()
场景2:实时展示命令输出(如git clone的进度)
如果需要实时查看命令的持续输出(比如克隆仓库的进度条),do shell script的同步模式会阻塞UI,此时需要用NSTask异步执行命令并实时捕获输出:
use framework "Cocoa" use scripting additions property outputTextView : missing value -- 创建基础窗口 set mainWindow to current application's NSWindow's alloc()'s initWithContentRect:{{100, 100}, {600, 400}} ¬ styleMask:(current application's NSTitledWindowMask + current application's NSClosableWindowMask + current application's NSMiniaturizableWindowMask) ¬ backing:(current application's NSBackingStoreBuffered) defer:true mainWindow's setTitle:"Real-time Shell Output Viewer" -- 创建滚动文本视图 set scrollView to current application's NSScrollView's alloc()'s initWithFrame:(mainWindow's contentView()'s bounds()) scrollView's setHasVerticalScroller:true scrollView's setHasHorizontalScroller:true scrollView's setAutoresizingMask:(current application's NSViewWidthSizable + current application's NSViewHeightSizable) set outputTextView to current application's NSTextView's alloc()'s initWithFrame:(scrollView's bounds()) outputTextView's setEditable:false outputTextView's setAutoresizingMask:(current application's NSViewWidthSizable + current application's NSViewHeightSizable) scrollView's setDocumentView:outputTextView mainWindow's contentView()'s addSubview:scrollView -- 配置异步任务执行git命令 set gitTask to current application's NSTask's new() gitTask's setLaunchPath:"/usr/bin/git" gitTask's setArguments:{"clone", "https://github.com/torvalds/linux.git"} -- 创建管道捕获标准输出和错误输出 set outputPipe to current application's NSPipe's pipe() gitTask's setStandardOutput:outputPipe gitTask's setStandardError:outputPipe -- 将错误输出合并到同一管道 set fileHandle to outputPipe's fileHandleForReading() -- 设置可读回调,实时读取输出并更新UI fileHandle's readabilityHandler:^(current application's NSFileHandle *handle) { set data to handle's availableData() if data's |length|() > 0 then set newOutput to current application's NSString's alloc()'s initWithData:data encoding:(current application's NSUTF8StringEncoding) -- 必须在主线程更新UI,避免线程冲突 current application's dispatch_sync(current application's dispatch_get_main_queue(), ^{ outputTextView's insertText:newOutput -- 自动滚动到最新内容 set endRange to current application's NSMakeRange(outputTextView's string()'s |length|(), 0) outputTextView's scrollRangeToVisible:endRange }) else handle's setReadabilityHandler:(missing value) -- 无数据时停止监听 end if } -- 启动任务 gitTask's launch() -- 显示窗口并启动应用循环 mainWindow's makeKeyAndOrderFront:me current application's NSApp's run()
使用说明
- 打开Script Editor,将代码粘贴进去,格式选择AppleScript + Objective-C
- 运行脚本,会弹出对应的窗口,执行命令后输出会显示在滚动文本视图中
内容的提问来源于stack exchange,提问作者TheOnlyOneHere
相关产品推荐
相关产品推荐

