Strange behaviour of the “collapsed” property

Here is something that strangely looks like the [handler issue|http://discussions.apple.com/thread.jspa?threadID=2020008&tstart=0] reported by Noaks yesterday. The first script below returns the appropriate result for any one of the four statements within the tell block. However, when the same four statements are put into a handler, individually or altogether, as in the second script below, the last one of them produces an AppleScript error at run time. Both scripts were run from the Script Editor window.

tell application "iTunes"
number of windows --> 1
front window --> browser window id 4177 of application "iTunes"
bounds of front window --> {57, 120, 1073, 822}
collapsed of front window --> false
end tell

getProperty("iTunes")
on getProperty(theName)
tell application theName
number of windows --> 1
front window --> browser window id 4177 of application "iTunes"
bounds of front window --> {57, 120, 1073, 822}
collapsed of front window --> iTunes got an error: A descriptor type mismatch occurred.
end tell
end getProperty

When using the above scripts with the Finder (just replacing "iTunes" with "Finder"), the AppleScript error (from the second script) becomes: “Finder got an error: Can’t get |collapsed| of Finder window id 36.”

On the other hand, when using the scripts with Safari or Mail (this time replacing not only "iTunes" with "Safari" or "Mail", but also "collapsed" with "miniaturized"), no error occurs.

Does anyone know what's so special about the “collapsed” property?

15-inch MacBook Pro 2.53 GHz, Mac OS X (10.5.7), AirPort Extreme

Posted on May 29, 2009 9:35 AM

Reply
10 replies

May 29, 2009 10:39 AM in response to red_menace

Thanks for your reply. However, I really don't understand your explanation. First, it seems to me that the target of the tell statement is well defined, since the parameter theName correctly passes the value "iTunes", "Finder", "Safari" or "Mail" to the handler. If it were not the case, how could the handler always return the correct value of the “bounds” property? Secondly, how come the same handler, used within the same script, correctly returns the “miniaturized” property (the exact equivalent of the “collapsed” property in these applications) when used with Mail and Safari?

May 29, 2009 10:51 AM in response to Pierre L.

The target of the tell statement is not defined as far as the Script Editor is concerned - it has no idea what the contents of the theName variable is at compile time. My guess would be that without the Script Editor knowing the application to target, it is using it's own terminology, which happens to accidentally work in some cases (Script Editor windows have bounds and miniaturizable, but not collapsed properties).

May 29, 2009 12:24 PM in response to red_menace

I understand now that the issue has nothing to do with the “collapsed" property. You seem to be perfectly right when you say that the Script Editor uses its own terminology, which would explain why the script accidentally works in some cases. I have verified that the handler actually produces an error each time it uses a terminology that is unknown by the Script Editor.

However, what is not clear for me yet is why, if the Script Editor “has no idea what the contents of the theName variable is at compile time”, a script such as the one below always work correctly, even with many windows and applications open and even when the target application isn't the frontmost one.

moveFrontWindow("Finder")
on moveFrontWindow(theName) -- theName should pass the name of the target application
tell application theName
set x to 50
set {a, b, c, d} to bounds of front window
set bounds of front window to {a + x, b, c + x, d}
end tell
end moveFrontWindow

Thanks for your patience!

May 29, 2009 1:12 PM in response to Pierre L.

A statement such as tell application theName (with the variable set to "Finder", for example), works at runtime, when the terms of the various statements are evaluated. The Script Editor, however, doesn't know anything about your use of the variable (other than the syntax), or what it's contents mean (other than being some text) - it uses the terminology it knows at compile time.

In other words, suppose you had a list of application names (e.g. {"Finder", "Safari", "iTunes"}) and iterated trough them, calling your handler using the various items. It might be obvious to you and me, but all the Script Editor sees is a list of text items - it has no way of figuring out what you are doing with them. The results of the control statements that step through the list, various variable assignments, handler calls, etc, all get evaluated when the script is run, but not when it is being edited. This is why your scripts never run quite like you expect them to (well, mine sure don't, anyway), and some of them do stuff by accident.

Until machines get some intelligence, that is. And we all know how that turns out.

May 29, 2009 3:18 PM in response to Pierre L.

As a work around "using terms from" seems to get past this problem.

--START
set theApp to "iTunes"
getProps(theApp)

on getProps(theApp)
using terms from application "iTunes"
using terms from application "Finder"
tell application theApp
number of windows
front window
bounds of front window
collapsed of front window
end tell
end using terms from
end using terms from
end getProps
--END

May 29, 2009 11:12 PM in response to Pierre L.

Hello

Telling application by variable is possible but requires special care both in using application specific terminology and in resolving application reference at run-time.

In addition to using 'using terms from' compiler directive, you may use raw code for 'collapsed', that is «class wshd» in both Finder and iTunes.

By the way, the terms 'bounds' = «class pbnd» and 'window' = «class cwin» are both defined in AppleScript language core (as well as in Script Editor (v2 or later)'s own terminology dictionary that is exposed to user script at compile time), and therefore they are compiled correctly without special care.

cf.
http://developer.apple.com/releasenotes/AppleScript/ASTerminology_AppleEventCode s/TermsAndCodes.html

--E.g.
getWinProps("iTunes")
on getWinProps(appn)
set rr to {}
tell application appn
set end of rr to count windows
set end of rr to window 1
set end of rr to bounds of window 1
--set end of rr to collapsed of window 1 -- [1]
set end of rr to «class wshd» of window 1
end tell
return rr
(*
[1] "iTunes got an error: A descriptor type mismatch occurred."
(error number -10001 : errAETypeError)
This is because 'collapsed' is a mere string, not a valid property name.
*)
end getWinProps
--END OF E.g.



Apropos, you can make use of "AppleScript Formatting…" feature (via Edit menu of Script Editor, at least in my fosil environment) to distinguish Language/application keywords from user variable names by text appearance. This helps to know whether a token in question is treated as a keyword or a user variable by compiler.

Cheers,
H

May 30, 2009 6:23 AM in response to red_menace

Thanks a lot, red_menace. I think I finally understand what you meant, although I am still somewhat reluctant to admit that the two statements *tell application "Finder"* and *tell application theName* (where theName has been assigned the value "Finder") are not equivalent. There are still a lot of things that I don't clearly understand in AppleScript, or even in programming in general.

May 30, 2009 6:26 AM in response to Chachi

Many thanks for that information, Chachi. However, since my initial idea of using a variable with a “tell application” statement was in order to be able to use the handler with any application, it seems to me somewhat impractical to add a “using terms from” block to the handle for every possible application. Nevertheless, I greatly appreciate your suggestion, which helps me to better understand AppleScript programming.

May 30, 2009 7:57 AM in response to Hiroto

Hi Hiroto. As usual, your reply comes last but not least. Just replacing “collapsed” with «class wshd» had my second script work properly with iTunes (actually with any application which has got the "collapsed" property). I don't know anything of +raw code+, which sounds to me like “advanced theoretical programming”, but you have piqued my curiosity: I certainly will have a look at it. Thank your very much.

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

Strange behaviour of the “collapsed” property

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.