4

I'm attempting to script BBEdit to make me feel more at home in coming from TextMate. One thing I need to be able to do is see if an object I have a reference to has a particular property.

For instance:

tell application "BBEdit"
    tell front window
        get selected items
    end tell
end tell

This will succeed on a project window, but not on a disk browser window, because the latter does not have a 'selected items' property. How do I see if there is such a property in the object?

Please note: I know how to inspect an object in Script Editor (get properties) to see what properties it has, but I need to know at runtime what they are.

Matthew Schinckel
  • 32,344
  • 6
  • 71
  • 109
  • Please note: I am not after the selection: that is easy, as every document has that property. This is `selected items`, which only a subset of _window_ types have. – Matthew Schinckel Aug 05 '11 at 23:55
  • Some helpful related question: https://stackoverflow.com/questions/37638203/applescript-get-value-of-property-by-string-name-of-property – basil Nov 13 '18 at 09:10

4 Answers4

1

I don't have bbedit so I can't check, but if different types of windows exist, and each type of window has different properties, then can't you just check the window type first? Then you would know what type of properties you can get. There must be some basic property of a window that tells you its type or kind or whatever that would help you make the decision.

regulus6633
  • 17,963
  • 5
  • 37
  • 47
  • Because every kind of window inherits from `window`, I can fetch the frontmost `window`, but don't know the kind of window. I can't see a property that tells you the kind. I guess I'm coming from the python-esque approach of duck-typing: I don't care about what type of window it is: I care if it has a `selected items` attribute. – Matthew Schinckel Aug 05 '11 at 23:54
  • Also, supporting the last comment: I don't want to have to know every type of window that has a `selected items` attribute: I want to know if the frontmost window has it. Having to hard-code in the types of window that have it will mean more work if BBEdit adds in more types of window that have this property. – Matthew Schinckel Aug 05 '11 at 23:57
1

What about the class?

tell application "BBEdit"
  if class of window 1 is disk browser window then
    # ...
  else
    # ...
  end if
end tell
0

The only solution I have so far is to wrap it in an error handler:

try
    set sel to selected items
on error errMsg number errNum
    if errNum is -1700 then
        -- Code that handles no selected items attribute
        return
    end
    error errMsg number errNum
end try
-- Code that handles when selected items attribute exists
Matthew Schinckel
  • 32,344
  • 6
  • 71
  • 109
0

There is a difference between documents and windows in BBEdit. Windows are an element of documents, but only windows have the selection property, so you can check the type of window first and avoid catching errors entirely (and make for cleaner code as a result).

Also, try using the selection property, which is hard property in BBEdit as opposed to "selected items" because selection will always return a usable object, even if only an insertion point.

Philip Regan
  • 4,848
  • 2
  • 22
  • 36
  • It is not the `selection` property I am after: it is `selected items`. This only appears on a certain subset of window kinds (project windows, for instance). – Matthew Schinckel Aug 05 '11 at 23:52