beginning AppleScript question

I'm a programmer so I shouldn't have any trouble understanding AppleScript, but I must be missing something fundamental. I wanted to write a script that types some text in a LibreOffice window, and according to many examples I have found on the web, I should write something like


tell application "LibreOffice" to keystroke "foo"


However this gets me the error "Can't get keystroke 'foo'"


I'm just a bit confused. Why is it trying to *get* something?

MacBook Pro (Retina, 15-inch, Mid 2015), OS X El Capitan (10.11.6)

Posted on Aug 9, 2016 4:55 PM

Reply
19 replies

Aug 9, 2016 10:19 PM in response to mike1127

I don't know if System Events can get the windows of an app that doesn't support AppleScript. The code works with TextEdit.

Word 2016 is scriptable and has a window class, but it does not return its windows when queried by System Events. It does if you query it directly. I don't know if that is a problem with System Events or with Word. Applications without an AppleScript Dictionary like Calculator and Stickies will provide window information to System Events.

Also, Graphic (non-Apple, non-scriptable), will respond with window info to System Events. I don't know why some apps work and others don't, but I suspect it might be related to whether the App supports built-in windows or rolls its own.


There is a standard set of supported things. It is the Standard Suite you can find any any Scriptable app dictionary.


If it has no windows then an empty set, {}, is completely accurate.

What is the set of windows in App "Foo?"

Why fail when you can provide a completely accurate response?

Aug 9, 2016 10:32 PM in response to mike1127

Typical programming syntax is simple and consistent and doesn't involve any redundant syntactical words, nor does it have multiple ways of doing things when one way would be simpler.

That's not the point of AppleScript. It is designed to provide the answer to your question regardless of the way you format the question.

get windows of application "TextEdit"

get every window of application "TextEdit"

get window 1 thru (count of windows of application "TextEdit") of application "TextEdit"

All provide the same result.


It's not about optimization or simplicity. It is designed to allow you to create a statement that makes sense to you.

I can easily filter the list with syntax that makes linguistic sense:

get name of windows of application "TextEdit" whose name begins with "Untitled"

Can you write a more concise statement in any other language which is as completely obvious as that?

Aug 10, 2016 12:31 AM in response to Barney-15E

Obviously there is going to be some disagreement on the type of scripting language. But let's say AppleScript is made for beginners. We want the following


(1) Beginners can easily read AppleScript

(2) Beginners can easily modify existing scripts to customize them

(3) Beginners can easily write brand new AppleScript


I will grant you that (1) is satisfied. It's easy to READ, yes.


But is "natural language" the best way to fulfill 2 and 3?


The fact there are different syntax forms for doing the same thing works against 3. It makes the language less consistent and more complicated.


There is a practical reason why you generally want one way of doing something. It's because it makes it easier to remember! As a beginner works with scripts, the proper syntax will sink in through repetition. This is Education 1-2-3. Actually it applies to experts too. I have been reading AppleScript for three days and I have seen so many different ways of doing the same thing that I probably don't even realize they are the same. That's really quite awful from an educational standpoint--two scripts that do the same thing but look different? Tell me with a straight face that makes it easy for beginners to figure out the language in a more deep way.


Second, natural language gives a superficial understanding but works against deep understanding. An important part of syntax is understanding the role that each identifier plays. In a traditional programming language, the role is almost always clear from the form of the statement. However, I am finding it difficult when scanning AppleScript to make out which are the reserved words, which relate more to statements, which relate more to field accessors or modifiers, which words are redundant, etc.


The "natural language" is trying to protect beginners from having to think about this, but is that really a good thing in the end? Don't beginners have to confront syntactical issues eventually?


How about those statements that require parentheses? They force a beginner to confront operator precedence rules. Those rules are "kind of sort of" in English but it doesn't have *consistency* in English. Therefore, the similarity of these syntactical constructions to natural language is doing nothing to train one's mind to have a consistent viewpoint and is probably doing harm.

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.

beginning AppleScript question

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