Check Box Help

Hello,

I am using this script to get the value of my check box, but when I check system preferences, I don’t have a login item.

    on getBoxStates_(sender)
        if (my loginBox) as boolean = true then
            tell application "System Events"
                make new login item at end of login items with properties {path:(path to me), hidden:false}
            end tell
            
        else if (my loginBox) as boolean = false then
            tell application "System Events"
                if login item "ScreenTunes" exists then delete login item "ScreenTunes"
            end tell
        
        end 
    end getBoxStates_

You have to check for the state of the check box


loginBox's state()

Thanks! I tried it with my other check boxes and they worked but this box still doesn’t create the login item. Also, is there a way to set the checkbox to true but only on the first time the user runs the app?

The smartest way is to use the ObjC code in this article posted by Nick Moore

http://stackoverflow.com/questions/815063/how-do-you-make-your-app-open-at-login

create a ObjC class LoginItem
put this in the .h file and the upper code from stackoverflow in the .m file


@interface LoginItem : NSObject {

}

+ (BOOL)willStartAtLogin:(NSURL *)itemURL;
+ (void)setStartAtLogin:(NSURL *)itemURL enabled:(BOOL)enabled;

@end

create a second ObjC class, name it whatever you like, put this in the .h file


@interface MyLoginClass : NSObject {

}

- (NSURL *)appURL;
- (BOOL)startAtLogin;
- (void)setStartAtLogin:(BOOL)enabled;

@property (nonatomic, assign) BOOL startAtLogin;

@end

and the lower code plus this line


@synthesize startAtLogin;

in the .m file.
Connect a blue cube in Interface Builder with “MyLoginClass”
then bind the value of the checkbox to startAtLogin. That’s all

I am getting 5 errors in LoginItem2.m and 1 warning in LoginItem.m. Most of the errors in LoginItem2 are semantic issue saying that LoginItem is an unknown receiver. The warning in LoginItem.m says that status is an unused variable in this line of code :

OSStatus status;

I forgot you must import the LoginItem.h in the second class.
Put this at top of LoginItem2.h outside the interface block


#import "LoginItem.h"

Thanks! That seemed to have stopped the errors in LoginItem2.m! Should I be concerned about the warning in LoginItem.m?

the variable OSStatus is unused, you can delete the line

You can also do it very much like your first post (with the fix for using the box’s state) by changing the way you get your app’s path. I’m not sure why (path to me) doesn’t work (did you get an error message about that?), but this method gets the correct path and creates the new login item:

set mb to current application's NSBundle's mainBundle()
		set myPath to mb's executablePath() as text
		tell application "System Events"
			make new login item at end of login items with properties {path:myPath, hidden:false}
		end tell

Ric