Map Drive For Whoever Logs into Mac

Hi,

I need to set up a laboratory of 15 imacs and have one network drive map for when the students log on.
The imacs are bound to the domain and the students will be using their AD credentials.

I found the “Launch Login Items For All Users at Startup” script at

http://macscripter.net/viewtopic.php?id=33730

First of all I wrote the script to actually mount the drive and tested this.
This worked fine.

tell application “Finder”
try
mount volume “afp://10.0.151.11/Current%20Work”
end try
end tell

I then copied and pasted the code from the URL above into AppleScript Editor and compiled it.

global myName

on run
set launchItemsPath to (path to me as Unicode text) & “Contents:Resources:LaunchItems:”
set myName to getMyName()

logToSystemLog("Started Running login items...")
try
	-- make sure the folder exists and continue script if it does
	alias launchItemsPath
	
	tell application "Finder"
		set launchItems to (files of folder launchItemsPath) as alias list
	end tell
	
	repeat with anItem in launchItems
		try
			-- if its executable then we execute it
			set isExecutable to do shell script "if [ -x " & quoted form of POSIX path of anItem & " -a ! -d " & quoted form of POSIX path of anItem & " ]; then echo 'true';fi"
			if isExecutable is "true" then
				logToSystemLog("Executing: " & POSIX path of anItem)
				do shell script quoted form of POSIX path of anItem & " &"
			else
				-- if it's not executable we determine its kind
				tell application "Finder" to set theKind to kind of (contents of anItem)
				--return theKind
				if theKind is "Alias" then
					tell application "Finder" to set theKind to kind of (original item of (contents of anItem))
				end if
				
				-- we launch it based on its kind
				if theKind begins with "script" or theKind begins with "compiled script" then
					logToSystemLog("Running Applescript: " & POSIX path of anItem)
					run script (contents of anItem)
				else
					logToSystemLog("Opening: " & POSIX path of anItem)
					--tell application "Finder" to launch (contents of anItem)
					do shell script "/usr/bin/open " & quoted form of POSIX path of (contents of anItem)
				end if
			end if
		on error theError number errorNumber
			set errorText to "Error: " & theError & " Error Number: " & errorNumber
			logToSystemLog(errorText)
		end try
	end repeat
on error theError number errorNumber
	set errorText to "Error: " & theError & " Error Number: " & errorNumber
	logToSystemLog(errorText)
end try
logToSystemLog("Finished Running login items...")

end run

on logToSystemLog(theText)
do shell script "/usr/bin/logger " & quoted form of (myName & space & theText)
end logToSystemLog

on getMyName()
set myPath to path to me as Unicode text
if myPath ends with “:” then
set n to -2
else
set n to -1
end if
set AppleScript’s text item delimiters to “:”
set myName to text item n of myPath
if (myName contains “.”) then
set AppleScript’s text item delimiters to “.”
set myName to text 1 thru text item -2 of myName
end if
set AppleScript’s text item delimiters to “”
return myName
end getMyName

I then carried out the instructions below as mentioned.

– save this code as an application bundle
– create the folder “Contents:Resources:LaunchItems:” inside the application bundle
– place items you want launched in that folder

Once having completed the steps above, running the application manually works as expected however nothing happens when the mac is re-started.

Am I missing something? Do I have to ammend the script above or anything else so the OS knows to run this script on Startup?

The imacs are running 10.6.7. Will the script work on this version of the OS?

I have to admit that I was given this problem to solve but have never used AppleScript or any other scripting language before so apologies in advance for my ignorance. I have just ordered an AppleScript book so this should slowly change over the coming weeks and months.

Any help offered or any ideas on getting over this would be very much appreciated.

Kindest Regards

Neil

Model: imac
AppleScript: 2.3 (118)
Browser: Safari 533.20.27
Operating System: Mac OS X (10.6)

Hi, you didn’t read the instructions in the code just above the instructions you posted. Here they are.

-- I just basically converted the shell script found at the below website into applescript code
-- and added the ability to also run applescript bundles (scptd files). Look at the following
-- link for a methodology of running this applescript at startup for all users.
-- http://www.macenterprise.org/articles/runningitemsatlogin

The applescript code I provided runs stuff inside of it. You mentioned that works. Good. Now you have to get it to run at login. That’s another process and I mention in the instructions to follow the given link for that.

Tip 1: In the linked article go to the section “Pulling it all together with /Library/Preferences/loginwindow.plist”

Tip 2: your mount volume command in your code should be improved. You have that command inside of a “Tell application Finder” block of code. It is not a Finder command. Applescript can mount the volume, you do not need the Finder for this. As such remove the tell Finder line and end tell line. It will work better.

Hi Hank,

I have carried out your instructions and now the drive is being mapped successfully for whoever logs on.

I really cannot thank you enough for spending the time looking over my problem. You’re an absolute saviour.

I will spend some time looking through the beginners section of macsripter so I can familiarise myself with AppleScript a little more.

Thanks again Hank

Neil

Glad to help Neil. Good luck with the learning. That’s how I learned! Well… by doing the tutorials AND a lot of practice. :cool: