Author Topic: Lesson #2.1 : Objects  (Read 1797 times)

0 Members and 1 Guest are viewing this topic.

Offline Publi-Script

  • Administrator
  • Hero Member
  • *****
  • Posts: 596
  • Karma: 16
  • Gender: Male
  • Automate your Life!
    • Publi-Script.com
Lesson #2.1 : Objects
« on: May 08, 2007, 03:40:57 PM »


2.1 Objects

Objects are relatively new to the programming scene. More and more programming languages adopt a DOM (Document Object Model) architecture making things much easier IMHO to the programmers. While AppleScript is not a true Object Oriented Programming language (OOP), it does includes many of its abilities.


What is an object?

An object is an entity that may:
  • Contain other objects.
  • Be defined by set of properties.
  • Perform certain tasks.

Here a quick DOM related to your day to day life:

Quote
Class: family
  • Properties:
    • Name string the name of your family
    • Size integer the number of member
  • Elements:
    • Class: member
    • Class: pet
  • Commands:
    • add

Class: member
  • Properties:
    • index integer
    • last string the last name of this member
    • first string the first name of this member
    • age real the age of this member
    • dob date the date of birth of this member
  • Elements:
    • Class: head
    • Class: arm
    • Class: leg
  • Commands:
    • clean
    • talk
    • walk

Of course this one can go on and on but I believe this will be enough to illustrate the concept.


How do we make use of a DOM?

Well in your family, if you ask William to take out the trash (assuming there IS a William in your family) you would expect William to react and accomplish the task (to do it without whining would be asking too much!). That said, if your family works like mine, simply telling anyone to take out the trash is a sure way to find it untouched the next day. It becomes evident that telling William specifically is very important if you really the trash taken out. To that avail, we each have a name to identify ourselves and we use this "property" every day.

On the other hand, when you ask William to take out the trash, you also assume that William knows what “Take out” means as well as what “Trash” is. You would normally and instinctively make this clear in your request.

With any OOP, such interaction is also possible but I’ll be honest with you, this can be more or less “instinctive”. In fact you will find a lot of your mistake revolving around “scope” or not not targetting the right object with the appropriate command.

One way to look at scope is to see it a container. The member William exists in YOUR family but might not in another. By the same token the member head does not exists in your family but head of William does because William is part of your family and head is a part of William.

The major bonus of using a DOM is that you can move from parent to child or from child to parent as you see fit. So if you are given a valid object (by valid I mean that the object is known and exists) you can determine its parent and/or its child(s)

Getting back to AppleScript programming per say, let’s look at an excerpt of Quark’s dictionary (To open an application dictionary , select Open Dictionary from the File menu of your Script Editor and point to the actual application executable.)

Quote
application (noun)An application program
properties

Property    Access    Type    Description
auto backup    get/set    boolean    whether the auto backup option is on
auto lib save    get/set    boolean    save changes to a library automatically whenever an entry is added
auto save    get/set    boolean    whether auto save is on
auto save interval    get/set    fixed    time (in minutes) between each auto save (0.25 - 10,000)
backup destination    get/set    alias    destination folder for backup files
best type    get    type class    the best descriptor type
class    get    type class    the class descriptor type
color TIFF resolution    get/set    color TIFF resolution    resolution for color TIFF images
colors showing    get/set    boolean    whether the colors palette is showing
convert quotes    get/set    boolean    convert to typographer’s quotes when importing text
current box    get/set    reference    currently selected box
default type    get    type class    the default descriptor type
doc layout showing    get/set    boolean    whether the document layout palette is showing
drag and drop text    get/set    boolean    allow drag and drop editing
font list    get    list of font record    list of fonts available to the application
frontmost    get    boolean    whether this is the frontmost application
gray TIFF resolution    get/set    gray TIFF resolution    resolution for gray scale TIFF images
grid guide color    get/set    color    color of grid guides
import styles    get/set    boolean    import style tags when importing text
language    get/set    language    program language
live scroll    get/set    boolean    perform live scrolling
margin guide color    get/set    color    color of margin guides
maximize document bounds    get/set    boolean    maximize document boundary when zooming or tiling
measurements showing    get/set    boolean    whether the measurements palette is showing
name    get    string    name of this application
object reference    get    reference    an object reference for this object
offscreen draw    get/set    boolean    whether off screen drawing is on
opaque text box editing    get/set    boolean    whether a text box turns opaque when clicking in it for editing
open document preference    get/set    open document preference    document settings to use on opening
pasteboard width    get/set    percent    width of the pasteboard (in percent)
picture import filters    get    list    names of the picture import filters
properties    get/set    record    allows getting a list of all properties
quote types    get/set    small integer    1 = “”, 2 = ””, 3 = „“, 4 = «», 5 = »«
ruler guide color    get/set    color    color of ruler guides
save document position    get/set    boolean    remember document size
selection    get/set    selection object    the selection visible to the user
show tool tips    get/set    boolean    display tool names
show xt manager    get/set    show xt manager    when to show XTension Manager
smart quotes    get/set    boolean    convert standard quotes to typographer’s quotes
speed scroll    get/set    boolean    whether speed scroll is enabled or disabled
style sheets showing    get/set    boolean    whether the style sheets palette is showing
text export filters    get    list    names of the text export filters
text import filters    get    list    names of the text import filters
tile to multiple monitors    get/set    boolean    tile documents to multiple monitors
tools showing    get/set    boolean    whether the tools palette is showing
total backups    get/set    small integer    total number of backups to keep (1 - 100)
trap information showing    get/set    boolean    whether the trap Information palette is showing
version    get    vers    the version of the application

elements
Element    Access    Key Forms
color system    get/ make/ delete    by name, by index
default document    get/ make/ delete    by index
delimit table    get/ make/ delete    by index
document    get/ make/ delete    by name, by index, relative to others, by whose/where filter
file    get/ make/ delete    by name
menu    get/ make/ delete    by name, by index, relative to others, by whose/where filter and by unique ID
project    get/ make/ delete    by name, by index, relative to others, by whose/where filter
window    get/ make/ delete    by name, by index, relative to others, by whose/where filter
xtension    get/ make/ delete    by name, by index, relative to others, by whose/where filter and by unique ID

The above is Quark’s definition of its application class.

BTW, I would highly suggest the application class to be your starting point with ANY application you wish to script.

So, here you can see that the application class contains just a handful of elements. It will be up to you find the proper element (or container) that will hold the element you wish to target.
For instance, if you wish to access page 6 of the current document, you have a couple of avenues available to you. You should start by asking the question: Where would a page belong?

The likeable subjects in that short list would be  default document, document and project.

Applescript dictionary being all hyperlinked you should only need to click on the desired class to access its definition. However, I just realized that Script Editor doesn’t seem to work like this in every case. Another a way of accessing a class definition is to type it in the search bar and then select  the appropriate entry in the search results window.

You will soon find out that default document does not contain a page element or anything close to it. On the other hand, document does. (Note that you can also find the pages class within the project class if you look within its layout space class.) This simply tells you that in order to access page 6, you will first have to “talk” to the application which in turn will “talk” to its document object that will in turn “talk” to its page object.

This translate to the following AppleScript code:

Quote
tell application "QuarkXPress Passport 7"
     tell document 1
          tell page 6
               
          end tell
     end tell
end tell

Note that an identifier was added to each class ensuring you are targeting the proper object. (Script Editor does not display this "identifier" but Script Debugger adds this info to its dictionary display, we will talk about this further in another lesson).

Navigating through an Application dictionary can be very frustrating but know that it contains a wealth of information. Different script editor will display this information in different way so I can only suggest you carefully test them all and use the one that pleases you the most


« Last Edit: May 09, 2007, 03:01:23 PM by Publi-Script »
Michel Lemieux
Forum Administrator
-------------------------
OS X 10.6.4
Quark 6.5 / 7.5 / 8.1.6
Adobe Design Premium CS / CS2 / CS3 / CS4 / CS5
Script Debugger 4.5