Running AppleScripts When Connected/Disconnected
Knowledge Base > Features > 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:
- Open Viscosity's Preferences window
- Select the connection you wish to add a script to and click the Edit button
- Click on the Advanced tab

- If you'd like an AppleScript script to run when your VPN connection becomes connected, click the Select button next to the "Connected Script" field. Likewise, if you want your script to run when your connection becomes disconnected, click the Select button next to the "Disconnected Script" field.

- Select your AppleScript script from the dialog window that appears and click the Open button. Please note that your script must be an AppleScript script. Shell scripts cannot be selected (please see the Common Tasks section below for tips on running shell scripts).
- Click the Save button
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:
- Open the AppleScript Editor application. This can be found at /Applications/Utilities/AppleScript Editor.app

- A new script editor window should appear. Input the following as a simple script:
display dialog "Test Script!" buttons "OK"

- Press the "Run" toolbar button to test your code. If everything is working a window should appear with the text "Text Script!".
- Go to the File menu and select Save
- 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.
- Now follow the steps in the section above to input the script into 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.
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.
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
|