Skip to content
Browse files

First Commit

  • Loading branch information...
1 parent 5b1c545 commit 0e79857796aa53d7d7bcf9a488b3e04814277609 @alexzielenski committed Dec 6, 2011
View
66 README.md
@@ -0,0 +1,66 @@
+# StartAtLoginController
+
+Easy to use controller that makes uses of the new ServiceManagement APIs. This is the required way to do login items for sandboxed Applications and soon Apple will make sandboxing required for all Mac App Store Apps so you should update early! (Works in non-sandboxed applications as well)
+
+I have tested it on 10.7 and it supports removing login dictionaries. Also, entries set by this class do not appear to be showing up in the Accounts Panel of System Preferences.
+
+## HOW-TO
+
+You must create an instance of the controller and set the bundle for it to use to your helper bundle. (It must point to a helper bundle that has LSBackgroundOnly or LSUIElement set to YES in its Info.plist and put this bundle in Contents/Library/LoginItems).
+
+Here is an example of a helper bundle:
+
+ - (void)applicationWillFinishLaunching:(NSNotification *)aNotification
+ {
+ NSString *appPath = [[[[[[NSBundle mainBundle] bundlePath] stringByDeletingLastPathComponent] stringByDeletingLastPathComponent] stringByDeletingLastPathComponent] stringByDeletingLastPathComponent];
+ // get to the waaay top. Goes through LoginItems, Library, Contents, Applications
+ [[NSWorkspace sharedWorkspace] launchApplication:appPath];
+ [NSApp terminate:nil];
+ }
+
+Then in your application you can set the bundle (identifier/path) of your helper app:
+
+ StartAtLoginController loginController = [[StartAtLoginController alloc] init];
+ [loginController setBundle:[NSBundle bundleWithPath:[[[NSBundle mainBundle] bundlePath] stringByAppendingPathComponent:@"Contents/Library/LoginItems/AppHelper.app"]]];
+
+And now you can manipulate the Services Entry:
+
+ [loginController setStartAtLogin: YES]; // adds the entry into LaunchServices and activates it
+ //
+ //
+ [loginController remove]; // removes the entire entry from the services list
+ //
+ //
+ BOOL startsAtLogin = [loginController startAtLogin]; // gets the current enabled state
+
+Unfortunately you cannot use interface builder to bind your checkbox to this object since you need to set the bundle of your helper app. But you can make a property accessor for you helper app in your prefs controller and bind it through there.
+
+## REQUIREMENTS
+
+Works only on 10.6.6 and up
+
+## LICENSE
+
+This is licensed under MIT. Here is some legal jargon:
+
+Copyright (c) 2010 Alex Zielenski
+All Rights Reserved
+
+Permission is hereby granted, free of charge, to any person obtaining
+a copy of this software and associated documentation files (the
+'Software'), to deal in the Software without restriction, including
+without limitation the rights to use, copy, modify, merge, publish,
+distribute, sublicense, and/or sell copies of the Software, and to
+permit persons to whom the Software is furnished to do so, subject to
+the following conditions:
+
+The above copyright notice and this permission notice shall be
+included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
+IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
+CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
+TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
+SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
View
32 StartAtLoginController.h
@@ -0,0 +1,32 @@
+// Copyright (c) 2011 Alex Zielenski
+// All Rights Reserved
+//
+// Permission is hereby granted, free of charge, to any person obtaining
+// a copy of this software and associated documentation files (the
+// "Software"), to deal in the Software without restriction, including
+// without limitation the rights to use, copy, modify, merge, publish,
+// distribute, sublicense, and/or sell copies of the Software, and to
+// permit persons to whom the Software is furnished to do so, subject to
+// the following conditions:
+//
+// The above copyright notice and this permission notice shall be
+// included in all copies or substantial portions of the Software.
+//
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
+// IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
+// CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
+// TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
+// SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+
+@interface StartAtLoginController : NSObject {
+ NSString *_identifier;
+ NSURL *_url;
+}
+@property (assign) BOOL startAtLogin;
+@property (copy) NSString *identifier;
+@property (copy) NSURL *url;
+- (void)setBundle:(NSBundle*)bndl;
+- (void)remove;
+@end
View
72 StartAtLoginController.m
@@ -0,0 +1,72 @@
+// Copyright (c) 2011 Alex Zielenski
+// All Rights Reserved
+//
+// Permission is hereby granted, free of charge, to any person obtaining
+// a copy of this software and associated documentation files (the
+// "Software"), to deal in the Software without restriction, including
+// without limitation the rights to use, copy, modify, merge, publish,
+// distribute, sublicense, and/or sell copies of the Software, and to
+// permit persons to whom the Software is furnished to do so, subject to
+// the following conditions:
+//
+// The above copyright notice and this permission notice shall be
+// included in all copies or substantial portions of the Software.
+//
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
+// IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
+// CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
+// TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
+// SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+
+#import "StartAtLoginController.h"
+#import <ServiceManagement/ServiceManagement.h>
+
+@implementation StartAtLoginController
+@synthesize identifier = _identifier;
+@synthesize url = _url;
+@dynamic startAtLogin;
+- (void)dealloc {
+ self.identifier = nil;
+ self.url = nil;
+}
+- (void)setBundle:(NSBundle*)bndl {
+ self.identifier = [bndl bundleIdentifier];
+ self.url = [bndl bundleURL];
+}
+- (BOOL)startAtLogin {
+ if (!_identifier)
+ return NO;
+
+ NSDictionary *dict = (NSDictionary*)SMJobCopyDictionary(kSMDomainUserLaunchd, (CFStringRef)_identifier);
+ BOOL contains = (dict!=NULL);
+ [dict release];
+ return contains;
+}
+- (void)setStartAtLogin:(BOOL)flag {
+ if (!_identifier||!_url)
+ return;
+ [self willChangeValueForKey:@"startAtLogin"];
+ // Let ServiceManagement know that we exist
+ if (LSRegisterURL((CFURLRef)_url, true) != noErr) {
+ NSLog(@"LSRegisterURL failed!");
+ }
+
+ // Make the setting
+ if (!SMLoginItemSetEnabled((CFStringRef)_identifier, (flag) ? true : false)) {
+ NSLog(@"SMLoginItemSetEnabled failed!");
+ }
+ [self didChangeValueForKey:@"startAtLogin"];
+}
+- (void)remove {
+ if (!_identifier)
+ return;
+ [self willChangeValueForKey:@"startAtLogin"];
+ CFErrorRef error = NULL;
+ if (!SMJobRemove(kSMDomainUserLaunchd, (CFStringRef)_identifier, NULL, true, &error)) {
+ NSLog(@"Could not remove job entry: %@", (NSError*)error);
+ }
+ [self didChangeValueForKey:@"startAtLogin"];
+}
+@end
View
0 StartAtLoginController/StartAtLoginController/.gitignore
No changes.

0 comments on commit 0e79857

Please sign in to comment.
Something went wrong with that request. Please try again.