Search code examples
applescript

How to learn Applescript


I'm new in programming. I can write some Python code and I would like to write some script on a Macbook with the Script Editor.

So what is the best way to learn AppleScript? What material do I need?

I have tried to learn AppleScript with the language guide and Window Anatomy.

But when I tried to read some code, I can't understand many of the statements.

Example:

tell application "System Preferences"
    activate
    set current pane to pane "com.apple.preference.sound" --I can't understand "com.apple.preference.sound".What's the rule of strings like this?
end tell
tell application "System Events"
    tell application process "System Preferences"
        tell tab group of window "Sound"
            
            click radio button "output" --There,I don't exactly know what's radio button,pane,table is.
            
            if (selected of row 1 of table 1 of scroll area 1) then
                set selected of row 2 of table 1 of scroll area 1 to true
                set deviceselected to "外置音响"
            else if (selected of row 2 of table 1 of scroll area 1) then
                set selected of row 3 of table 1 of scroll area 1 to true
                set deviceselected to "LG"
            else
                set selected of row 1 of table 1 of scroll area 1 to true
                set deviceselected to "MAC"
            end if
        end tell
    end tell
end tell

It seems I don't know these concepts outside of the language syntax. How can I learn these things?


Solution

    1. The string is a string (a sequence of characters; surrounded by quote marks when written in literal—code—form). The content of that particular string is a Uniform Type Identifier which is just a naming convention, much like domain names in URLs except backwards.

    2. Knowing what the various GUI widgets are called takes a passing familiarity with GUI programming. e.g. Radio buttons are the circular buttons which come in groups where you can pick any one. There is an helpful app for GUI Scripting if you really, really need it. Also, GUI Scripting is a klunky fragile brittle pig and only used as the last resort when apps don’t provide a scripting interface of their own. Which brings us to a more general point…

    3. Knowing what the various types of objects that exist in an AppleScriptable application are called, plus what information they contain and how they can be manipulated with commands, requires reading that application’s AppleScript dictionary (File > Open Dictionary in Script Editor) plus some intelligent guesswork (since app dictionaries are almost always famously inadequate on details). A good AppleScript book will help you to develop that skill, and will save you a lot of head-scratching and time in the long run. (I was lead author on @BernardR’s book #2, btw.)

    4. If you are serious about learning AppleScript, I strongly recommend you get Script Debugger. It is far superior to Apple’s Script Editor not only for exploring application dictionaries but also the live objects within the running application itself. The user-to-user forum there is also good for AppleScript-specific advice.

    5. Lastly, if you are familiar with an Object-Oriented Programming language such as Python, you will find AppleScript uses a lot of the same jargon when talking about scriptable applications: classes, objects, etc. Do not be fooled. Application scripting (Apple event IPC) is not OOP: it is remote procedure calls which pass simple queries as arguments. The queries identify the data you wish a command to operate on (e.g. size of first word of every paragraph of front document) and the command tells the application what to do with whatever data it finds (e.g. set (size of…) to 18). Thus in AppleScript you can perform operations such as:

      tell application "TextEdit" set size of first word of every paragraph of front document to 18 end tell

    which is not a concept you can express in OOP; it’s a lot more powerful than that. (BTW, this is why programmers hate AppleScript: because they incorrectly assume application scripting is OOP; then when it behaves in some profoundly non-OOP way it confuses the hell out of them.)

    --

    p.s. Good Luck. You will need it, as it is awesome and frustrating in equal measure. (Although the awesome parts usually make it worth all the hair pulling it takes in getting there.)