Login Script

Joined
Aug 13, 2009
Messages
13
Reaction score
0
Points
1
I have created an AppleScript which I intend to run automatically whenever a user logs in.

It must run for each user, as it requires their username to create a folder with their username as its name on an external drive, so running it from the /Library/StartupItems folder is not an option.

Adding it to the System Preferences/Accounts/Login Items is also not viable, since the computers are on a network with over 1000 users, so that would take ages to do on each of the 21 computers.

Is there a way I can get the .scpt or .app file to run during the login process? I have tried creating a logon hook, but that didn't work.
 
Joined
Aug 3, 2009
Messages
1,562
Reaction score
39
Points
48
Location
The Netherlands
Did you try the login hook in Terminal?

And why didn't that work? Did you get an error message?
If you did...What did it say?
 
OP
C
Joined
Aug 13, 2009
Messages
13
Reaction score
0
Points
1
I added the loginhook through terminal, as per the instructions in the following link:
Mac OS X: Creating a login hook

The AppleScript code is:
Code:
tell current application
	set userName to do shell script "whoami"
end tell
tell application "Finder"
	--Check that the Safe folder exists
	set drv to "Drive01"
	if not (exists folder (drv & ":Safe")) then
		make new folder at drv with properties {name:("Safe")}
	end if
	set safe to drv & ":Safe"
	set posixSafe to "/Volumes/" & drv & "/Safe"
	tell current application
		do shell script ("chmod 777 " & posixSafe)
	end tell
	--Move other folders on drive to Safe folder
	set folderList to {}
	repeat with currentFolder in disk drv
		--Check name of folder
		if not (get name of currentFolder = "Safe") then
			--Add to move list
			copy name of currentFolder to end of folderList
		end if
	end repeat
	--Get length of list
	set folderLength to length of folderList
	--Cycle through list and move unwanted folders
	repeat with i in folderList
		move folder (drv & ":" & i) to folder (safe)
	end repeat
	--Check that the user folder exists in the safe folder
	if not (exists folder (safe & ":" & userName)) then
		--Create the user folder in drv
		make new folder at drv with properties {name:(userName)}
	else
		--Move the user folder from safe to drv
		move folder (safe & ":" & userName) to folder (drv)
	end if
	if not (userName = "admin") then
		--Set denied permissions on the safe folder
		tell current application
			do shell script ("chmod 770 " & posixSafe)
		end tell
	end if
end tell

There is no error message for the .scpt file when it is set up as a loginhook. Nothing happens to the folders in the drive "Drive01".

The error message for the .app file when used as a loginhook is:
Code:
Can't get some object

Which doesn't look like a helpful error message, but if somebody knows what it means I'll be eternally greatful.
 
OP
C
Joined
Aug 13, 2009
Messages
13
Reaction score
0
Points
1
I think I have figured it out - the app is being run by the loginhook BEFORE the drive is mounted.

If I can get the .app to autorun when the drive is mounted, that would do what I need it to do, but I hear OS X does not have a built-in autorun feature.

Is there any way to run an app or scpt from the built in hard drive when a USB storage device is mounted?

Or is there any way to mount the drive before the user logs in?

----------------------------------------edit---------------------------------------

I have found out how to mount the USB drive before login at the following url:
Automatically Mount a USB Drive Before Login on Mac OS X

The app now runs in the loginhook without errors, but it is using the root user to create a folder on the drive, instead of the logged-in user.

I'll keep searching for an alternative to the "whoami" command, but I've almost got this sorted out.
 

Shop Amazon


Shop for your Apple, Mac, iPhone and other computer products on Amazon.
We are a participant in the Amazon Services LLC Associates Program, an affiliate program designed to provide a means for us to earn fees by linking to Amazon and affiliated sites.
Top