Start script at startup..........?

Hello,

I’m looking to find out how to run a script at startup. I have found this thread, but the second/last post seems to have some foreign characters in the code, and where do you store the script (roots home)?

thread reference = http://applescript.net/viewtopic.php?id=9054

Thanks
Dave

If your script is saved as an application, you could add it to your login items (System Preferencess>Accounts>User>Login Items) unless you want to avoid that and use the script you found to do it for you.

It assumes the script is saved as an app in your applications folder. This is what you’d expect that post to look like:

set the_path to POSIX path of (path to applications folder from local domain as string) & "App Name.app"
tell application "System Events"
	make login item at end with properties {path:the_path, hidden:false}
end tell

I rewrote the script to get the path to the chosen application:


display dialog "Choose an application to add to your Login Items."

repeat
	set theApp to name of (info for (choose file without invisibles))
	if theApp does not end with ".app" then
		display dialog "You must choose an application."
	else
		exit repeat
	end if
end repeat

set the_path to POSIX path of (path to applications folder from local domain as string) & theApp
tell application "System Events"
	make login item at end with properties {path:the_path, hidden:false}
end tell

I need the script to run before a user logs in, will this do the trick for me?

Thanks
Dave

What I’m looking for is a way to open an applescript application when the machine boots up. Is this possible?

Thanks
Dave

hi d,

have you checked out LoginWindow Manager?

you could probably run an AppleScript from that if you called it with osascript.

I was working on the script - see the edited post - hence the delayed reply.

Adding the AppleScript application to Login Items will mean that it runs when the user logs in, not when the machine boots - I dont think you can do that; I’ll try to find what I read about it in the past.

EDIT: waltr answered while I was typing.

hi d,

also, check this article:

http://www.sm.luth.se/~alapaa/file_fetch/unixcdbookshelf/mac/ch02_01.htm

i particulary like the ‘SystemStarter’ stuff. seems like that would be a good place for a script.

Walt and Capi,

Thanks for your info. I have tried the loginwindow manager in the past for other situations, but not for this one. I will give LWM a try because putting the script/app into loginitems isn’t any good for people logging into a network account (e.g. Active Directory), but LWM might work.

I am particularly interested in using the “Startup items” but when I tried it before it just halted the machine. I tried to structure it like existing startup items, but didn’t work. I’ll research it a bit more and hopefully I can get it working that way.

Thank you
David

According to what I have just read here - http://forums.macnn.com/archive/index.php/t-122031.html - it can be done, I’m not experienced enought to help.

Good luck.

j

Capi,

Thanks, I think that’ll do the trick!!

Cheers!!

Dave

If you want the script to run at boot, before anyone logs in, then the tool you need is ‘launchd.’
You must be using 10.4, I believe, for launchd to be available, but it is the way to go if you want your application to do any of the following:

  • launch only when a particular user logs in
  • launch when ANY user logs in and run as that user
  • run as root when system starts up
  • run only when a particular folder has something added to it (does NOT rely on Folderactions!)

LaunchD is a unix utility which automatically can run just about anything.
It can launch things at startup or login, fixed or repeating schedule,
when a file changes, or when you add a new file to a folder, and more.
It can keep a certain program running and if the program crashes or
is quit by a user, launchd will automatically relaunch the program.
More advanced features of launchd include the ability to scan a
directory periodically for files, launching programs in response
to network port access, changes to files, or the creation of files
in a particular directory.

You can have it launch a program just when a particular user logs in,
when any user logs in, or it can run as root when the system boots.

There are a lot of interesting uses for this utility.
Since launchd relies on pList files placed in certain folders
it would be simple to modify a “template” plist file and use this
with your applescript applications. Late Night Software’s free plist OSAX would be helpful in this regard.

A brief intro to using Lingon (a program which puts a front end onto
creating the plist files for launchD) can be found here:

http://www.macworld.com/2006/01/secrets/februarygeekfactor/index.php

This might seem a little complicated, but using Lignon to load and run launchd jobs is fairly easy. The Macworld article goes over this in detail.

HTH

Vince

Vince,

Your right and to tell you the truth I totally forgot about launchd!!

Thanks
Dave