Skip to content

Instantly share code, notes, and snippets.

@madrobby
Last active August 29, 2015 14:01
Show Gist options
  • Save madrobby/9f134c440bd6524e7e7a to your computer and use it in GitHub Desktop.
Save madrobby/9f134c440bd6524e7e7a to your computer and use it in GitHub Desktop.
RubyMotion vs. Objective-C showdown
-(BOOL)appIsPresentInLoginItems
{
NSString *bundleID = @"blah";
NSArray * jobDicts = nil;
jobDicts = (NSArray *)SMCopyAllJobDictionaries( kSMDomainUserLaunchd );
if ( (jobDicts != nil) && [jobDicts count] > 0 ) {
BOOL bOnDemand = NO;
for ( NSDictionary * job in jobDicts ) {
if ( [bundleID isEqualToString:[job objectForKey:@"Label"]] ) {
bOnDemand = [[job objectForKey:@"OnDemand"] boolValue];
break;
}
}
CFRelease((CFDictionaryRef)jobDicts); jobDicts = nil;
return bOnDemand;
}
return NO;
}
HELPER_BUNDLE_ID = 'blah'
def launchesAtLogin?
SMCopyAllJobDictionaries(KSMDomainUserLaunchd).any? do |job|
job['Label'] == HELPER_BUNDLE_ID && job['OnDemand']
end
end
@joshuatbrown
Copy link

The Objective-C can be shorter...

Line 4 could be:

NSArray *jobDicts = (NSArray *)SMCopyAllJobDictionaries( kSMDomainUserLaunchd );

(this removes the need for line 5)

Line 7 can be:

if ([jobDicts count] > 0) {

No need for the nil check, since this expression will evaluate to false if jobDicts is nil.

I realize the Ruby is still much shorter, but this helps a bit...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment