startup snippets

How to set an application to load at login with Cocoa and Objective-C

Tagged startup, login, cocoa, objective-c  Languages objectivec

In XCode add the following to your controller's header file:

@interface AppController : NSObject {
IBOutlet NSButton *buttonOpenAtLogin;
}
.
.
.
- (IBAction)addLoginItem:(id)sender;
- (void)enableLoginItemWithLoginItemsReference:(LSSharedFileListRef )theLoginItemsRefs ForPath:(CFURLRef)thePath;
- (void)disableLoginItemWithLoginItemsReference:(LSSharedFileListRef )theLoginItemsRefs ForPath:(CFURLRef)thePath;

Add the following to the implementation:

@implementation AppController

- (void)enableLoginItemWithLoginItemsReference:(LSSharedFileListRef )theLoginItemsRefs ForPath:(CFURLRef)thePath {
    // We call LSSharedFileListInsertItemURL to insert the item at the bottom of Login Items list.
    LSSharedFileListItemRef item = LSSharedFileListInsertItemURL(theLoginItemsRefs, kLSSharedFileListItemLast, NULL, NULL, thePath, NULL, NULL);        
    if (item)
        CFRelease(item);
}

- (void)disableLoginItemWithLoginItemsReference:(LSSharedFileListRef )theLoginItemsRefs ForPath:(CFURLRef)thePath {
    UInt32 seedValue;
    
    // We're going to grab the contents of the shared file list (LSSharedFileListItemRef objects)
    // and pop it in an array so we can iterate through it to find our item.
    NSArray  *loginItemsArray = (NSArray *)LSSharedFileListCopySnapshot(theLoginItemsRefs, &seedValue);
    for (id item in loginItemsArray) {      
        LSSharedFileListItemRef itemRef = (LSSharedFileListItemRef)item;
        if (LSSharedFileListItemResolve(itemRef, 0, (CFURLRef*) &thePath, NULL) == noErr) {
            if ([[(NSURL *)thePath path] hasPrefix:SGApplicationPath])
                LSSharedFileListItemRemove(theLoginItemsRefs, itemRef); // Deleting the item
        }
    }
    
    [loginItemsArray release];
}

- (IBAction)addLoginItem:(id)sender {
    CFURLRef url = (CFURLRef)[NSURL fileURLWithPath:SGApplicationPath];
    
    // Create a reference to the shared file list.
    LSSharedFileListRef loginItems = LSSharedFileListCreate(NULL, kLSSharedFileListSessionLoginItems, NULL);
    
    if (loginItems) {
        if ([[buttonOpenAtLogin selectedCell] state] == YES)
            [self enableLoginItemWithLoginItemsReference:loginItems ForPath:url];
        else
            [self disableLoginItemWithLoginItemsReference:loginItems ForPath:url];
    }
    CFRelease(loginItems);
}
.
.
.

In Interface Builder do the following:

  • Add a User Defaults Controller Bindings object to IB.
  • Add a checkbox button or menu item to IB
  • Bind the button to the IBOutlet in the controller
  • Select the button and bind it to the User Defaults Controller (Cmd+5). Enter values in "Controller Key" and "Model Key Path"

Code found here

See http://developer.apple.com/cocoa/cocoabindings.html for more details...

Nginx start script for Debian

Tagged debian, nginx, startup  Languages bash
#! /bin/sh

### BEGIN INIT INFO
# Provides:          nginx
# Required-Start:    $all
# Required-Stop:     $all
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: starts the nginx web server
# Description:       starts nginx using start-stop-daemon
### END INIT INFO

PATH=/opt/nginx/sbin:/sbin:/bin:/usr/sbin:/usr/bin
DAEMON=/opt/nginx/sbin/nginx
NAME=nginx
DESC=nginx
PID=/var/run/nginx.pid

test -x $DAEMON || exit 0

# Include nginx defaults if available
if [ -f /etc/default/nginx ] ; then
        . /etc/default/nginx
fi

set -e

case "$1" in
  start)
        echo -n "Starting $DESC: "
        start-stop-daemon --start --quiet --pidfile $PID \
                --exec $DAEMON -- $DAEMON_OPTS
        echo "$NAME."
        ;;
  stop)
        echo -n "Stopping $DESC: "
        start-stop-daemon --stop --quiet --pidfile $PID \
                --exec $DAEMON
        echo "$NAME."
        ;;
  restart|force-reload)
        echo -n "Restarting $DESC: "
        start-stop-daemon --stop --quiet --pidfile \
                $PID --exec $DAEMON
        sleep 1
        start-stop-daemon --start --quiet --pidfile \
                $PID --exec $DAEMON -- $DAEMON_OPTS
        echo "$NAME."
        ;;
  reload)
          echo -n "Reloading $DESC configuration: "
          start-stop-daemon --stop --signal HUP --quiet --pidfile     $PID \
              --exec $DAEMON
          echo "$NAME."
          ;;
      *)
            N=/etc/init.d/$NAME
            echo "Usage: $N {start|stop|restart|reload|force-reload}" >&2
            exit 1
            ;;
esac

exit 0