Learning and Developing AppleScripts

Hey Bern,

Because Keyboard Maestro just runs the script as you've written it with no feedback.

(Unless you display the result in a window and have “Include Errors” checked in the gear menu of the Execute an AppleScript action.)

Script Editor and Script Debugger provide feedback on and results for code that is run and tells you when you've made an error.

In Script Editor look in the Result panel of the script window – and try opening the Log History window before running a script.

In Script Debugger there are quite a few more options:

View	>	Result & Variables Tab
View	>	Show Event Log
View	>	Show Event Log in Window
View	>	Show Event Log Result Explorer

Script	>	Show Result Explorer Window
Script	>	Event Log in Window

You should always write, proof, and debug AppleScript in a proper AppleScript editor.

Only then should you move your code to a Keyboard Maestro action.

-Chris

Chris @ccstone gave you a great, thorough answer.
But simply put, KM is NOT a scripting editor or IDE for coding.

Chris' bottom line is worth repeating:

Crystal clear and useful. Thank you.

BTW, did you come up with a 1st Applescripts book recommendation?

Bern

Learning AppleScript

  1. AppleScript: Beginner's Tutorial at macosxautomation.com
  2. Script Debugger 7 -- a full IDE for developing AppleScript.
    SD7 now comes with a 20-day free trial, and then will gracefully downgrade to the FREE SD7 Lite (which is still much better than Script Editor).

You can get lots of AppleScript help here in this forum, and in the Script Debugger Forum.

If you really want a book, I like, and have used (mostly before I discovered Ref #1, this book:
Learn AppleScript: The Comprehensive Guide to Scripting and Automation on Mac OS X, 3rd Edition, Kindle Edition by Hamish Sanderson (Author), Hanaan Rosenthal (Author)

Hey Bern,

After getting my hands on a copy I'm inclined to recommend “AppleScript 1-2-3” as a first book for scripting neophytes.

-Chris

Hi Chris,

Your reply reminds me of a Buddha story.

A mother brought her son before the Buddha and asked the Buddha to tell him not to eat candy. The Buddha told the mother to bring him back in two weeks. Upon returning two weeks later, the Buddha then lectured the boy not to eat candy. The woman of course thanked the Buddha and asked him why he had them come back two weeks later? The Buddha replied “As of two weeks ago I hadn't stopped eating candy".

Anyway, I greatly appreciate yours and all the others inputs!!

For less then $60 I’ve ordered the following used books on Amazon and eBay.

Learn AppleScript The Comprehensive Guide to Scripting and Automation on Mac OS (actually, this was a PDF I bought.)

The Tao of Applescript/Book and 2 Disks (Bmug's Guide to Macintosh Scripting)

AppleScript Programming for the Absolute Beginner by Jerry Lee Ford Jr. (2007-07-31)

AppleScript For Dummies 2e

Apple Training Series: AppleScript 1-2-3

AppleScript: The Definitive Guide, 2nd Edition, Neuburg, Matt

Danny Goodmans Applescript Handbook

Beginning AppleScript by Stephen G. Kochan

After throwing my hat over the fence with these, I scoured the Internet for materials. I came up with about 80 resources which I will scrub through and sort out. One of the best ones surprised me in that you did not offer it:

References For Learning & Using Applescript

This will be the first branch I’ll work my way through.

I’ll skim, dip, peruse and generally wrangle and wrestle my way thur this little forest of data I’ve collected until I have enough exposure to feel I have a handle on this language.

In working to get my hands around this NEW language, I'm discovering how very kinesthetic my learning style is. Simply collecting a large enough pile to figure out how to sort through will, I think, begin to give me an outline of the overall structure of this language which I feel I need before diving into the details of it. I need a kind of kinesthetic experience of the thing from which to generate the concepts as I don’t do as well just working conceptually. This kind of language learning seems very conceptual to me at this point. Perhaps when I get more of an overview and lay of the land I will be grounded enough to start feeling more comfortable and get interested in the details.

Thanks again for all the support! I'm off to see the wonderful Wizard of AS… Or as Bilbo said “I’m going on an Adventure…”

BTW, I’m offering all this noise, splashing around, and general fanfare in case there are other kinesthetic learner looking to get a purchase in climbing this mountain.

Good luck with your massive book collection! It appears as if you will be doing extensive research. :wink:

Or, it may overwhelm you and confuse you. There are so many different writing and teaching styles.

May I ask, have you tried just using the tutorial at
AppleScript: Beginner's Tutorial at macosxautomation.com?

I'm not sure you really need all of those other books, to be honest.
MacOSXautomation.com taught me the basics that I needed to know, in a very easy to learn manner.
While there is some good stuff in those books, there are many tips and tricks that are not in there, at least not presented in a manner you can easily find them.

So, here's my basic suggestion for learning AppleScript:

  1. Use the tutorial at MacOSXautomation.com, and be sure to actually write, test, and modify the examples provided there.
    • Even better, if one of the examples is close to a real-world need, use the example to build your own script.
  2. When you need help, just do a Google search something like this:
    AppleScript keyword(s) or phrase
    • Very often this will lead you to a very good example of what you are trying to do much faster than reading/search any book.
  3. If a Google search fails, then post your question either here, or better, in the Script Debugger Forum, which is also the best place for ASObjC questions.
  4. MacScripter.net is also a great resource to search, though often it will show up in the Google search
    • Of course you can also post questions there.

AppleScript vs JXA

Finally, I have to ask to you really want to learn AppleScript?

There is a great alternative, JavaScript for Automation (JXA), which uses the core JavaScript engine and syntax, and, IMO, is much easier to learn than AppleScript.

JavaScript has many, many built-in functions that cover basic needs, which AppleScript does NOT have. For example, extensive string functions, real objects, date functions, numeric functions, and most importantly, a really good RegEx engine with syntax that is easy to use. For more info see:
JXA Resources

Since we have really strayed off-topic, I am going to move all of these posts about learning AppleScript to a new topic.

2 Likes

I forgot about one more tool I use with AppleScript to get the exact syntax, and remember seldom used commands:
Dash API Documentation Browser and Code Snippet Manager

It is a great documentation browser and search tool for many languages, including AppleScript.

This has tempted me in the past to purchase it. If you had to choose a single example that illustrates how Dash aided your AppleScript, does anything come to mind ? (It's ok if not, it almost sounds like an interview question.)

@CJK, it is not so much just one command, as it is the totality of commands that I have used Dash to get more info on. I also find it useful to get JXA (JS) syntax when using an editor like Script Editor that does NOT provide any time of syntax auto-complete.

Dash has a free trial, so the best thing to evalue it is to try it.

1 Like

Dash! :open_mouth: waaay cool, thanks!

Now working through it. Nicely written and set out. Thank you!!

I'm finding that looking through multiple approaches to the same outcome provides supporting pieces missed by any one presentation. For example, in APPLESCRIPT FOR ABSOLUTE STARTERS By Bert Altenburg on the first page he says:

"Once you dive into the world of AppleScript, you’ll notice that the term ‘AppleScript’ is used quite loosely for three different concepts.

  • The AppleScript language: The English-like language which is used to give written instructions to your Mac;
  • An AppleScript: A series of instructions, a.k.a. a script, written in the AppleScript language; and
  • A part of the Mac operating system (Mac OS X), which actually reads an AppleScript and executes the instructions containing it.

In this book, if there is need to refer to one of these three concepts specifically, the following terms are used respectively:

  • The AppleScript language;
  • An AppleScript or a script (noun);
  • The AppleScript component of Mac OS X."

This I found to be really useful at the beginning as I start to put meaning to the initially occurring huge, dark, dense tangled mass of words I must become fluent in. What's worse (especially in the beginning) is the practice of using multiple words interchangeably :face_with_symbols_over_mouth:. While being comfortable with multiple ways to say things is cool AFTER you get the hang of things and the lay of the land somewhat in mind, getting multiple terms at the outset is NOT helpful and only makes pulling the tangles apart more difficult and forestalls understanding and clarity. Where the hell did I put that perfect world! :wink:

Of course everyone learns differently and for some, having multiple ways to say the same thing at the outset is highly supportive and not a hindrance so good thing there are multiple pathways!

I downloaded Script Debugger 7. Very cool and I haven't played with it enough to get over the initial overwhelm the interface leaves me with. I will keep dipping my toes in until I warm up to it. I suspect we'll become close friends as it looks to be way more interesting than Script Editor. That it has a free Lite mode makes it a no brainer and again, for me, having multiple approaches enhances my learning process and experience.

Thank you!!

1 Like

Googling “how to learn javascript for beginners” here’s some top hits:

The basics for controlling an application is to get an application object, execute methods of the object to get the needed information or the desired effect. You can get information about each application objects valid methods from the File > Open Dictionary menu in the Script Editor.


It is really simple and mostly self-explanatory. Let’s go over it together.


Firstly, we create a couple of variables that we will use frequently: currentApp for prompting something in the current application, finder for creating and moving files around and systemEvents for accessing to files.

The OS has a set of standard scripting additions that provide the ability to speak text, display user interaction dialogs, and more. To use them, an application must explicitly set the includeStandardAdditions flag to true. The third line stands for that. We’re declaring that we want to use these additions.
After that, we prompt a folder chooser panel to get the folder to be processed. Then we create a new folder in the same directory to move the processed files into it."


And from our own @DanThomas

I’ll tell you right up front - getting up to speed is a daunting task, because the documentation, when there actually is some, is extremely sparse. That’s why I’m hoping you’re willing to document your experience, here in this topic.


While JXA initially looks cool and capable of producing results fast and perhaps easier then via AppleScripting, getting into JXA from the perspective of not really knowing anything about scripting and given the seemingly little being offered directly for the rank novice isn't as approachable as I’m finding for AppleScripts.

I don’t have the necessary distinctions to compare the two languages to meaningfully evaluate whether it’s worth taking the perhaps shorter but seemingly more difficult climb to get going with JXA rather than AppleScripts. It seems AppleScripts was designed with learning it baked into its very structure. That gets a LOT of points from where I’m standing.

Of course the argument that “So what? When you get to the top of that mountain you’ll find that it’s not all that great compared to what you see on the JXA mountain” can be leveled and again I can’t effectively argue.

Do I really want to learn AppleScripts? No . I just want to do some really simple stuff like file some stupid receipts and make some notes and organize some information. Are the tools ALREADY available to do that? YES. And..

I could just read physical books, write on paper, and throw receipts into a shoe box like most people and rummage around looking for stuff when I want or need it but computers appeared and damn it I'm going to get it to do what I want it to do the way I want it to do it and now if that means learning all this coding language thenI will! Do I "want to", no.

Not really. :wink:

TLDR.

If you have an essential question or two in all of that, please post succinctly, and I will try to answer. :smile:

Nothing essential, just appreciations and external thinking for my own clarification that hopeful will benefit others considering joining the climb. Thank you.

Bern,

Most of the text you posted seem like quoted text to me. Please correct me if I’m wrong.

In case I am not wrong, please format quoted text as quoted text. And, BTW, don’t post too much in one chunk. People will not read it (me included).

Back to the topic, more or less:

I started to dive deeper into AppleScript around 2012 or so, quite some years earlier into shell scripting, but that mainly for very specific (occasional) tasks. I’m using Macs since 1994.

In my experience :

With AppleScript you can do many things, with AppleScript-ObjC you can do even more things. Never had a need for JXA. But I like it, because the JavaScript syntax is way easier than the damn AppleScript syntax (which, ironically, was planned as an “easy’ syntax with its similarities to natural languages.)

How to learn it (the “climb” as you name it):

If you are not pressed (by your chief or something), then just let it evolve: You’ll find things in your every-day life you want to automize. Then just do it. Thoroughly. I mean, that one task, thoroughly. Look up the resources on this forum or on StackExchange, etc.

By programming that one task you’ll learn more than you’ll learn with courses (IMHO).

The point is that you don’t have to “know” AppleScript. Nor any other language. However, it is good to know more of AppleScript, and of other languages. But it is also good to know just something about other languages, because this gives you the possibility to look at an object from different perspectives.

It’s a bit like with natural languages: The more languages you know (or practice) the better you understand your own language (this point is very important!) and the easier it will be to understand new ones (and to wrap around any things in general.)

Just my 3 cents

– Tom

Added quotation marks around quotes. Thought the preface to each quote made it clear the following as being quoted. I'll follow the agreed upon convention going forward. TY.

As for posting too much “The definition of genius is taking the complex and making it simple.” ― Albert Einstein

I'm working on it...

Nothing on fire so definitely taking the evolving route. I'm as interested in the process of learning AppleScripts as doing anything useful with it. It's a longer path that interests me.

I like your expression of doing one thing thoroughly. I phrase it "Doing complete work" and we're on the same page.

Definitely, multiple languages shed light on each other. This idea informs my choice to move among multiple sources.

"...JavaScript syntax is way easier than the damn AppleScript syntax..."

Could you give me an example or two? The tell x to do y of AppleScript seems clear and simple.

tell application "Finder"
   empty the trash
end tell

How would JXA or plain JS empty the trash?

Thanks Tom.

28 posts were split to a new topic: How to Quote Text and Script Code Blocks

Edited it to make it more approachable. Thank you :wink:

@BernSh and @Tom, since your extensive discussion about how to make forum quotes was considerably off-topic, I moved all of your related posts to a new topic in the Forum Admin section.