AppleScript:关于未定义变量的奇怪错误

发布于 2024-10-21 14:51:44 字数 1051 浏览 2 评论 0原文

有时,我会收到此错误:

./mac/get_foregroundapp_info.scpt:254:265:执行错误:变量 window_name 未定义。 (-2753)

这很奇怪,因为我看不到变量是如何未定义的。这是代码:

global frontApp, frontAppName, idleTime, window_name

tell application "System Events"
    set frontApp to first application process whose frontmost is true
end tell

set window_name to ""
try
    set window_name to name of front window of frontApp
end try
if window_name = missing value then
    set window_name to ""
end if

错误出现在 if window_name ... 行中。

但这种情况很少出现,大约只占 2-5% 的病例。

当我按住 Command 键并将鼠标悬停在 Chrome 中的某些链接上时,它似乎更频繁/总是(?)出现。不知道现在这是否是完全随机的行为或以任何方式相关。


编辑:当我用预览打开图像时,它似乎总是出现。奇怪的是,如果我用预览打开 PDF,它似乎可以工作。


正如 Chuck 指出的,可以使用另一个 try 块捕获此“未定义变量”错误。

但我的主要问题实际上是:为什么变量未定义?这意味着什么?因为在我看来,它必须在那个地方定义。


(顺便说一句,这段关于获取窗口标题的代码有几个缺点。其中之一已在此处描述。因此,它也并不总是有效。请参阅另一个解决方案 这里似乎总是有效,而且也没有这种奇怪的错误。)

Sometimes, I get this error:

./mac/get_foregroundapp_info.scpt:254:265: execution error: The variable window_name is not defined. (-2753)

This is strange because I cannot see how the variable is not defined. This is the code:

global frontApp, frontAppName, idleTime, window_name

tell application "System Events"
    set frontApp to first application process whose frontmost is true
end tell

set window_name to ""
try
    set window_name to name of front window of frontApp
end try
if window_name = missing value then
    set window_name to ""
end if

The error appears in the line if window_name ....

But it appears only rarely, maybe in about 2-5% of the cases.

It seems to appear more often / always (?) when I keep the Command-key pressed down and mouse hold over some link in Chrome. No idea if that is completely random behavior now or in any way related.


Edit: It seems to appear also always when I have an image opened with Preview. Strangely it seems to work if I open a PDF with Preview.


As Chuck pointed out, it is possible to catch this 'undefined-variable'-error with another try block.

But my main question actually here is: Why is the variable undefined? What does that mean? Because as I see it, it must be defined at that place.


(Btw., this code about getting the window title has several drawbacks. One of them is described here. It also doesn't always work because of this. See another solution here which seems to always work and also without such strange errors.)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

回心转意 2024-10-28 14:51:44

听起来 try 块中出现了错误。尝试在其中放置一个错误处理程序或删除 try 块,因为现在它只会使变量无法定义,然后您将在下一行看到错误。

It sounds like an error is cropping up in the try block. Try putting an error handler in there or getting rid of the try block, because right now it will just make the variable not get defined and then you'll get the error you're seeing on the next line.

葮薆情 2024-10-28 14:51:44

虽然我不知道你为什么会收到错误,但你尝试过这样的事情吗?

try
    set window_name to name of front window of frontApp
on error
    set window_name to ""
end try

或更多与您已有的内容一致的内容

set window_name to ""
set window_name to name of front window of frontApp
try
    if window_name = missing value then set window_name to ""
on error
    set window_name to ""
end try

Although I don't know why you're getting the error, have you tried something like this?

try
    set window_name to name of front window of frontApp
on error
    set window_name to ""
end try

or more in keeping with what you already have

set window_name to ""
set window_name to name of front window of frontApp
try
    if window_name = missing value then set window_name to ""
on error
    set window_name to ""
end try
自控 2024-10-28 14:51:44

我找到了解决方案,这表明“错误-2753”是一个假错误。因为我的解决方案是找到并正确添加块

  tell application "PrettyApp"
  end tell --PrettyApp 

I find solution, that says me "Error -2753" is a fake error. Bacause my solution is find and added correctly block

  tell application "PrettyApp"
  end tell --PrettyApp 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文