|
15 May 2008
One thing Mac OS X is good at is connecting to numerous types of networks. One thing Mac OS X is not good at is doing so easily all of the time. Sure Leopard has moved a great deal towards fixing this but it can still be a pain.
This tutorial will show you how to map to different network shares in Mac OS X. While it is been written for Mac OS X 10.5 (Leopard) users, it's pretty much the same for Mac OS 10.4 Tiger.
Connecting to a network share using Finder
Connecting to a network share using Finder is pretty easy if you
know how to do it. From Finder (click on your desktop to get to the Finder if you're unsure what it means), select the Go menu and
click Connect to Server.
You should see the following screen or something similar:
In the Server Address field type in the server you wish to connect to ensuring you have the correct protocol at the start, in this example I am connecting to a Windows network. The protocols are as listed below:
- AFP:// Apple Filing Protocol
- SMB:// Samba (Windows) network
- FTP:// File Transfer Protocol
- NFS:// Network File System
- HTTP:// HyperText Transfer Protocol
Everything you type after the "//" is the specific address for the server you want to connect to. Complete the details you need and click Connect. A connection dialogue window similar to the one below should appear.
Ensure Registered User is selected then enter your username and password. Under Tiger you may get a Domain field when connecting to a Windows network so enter whatever your domain is. Once these details are entered either press the Enter key on your keyboard or click Connect.
If you didn't enter what is known as the complete path to a folder you want to access (and just entered only the server) then you should see a box like this, listing the various volumes on the server:
This lists all the shares (folders/volumes) on the network so select the share you wish to connect to and click OK. One thing that tripped me up at first was that after doing this, the shares didn't show up on the Desktop. To fix this, follow this procedure.
In the Finder go to the Finder menu and choose Preferences
Ensure you are on the General tab of the Finder Preferences window that appears. Under Show these items on the Desktop you'll probably see that Connected servers is unticked. Tick this box and close the window.
You should now see the share mounted on the desktop and should be able to access it.
Mount a share on startup
All the above is fine, but it's a complete pain to map multiple shares as
you have to do this procedure for every share but you can alleviate this by
inserting into Login Items under system
preferences. To do this follow this process AFTER mounting the share as above.
Open System Preferences and click the Accounts tab and then click the Login Items tab
On the Desktop click and drag the share(s) you want to connect to automatically every time into the list with all the other items ('Deng' is the share used and added in the example below)
There are a couple of issues with this method. First it means having to follow the above procedures every time you do a rebuild of your system. Secondly, if you don't save your password to your keychain or your network password changes then you have to enter your password every time you log in. That's a pain for multiple shares. There is a much better way but it is more complex.
Connect using AppleScript
Open Finder and go to Applications. Under this directory there is a directory called AppleScript. Open this and run Script Editor. First we need to get some details of login IDs so as to make the script run as hands off as possible so enter this line in the editor window...
set username to do shell script "whoami"
You may have guessed what is happening here but I'll explain it so you understand. Set username to will create a variable called "username" which will be used later on in the script. The second half of the line - do shell script "whoami" - runs the UNIX command "whoami" which tells you who the currently logged in user is. In full the "username" will be set to the output of "whoami"
Note you can set this manually if you want by typing this...
set username to "lowededwookie"
The reason I initially used "whoami" is because if your local account is named the same as your network password then instead of manually entering the username into the script. This makes the script able to be used by multiple users without the need for editing the script to contain their login IDs.
You can get around this by altering the username variable to match that of the network like this:
set username to do shell script "whoami"
if username is "lowededwookie
set username to "darrynlowe"
end if
It's somewhat convoluted and still requires manual entry of login IDs but it can still be used by others who do have their local login matching their network login.
Next we need to set some variables. In this example I'm going to be setting up for a Windows network so I'm going to be setting up domain access and shares.
property pass : ""
"pass" is a variable that will be used for the password. We can't use "password" as a variable on account of "password" being an internal AppleScript command. Notice that there is nothing between the quotation marks? This sets "pass" to an empty value. We need it this way but I'll get to that shortly.
property network_share : ""
This is the variable we are going to use for the network mount. Once again it is undefined.
property domain : "winnet"
This creates the variable for the domain access. In this example I've defined the variable as "winnet". Note, if you are connecting to FTP, NFS, AFP, HTTP, etc. you do not need to set domain access only when using Windows networking (SMB://). Also, if you are connecting to a Windows machine that is just a local workgroup as opposed to a network you don't really have to set the domain either but it makes it easier if you do but in most cases the local workgroup will be defaulted to "workgroup" so use that as your variable definition (property domain : "workgroup").
property server : "winsrv"
Same as domain only this is the server you want to connect to.
property shared_directory : "share"
Like domain and server this is the share on the server you wish to connect to.
Now that all the variables have been defined it's time to do something with them.
First we need to speak to the application we are going to control, in this case it's Finder so we enter this:
tell application "Finder"
This starts AppleScript communicating with Finder. Everything from this point on will force Finder to perform actions.
if pass is "" then
Remember how "pass" was undefined above? We're now about to set the variable as something but we're going to set it using user interaction and we do that like so:
set dialog_1 to display dialog "Please enter your password:" default answer "" with hidden answer
This creates a dialogue box with an input field for you to enter some text. Note the with hidden answer at the end of the command? This will change the characters to the ••••• as you type thus hiding your password from everyone.
set the pass to the text returned of dialog_1
This sets the variable we called "pass" to whatever it is that you typed in the field.
end if
This stops the "if" command we just started.
Now to tie it all together.
set network_share to "smb://" & domain & ";" & username & ":" & pass & "@" & server & "/" & share_directory
Note that this is a single line and not a line break after the & on the first line.
This looks quite complex but some of it you'll recognise. First we are defining "network_share" and we are setting it to what is effectively this...
set network_share to "smb://domain;username:pass@server/share
Why don't we just do that? Simply because domain, username, pass, server, and share are all variables and the term variable refers to something that changes. True we set them manually in some cases but these variables can be reused as we will find out shortly. But to demystify the previous defining of the "network_share" variable we'll look at it closer.
So from "set network_share to" we see this "smb://" (note quotations) then we see & domain &. See how domain has no quotations but "smb://" does? Anything between quotations is treated like plain text. Anything without quotations is treated as a variable and as such will call the value of the variable and print that. For example we set the variable domain to "winnet" so in this case when the network_share is completed it will print "smb://winnet" and not "smb://" & domain &. You'll see the full results later. This format is the same throughout the whole string. "&" ties the plain text and the variables together. If you need a space between the two you'd use a space between the quotations.
To map a user's home drive on the network most Windows networks would use what's called a "dollar share" where the share name has a "$" symbol at the end of it. This hides the share so unless you know the share name you'll never be able to see it. We can use this line to set up a user's home drive like so:
set network_share to "smb://" & domain & ";" & username & ":" & pass & "@" & server & "/" & username & "$"
Remember how I said using variables allows for reuse? In this line we see the variable "username" used twice with the last one effectively being printed out as "lowededwookie$".
So let's carry on with the mapping:
if not (exists network_share) then
mount volume network_share
else
eject network_share
mount volume network_share
end if
This routine will check to see if a network_share has already been previously mounted. If it hasn't (if not (exists network_share) then) it will mount it. If it has already been mounted then it will disconnect it and then remount it.
To finish the entire script we use:
end tell
This tells Finder to finish running the script and exit.
You'll notice that both "tell" and "if" both have ends to them. This is because while the "tell" or "if" is open it will run everything. If you don't tell either to "end" then the script won't compile because it doesn't know what to do at the end.
One great thing about AppleScript is that it is very plain language to the extent that while it doesn't completely match English it is very readable to the average person. It is a logical set of instructions that will be processed very quickly in much the same way that we subconsciously process instructions.
To see if you were paying attention let's look at the entire script
and see if you can see what's going on. I'll use be spacing the lines so as to
remove confusion. If there is no space between the lines then this is to be
treated as a single line.
Download a zipped version of the complete script to review by clicking here
You'll notice that I used indents. I recommend doing this between things like "tell" and "if" as it makes it so much more readable and thus easier to trace problems that the editor may have missed (not very likely).
The beauty of this approach is that if your network password changes then you don't have to change the password for every mapping. You only enter the password once and the script handles the rest.
Once the script is done save it as an application. Simply choose File / Save from the menu and you'll see the save dialog. Change File Format to Application. For what we're about to do we need to make one more change. Notice the tick in Startup Screen? Untick this box.
Click Save.
Setting up this script to run automatically is exactly the same principle as earlier when we added mounts to Login Items only this time instead of a mount we drag the application we just made into the list.





