App Support.

We're here to help.



Running AppleScripts When Connected/Disconnected

Viscosity allows you to run custom AppleScript scripts when your connection connects or disconnects. This can allow you to automate common tasks, such as connecting to file servers, opening web pages, opening applications, and controlling any application that is AppleScriptable.

Selecting AppleScript Scripts To Use

Viscosity makes it easy to specify scripts to run when a connection connects or disconnects, and allows you to specify different scripts for each connection. You can specify a script like so:

  1. Open Viscosity's Preferences window
  2. Select the connection you wish to add a script to and click the Edit button
  3. Click the Advanced tab


  4. Click the Select button next to time you would like the script to run, browse to the script on your hard drive then click Open.
  5. Click Save.

Viscosity offers three times to run a script during the connection cycle. Before Connect will run a script right when you click to connect a connection. Connected will run a script when the connection is connected and active, and Disconnected will run a script when the connection has completed it's disconnect procedures.

Writing AppleScript Scripts

Writing your own AppleScript scripts is fairly easy thanks to Apple's AppleScript Editor application. Follow the instructions below to get started writing simple AppleScripts:

  1. Open the AppleScript Editor application. This can be found at /Applications/Utilities/AppleScript Editor.app

  2. A new script editor window should appear. Input the following as a simple script:

    tell application "Viscosity" to display dialog "Test Script!" buttons "OK"


  3. Press the "Run" toolbar button to test your code. If everything is working a window should appear with the text "Text Script!".
  4. Go to the File menu and select Save
  5. Select a location to save the script to, and give it a name. In this instance we are going to leave the File Format as Script. Click the Save button.
  6. Now follow the steps in the section above to input the script into Viscosity.

If your AppleScript interacts with the user interface it is necessary to associate such interaction with an application using AppleScript's "tell" command. For example while tell application "Viscosity" to display dialog "Test" and tell application "Finder" to display dialog "Test" will both work, simply putting display dialog "Test" will not. Please be aware that this is a change made in version 1.4.4 of Viscosity (versions prior to this always associated user interaction with Viscosity).

Common Tasks

The following are example AppleScript snippets for common tasks. These can be combined using the instructions above to create your Connected/Disconnected scripts.

Connect/Disconnect To A File Server

The following AppleScript can be used to connect to a AFP (Mac) file server. Place the following code in your Connected script.

tell application "Finder"
	mount volume "afp://serveraddress/sharename" as user name "username" with password "password"
end tell

Place the following code in your Disconnected script to disconnect the file server when the VPN connection is terminated.

tell application "Finder"
	eject "sharename"
end tell

Connect/Disconnect To A Windows File Server

The following AppleScript can be used to connect to a SMB/CIFS (Windows) file server. Place the following code in your Connected script.

tell application "Finder"
	mount volume "smb://serveraddress/sharename" as user name "username" with password "password"
end tell

Place the following code in your Disconnected script to disconnect the file server when the VPN connection is terminated.

tell application "Finder"
	eject "sharename"
end tell

Display A Message To The User

The following AppleScript can be used to display a message to the user. This could be used to display a welcome message to the user, instructions, or even Terms of Use. Place the following code in your Connected script.

tell application "Viscosity" to display dialog "Welcome Message" buttons "OK" default button "OK"

Open A Web Page

The following AppleScript will automatically open the user's default web browser and go to the specified web page. This could be used to automatically open a company's Intranet web page, display a welcome page, etc.

tell application "Finder" to open location "http://www.sparklabs.com.au"

Play A Sound

The following AppleScript will play a sound file. It can be pointed at the sounds included with Mac OS X, or pointed to a custom sound file.

set playsound to POSIX path of ("/System/Library/Sounds/Glass.aiff")
do shell script ("afplay " & playsound & " > /dev/null 2>&1 &")

Run A Shell Script

The following AppleScript will allow you to run a shell script. You can use this approach to run shell scripts instead of using OpenVPN's up/down commands (which are in use by Viscosity's DNS support).

do shell script "/path/to/script.sh"

By default AppleScript scripts have the same permissions as the user using Viscosity. If you need to explicitly run a shell script with administration rights, it can be done like so:

do shell script "/path/to/script.sh" user name "username" password "password" with administrator privileges