Applescript Xcode,我需要读取文件并在标签上连续显示更改

Applescript Xcode, I need to read a file and show changes continuously on a label

当我更改 TextField 内容时,我的代码会更新标签,但这不是我需要的:

property aTextField : missing value
property aTextLabel : missing value

on controlTextDidChange_(aNotification)
    log aTextField's stringValue
    aTextLabel's setStringValue: aTextField's stringValue
end textDidChange_

我需要这样的东西:

property aTextLabel : missing value

on applicationWillFinishLaunching_(aNotification)
    repeat
        set MyFileTextContent to (do shell script "cat /Users/Johann/Desktop/myLabelValue.txt")
        aTextLabel's setStringValue:MyFileTextContent
    end repeat
end applicationWillFinishLaunching_

但是显然这样行不通,但我不知道如何实现我的目标。

警告:我不写 AppleScripts。但这适用于简单的情况。请注意,tail -f 的大多数(所有?)实现使用延迟,然后统计文件以进行更改,然后如果有更改,它会读取文件。所以保留一个变量来显示旧内容并对其进行测试。

on applicationWillFinishLaunching_(aNotification)
    set oldContents to ""
    repeat
    set MyFileTextContent to (do shell script "tail  /Users/Johann/Desktop/myLabelValue.txt")
    set aTextLabel to MyFileTextContent
    if MyFileTextContent is not equal to oldContents
        set oldContents to MyFileTextContent
        set aTextField to MyFileTextContent
    end if
    delay 5
    end repeat
end applicationWillFinishLaunching

您需要调整aTextLabel 的设置。您将需要尝试延迟。最后,请注意我使用的是尾巴而不是猫。根据你的needs._

调整

我只能用swift给你举个例子。希望这会让你走上正轨。这是一种使用 Timer.scheduledTimer 的更简单的方法。这是 ViewController 的完整代码。故事书应该有一个名为 fileContents 的标签 (NSTextField)。有一个开始和结束按钮。在桌面上创建一个名为 mytext.txt 的文本文件以进行测试。如果您在使用它时遇到问题,我可以 post github 上的整个 Xcode 项目。

//
//  ViewController.swift
//  monitorFile
//
import Cocoa

class ViewController: NSViewController {

    @IBOutlet weak var fileContents: NSTextField!
    var monitorTimer: Timer!

    var homeDir : URL!
    var desktopFile : URL!

    // This keeps us from updating the display when content didn't change
    var oldContent: String!

    override func viewDidLoad() {
        super.viewDidLoad()

        homeDir = FileManager.default.homeDirectoryForCurrentUser
        desktopFile = homeDir.appendingPathComponent("Desktop", isDirectory: true).appendingPathComponent("mytext").appendingPathExtension("txt")

    }

    override var representedObject: Any? {
        didSet {
        // Update the view, if already loaded.
        }
    }

    override func viewWillDisappear() {
        if (monitorTimer != nil) {
            endTimer()
        }
    }

    func startMonitor() {
        if (monitorTimer == nil) {
            // timeInterval is a double representing seconds
            monitorTimer = Timer.scheduledTimer(timeInterval: 3, target: self, selector: #selector(updateDisplay), userInfo: nil, repeats: true)
        }
    }

    @objc func updateDisplay() {
        let newContent = readIt(location: desktopFile)
        if (newContent != oldContent) {
            oldContent = newContent
            fileContents.stringValue = newContent
        }
    }

    func endTimer() {
        if (monitorTimer != nil) {
            monitorTimer.invalidate()
        }
    }

    func readIt(location: URL) -> String {
        var contents = "No content"
        do {
            contents = try String(contentsOf: location)
        } catch {
            print("Failed reading from URL: \(desktopFile), Error: " + error.localizedDescription)
        }
        return contents
    }

    @IBAction func stopMonitor(_ sender: NSButton) {
        endTimer()
    }

    @IBAction func startMonitor(_ sender: NSButton) {
        startMonitor()
    }
}

解决方法:

property NSTimer : class "NSTimer"
property myTextField : missing value

on log1:sender
    performSelector_withObject_afterDelay_("log1:", missing value, 1.0)
    set myText to (do shell script "cat texFile.txt")
    myTextField's setStringValue:myText
end log1:

on applicationWillFinishLaunching:aNotification
    performSelector_withObject_afterDelay_("log1:", me, 0)
end applicationWillFinishLaunching: