Skip navigation
This discussion is archived

How to run applescript at startup?

1045 Views 7 Replies Latest reply: Dec 28, 2006 12:26 PM by David Boyer2 RSS
David Boyer2 Calculating status...
Currently Being Moderated
Jul 7, 2006 8:46 AM
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

G5 dual 1.8/1gig   Mac OS X (10.4.6)  
  • Richard Moore Calculating status...
    Currently Being Moderated
    Jul 7, 2006 9:56 AM (in response to David Boyer2)
    Just delete those illegal characters, save your script as an application in the applications folder. and run the script with the appropriate name for the application.

    Or if you do not want to run this script or cant get it to work, just simply save your script as an application. Then open System Preferences > accounts > login items, then select the add option (small plus sign) and select your application.
    iMac G5 17 1.8ghz 1GB RAM AX, Mac OS X (10.4.7)
  • Cyclosaurus Level 6 Level 6 (12,915 points)
    Currently Being Moderated
    Jul 7, 2006 7:58 PM (in response to David Boyer2)
    You can launch osascript by way of StartupItems, but of security, you will have to do lotsa maunal work. Run the following script:'

    --begin
    set nl to ASCII character 10
    set _name to text returned of (display dialog "Name of Startup Item:" default answer "")

    tell application "Finder"
    try
    set new_folder to (make new folder at desktop with properties {name:_name}) as string
    end try
    end tell

    set shell_contents to "#!/bin/sh" & nl & nl & "/usr/bin/osascript "
    do shell script "/bin/echo " & quoted form of shell_contents & " > " & quoted form of (POSIX path of (new_folder & _name))

    set plist_contents to "{" & "
    Description = \"" & _name & "\";" & nl & "}"
    do shell script "/bin/echo " & quoted form of plist_contents & " > " & quoted form of (POSIX path of (new_folder & "StartupParameters.plist"))
    --end

    it'll prompt for the Startup Item name. <folder name>
    it'll create a folder on your desktop with specified name, the folder contains two text files.
    leave the StartupParameters.plist alone, you need to edit the other file
    open other file with TextEdit, you find:

    #!/bin/sh

    /usr/bin/osascript


    you have to append POSIX path of your script in quoted form to after /usr/bin/osascript . ie: /usr/bin/osascript '/blah/blah blah'

    move (copy) the folder to '/Library/StartupItems/' folder

    now you have to chmod and chown for the folder, in Terminal:

    sudo chmod -R 755 /Library/StartupItems/<folder name>
    sudo chown -R root:wheel /Library/StartupItems/<folder name>
  • fragmore Level 3 Level 3 (660 points)
    Currently Being Moderated
    Sep 5, 2006 7:07 PM (in response to David Boyer2)
    Save your script as an application. Then open system prefs go to accounts then login items, after unlocking the panel hit the + sign and locate the Applescript application that you saved and enter it in the list.

    Simple eh!
    Power Mac G4 (AGP Graphics) Dual 500, Mac OS X (10.4.7), 2 GB Ram, Nvidia FX5500 agp card
  • MacPeter Calculating status...
    Currently Being Moderated
    Sep 6, 2006 1:16 AM (in response to David Boyer2)
    I have a shell script to run at startup. I did this by putting appropriate files into the directory

    /Library/StartupItems

    I believe that you could arrange to run an Apple script at startup in just the same way. An admin user can put things into the /Library. However some of the things you will have to do must be done as 'superuser' using the sudo command -- so be careful.

    I wanted a firewall setup different from the ones Apple provides for simply through System Prefs. So I made a directory

    /Library/StartupItems/Firewall

    with permissions drwxr-xr-x owned by root and group being wheel.

    I put two files in this directory. One of these is my script for starting up the firewall. In your case this would be your applescript saved as an application. The second file is a .plist file. This has to be written with some care, in ascii. My .plist file is like this:

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
    <plist version="1.0">
    <dict>
    <key>Description</key>
    <string>Firewall</string>
    <key>Messages</key>
    <dict>
    <key>start</key>
    <string>Starting firewall</string>
    <key>stop</key>
    <string>Stopping firewall</string>
    </dict>
    <key>OrderPreference</key>
    <string>Late</string>
    <key>Provides</key>
    <array>
    <string>Firewall</string>
    </array>
    <key>Requires</key>
    <array>
    <string>NetworkExtensions</string>
    <string>Resolver</string>
    </array>
    </dict>
    </plist>

    Basically the .plist file (which must be named StartupParameters.plist)
    contains information about when, during the boot process, your script should be launched.

    It took me a while to work this out. Originally I had expected to be using launchd for this: but it that did not seem to work. Hence this method, which is really the same as used to work in Panther. Hope this helps!
    G5 single processor 1.6MHz, Mac OS X (10.4.7), PowerBook 15"
  • Vincent Angeloni1 Calculating status...
    Currently Being Moderated
    Sep 11, 2006 10:36 AM (in response to David Boyer2)
    Hi,

    If you want your applescript to run prior to login, then use launchd (OS X 10.4 or greater).

    Save the applescript as an application and then use Lingon and the Macworld article (link below) to set up a launchd job that will run prior to login.

    http://www.macworld.com/2006/01/secrets/februarygeekfactor/index.php
    http://www.macworld.com/2005/12/secrets/febgeekfactorside/index.php
    http://lingon.sourceforge.net/

    HTH

    Vince

    G5 Quad    

Actions

More Like This

  • Retrieving data ...

Bookmarked By (0)

Legend

  • This solved my question - 10 points
  • This helped me - 5 points
This site contains user submitted content, comments and opinions and is for informational purposes only. Apple disclaims any and all liability for the acts, omissions and conduct of any third parties in connection with or related to your use of the site. All postings and use of the content on this site are subject to the Apple Support Communities Terms of Use.