TITLE
    AppleScript: Using Network Setup Scripting in Mac OS 8.5
Article ID:
Created:
Modified:
30829
11/10/98
11/2/00

TOPIC

    How do I use AppleScript to switch between named configurations of AppleTalk or TCP/IP.


DISCUSSION

    What follows is a series of AppleScript samples that should answer the above question as well as explain how to use some of the facilities of Network Setup Scripting.

    There are five control panels, relating to networking, that are capable of creating named configurations - AppleTalk, TCP/IP, Modem, Remote Access, and InfraRed (if applicable to your computer). All of these named configurations are maintained in a database. Each of these control panels can be defined to have multiple configurations, by selecting Configurations from the File menu (or typing Command-K) and creating a new configuration using the duplicate button.

    There are three basic entities in Network Setup Scripting: transport options, configurations, and configuration sets. Here's an AppleScript that will acquire the settings for any computer:

    --Begin AppleScript
    tell application "Network Setup Scripting"

    --basic acquisition of information
    open database
    get every transport options


    --These are the named config items for AppleTalk, TCP/IP, Modem and Remote Access Control Panels
    get configurations
    --> The response here will depend on the configurations defined for the computer


    get configuration sets
    close database

    end tell
    --End AppleScript


    To see the results of these database queries, open the Event log window of the Script Editor, check "Show Event Results" and watch the execution results of the script in this window.

    To make changes to the database, you must perform the operations within the scope of a transaction. These transactions are defined by AppleScript verbs of Network Setup Scripting. To protect the integrity of the database, it is suggested that an AppleScript try block be used to recover from any errors encountered. A model error wrapper could be written as follows:

    --Begin AppleScript
    tell application "Network Setup Scripting"

    try
    open database
    begin transaction

    (*
    Include commands here to make changes to the database via AppleScript

    *)
    end transaction
    close database
    log ("Normal completion")

    on error ErrMsg number ErrNmbr
    log ("Error " & ErrNmbr & ": " & ErrMsg)
    abort transaction
    close database
    log ("Error encountered")

    end try
    end tell
    --End AppleScript



    Now with the basic groundwork for syntax shown, AppleScripts can be developed to switch between configuration sets that you have created in each of the Control Panels. In the scripts that follow, the configuration names in double quotes are example names and must be changed to match the named configurations used on the computer where the scripts are run.

    Here's is a simplified example to switch between AppleTalk configurations.

    --Changing a named AppleTalk configuration
    --Begin AppleScript
    tell application "Network Setup Scripting"

    try
    open database
    begin transaction
    set active of AppleTalk configuration "EtherTalk" to true
    --set active of AppleTalk configuration "LocalTalk" to true
    end transaction
    close database

    on error
    abort transaction
    close database

    end try
    end tell
    --End AppleScript



    Here's is a simplified example to switch between TCP/IP configurations.

    --Changing a named TCP/IP configuration
    --Begin AppleScript
    tell application "Network Setup Scripting"

    try
    open database
    begin transaction
    set active of TCPIP v4 configuration "Fixed IP Address" to true
    --set active of TCPIP v4 configuration "MacIP" to true
    end transaction
    close database

    on error
    abort transaction
    close database

    end try
    end tell
    --End AppleScript



    Here's is a simplified example to switch between Modem configurations.

    --Changing a named Modem configuration
    --Begin AppleScript
    tell application "Network Setup Scripting"

    try
    open database
    begin transaction
    set active of modem configuration "GeoPort" to true
    --set active of modem configuration "ISDN" to true
    end transaction
    close database

    on error
    abort transaction
    close database

    end try
    end tell
    --End AppleScript



    Here's is a simplified example to switch between Remote Access configurations.

    --Changing a named Remote Access configuration
    --Begin AppleScript
    tell application "Network Setup Scripting"

    try
    open database
    begin transaction
    set active of Remote Access configuration "Dial Apple ARA" to true
    --set active of Remote Access configuration "Dial Home ARA" to true
    end transaction
    close database

    on error
    abort transaction
    close database

    end try
    end tell
    --End AppleScript



    Here's is a simplified example to switch between InfraRed configurations.

    --Changing a named Infrared configuration
    --Begin AppleScript
    tell application "Network Setup Scripting"

    try
    open database
    begin transaction
    set active of Infrared configuration "IRTalk" to true
    --set active of Infrared configuration "IrDA" to true
    end transaction
    close database

    on error
    abort transaction
    close database

    end try
    end tell
    --End AppleScript


    As an alternative, consider using the Location Manager to group several configuration changes. Sets can be defined within the Location Manager which involve changing several of the items mentioned above. By 'Scripting the Location Manager', you can have several network items changed as a group with a new location setting.

    For information about Scripting the Location Manager see article 24586: " AppleScript: Location Manager 2.0 Scripting "

Document Information
Product Area: Apple Software
Category: AppleScript
Sub Category: General Topics
Keywords: kmos85

Copyright © 2000 Apple Computer, Inc. All rights reserved.