Xcode AppleScript:更改变量内容的标签

Xcode AppleScript : change label which content of variable

这是我的脚本:

property MyLabel : missing value

on buttonClicked_(sender)
    set the plistfile_path to "~/Desktop/MY_DATA.plist"
    tell application "System Events"
        set p_list to property list file (plistfile_path)
        -- read the plist data
        set theMyDataFromPlist to value of property list item "DATA1" of p_list

         end tell
end buttonClicked_

This gonna take the data I want from a plist and set in a variable (theMyDataFromPlist).

如何在标签上打印此变量 "MyLabel" 并使脚本在数据更改时自动刷新?

另外,当我点击文本(或其他按钮)时,我可以将值复制到剪贴板吗? (set the clipboard to theMyDataFromPlist 有用吗?)

我也想知道是否可以对 Swift 做同样的事情?

How can I print this variable on the label "MyLabel"

您想在哪里 打印 变量?您可以让 AppleScript 显示对话框:

set variable to 42
display dialog "The value of variable is " & variable

AppleScript 不是作为终端语言设计的,它是为 UI 脚本编写的,所以它不像大多数脚本语言那样只提供打印,因为用户会在哪里看到打印的值。

and make the script auto refresh when the data change ?

不太确定您在这里期待什么。每当文件内容发生变化时,您的系统都会神奇地 运行 编写您的脚本?那不会发生。您可以创建一个 launchd 作业并让 launchd 监视该文件,然后在文件更改时执行您的脚本;这在这里描述:

https://discussions.apple.com/message/13182902#13182902

但是某些进程必须监视文件,如果您的脚本应该这样做,它必须一直 运行ning,不间断。然后你可以每隔 X 秒编写一些代码 运行,检查文件的最后修改日期,每当更改时,重新读取 plist。这个轮询非常丑陋,但是 AS 可以开箱即用的最好的事情。

顺便说一句,剩下的在哪里?你说

also, when I click on the text (or another button), can I copy the value to the clipboard.

哪个文本?哪个按钮?听起来你有一个完整的应用程序,但你向我们展示的只是 11 行脚本代码。您甚至没有提到您有一个带有 UI 的整个应用程序。你的问题以 "Here is my script" 开头,所以你听起来好像只有这 11 行。

(would set the clipboard to theMyDataFromPlist work?)

你为什么不简单地尝试一下呢?将该行粘贴到 ScriptEditor 中所花的时间与提出该问题所花的时间相同。刚试了一下,原来只能设置字符串。

此代码无效:

-- bad code
set variable to 42
set the clipboard to variable

但这段代码确实有效:

-- good code
set variable to 42
set the clipboard to "" & variable

I also wonder is that possible to do the same with Swift?

就我个人而言,我什至不会考虑用 AppleScript 编写应用程序;在我这样做之前,我宁愿停止编写代码。当然这可以在 Swift 或 Obj-C 中完成。您在 AS 中可以做的一切都可以用其他两种语言完成,不,反之则不成立。

使用 Obj-C 或 Swift,您还可以使用 GCD,使用 GCD 监视文件的更改很容易。看看