2

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.)

Community
  • 1
  • 1
Albert
  • 65,406
  • 61
  • 242
  • 386
  • How are you executing this code? I could not think of any lightweight method of _not_ having AppleScript Editor being the frontmost app when testing the code. – Uriah Carpenter Mar 14 '11 at 01:33
  • @Uriah: I put this in some file and then on Terminal, I executed this: `while true; do sleep 1; osascript -ss myscript; done` – Albert Mar 14 '11 at 08:10

3 Answers3

1

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.

Chuck
  • 234,037
  • 30
  • 302
  • 389
0

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 
WINSergey
  • 1,977
  • 27
  • 39
0

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
Chuck
  • 4,662
  • 2
  • 33
  • 55
  • The second actually works because it seems to catch the 'undefined-variable' error. But my main question mostly is: Why is the variable undefined? What does that mean? Because as I see it, it must be defined at that place. – Albert Mar 14 '11 at 08:14
  • I don't immediately see why you're getting the error. I would have to debug the full script to attempt to figure that out. If you have Script Debugger, you might try using that. It has a demo download if you're interested. http://latenightsw.com – Chuck Mar 14 '11 at 21:02