Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable launch at login #2

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 0 additions & 11 deletions Mute Me Now/AppDelegate.m
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#import "AppDelegate.h"
#import "TouchBar.h"
#import <ServiceManagement/ServiceManagement.h>
#import "TouchButton.h"
#import "TouchDelegate.h"

Expand Down Expand Up @@ -28,16 +27,6 @@ - (void)applicationDidFinishLaunching:(NSNotification *)aNotification {

[NSTouchBarItem addSystemTrayItem:mute];
DFRElementSetControlStripPresenceForIdentifier(muteIdentifier, YES);

[self enableLoginAutostart];
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Automatically enabling "launch at login" is very user-hostile and I wouldn't recommend doing it. Let the user decide for themselves.

Also, Apple won't allow this in Mac App Store if you ever going to publish there.


}

-(void) enableLoginAutostart {
BOOL state = [[NSUserDefaults standardUserDefaults] boolForKey:@"auto_login"];
if(!SMLoginItemSetEnabled((__bridge CFStringRef)@"Pixel-Point.Mute-Me-Now-Launcher", !state)) {
NSLog(@"The login was not succesfull");
}
}

- (void)applicationWillTerminate:(NSNotification *)aNotification {
Expand Down
5 changes: 1 addition & 4 deletions Mute Me Now/Base.lproj/Main.storyboard
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<document type="com.apple.InterfaceBuilder3.Cocoa.Storyboard.XIB" version="3.0" toolsVersion="12118" systemVersion="16E195" targetRuntime="MacOSX.Cocoa" propertyAccessControl="none" useAutolayout="YES" initialViewController="B8D-0N-5wS">
<document type="com.apple.InterfaceBuilder3.Cocoa.Storyboard.XIB" version="3.0" toolsVersion="12118" systemVersion="16F73" targetRuntime="MacOSX.Cocoa" propertyAccessControl="none" useAutolayout="YES" initialViewController="B8D-0N-5wS">
<dependencies>
<deployment identifier="macosx"/>
<plugIn identifier="com.apple.InterfaceBuilder.CocoaPlugin" version="12118"/>
Expand Down Expand Up @@ -716,9 +716,6 @@
<behavior key="behavior" changeContents="YES" doesNotDimImage="YES" lightByContents="YES"/>
<font key="font" metaFont="system"/>
</buttonCell>
<connections>
<action selector="onLoginStartChanged:" target="XfG-lQ-9wD" id="57j-16-wLE"/>
</connections>
</button>
<textField horizontalHuggingPriority="251" verticalHuggingPriority="750" allowsCharacterPickerTouchBarItem="NO" translatesAutoresizingMaskIntoConstraints="NO" id="paW-7T-Udu">
<rect key="frame" x="18" y="23" width="31" height="17"/>
Expand Down
16 changes: 16 additions & 0 deletions Mute Me Now/MMNLaunchAtLoginController.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
//
// MMNLaunchAtLoginController.h
// Mute Me
//
// Created by Dmitry Rodionov on 17/06/2017.
// Copyright © 2017 Pixel Point. All rights reserved.
//

#import <Cocoa/Cocoa.h>

@interface MMNLaunchAtLoginController : NSObject
@property (readwrite) BOOL shouldLaunchOnLogin;

+ (instancetype)sharedController;

@end
59 changes: 59 additions & 0 deletions Mute Me Now/MMNLaunchAtLoginController.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
//
// MMNLaunchAtLoginController.m
// Mute Me
//
// Created by Dmitry Rodionov on 17/06/2017.
// Copyright © 2017 Pixel Point. All rights reserved.
//

#import "MMNLaunchAtLoginController.h"
#import <ServiceManagement/ServiceManagement.h>

static NSString *const kLauncherBundleID = @"Pixel-Point.Mute-Me-Now-Launcher";

@implementation MMNLaunchAtLoginController

+ (instancetype)sharedController
{
static id shared = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
shared = [[self alloc] init];
});

return shared;
}

- (BOOL)shouldLaunchOnLogin
{
// SMCopyAllJobDictionaries() was deprecated back in 10.10, but as Apple says in
// their own documentation:
//
// > For the specific use of testing the state of a login item that may have been
// > enabled with SMLoginItemSetEnabled() in order to show that state to the
// > user, this function remains the recommended API. A replacement API for this
// > specific use will be provided before this function is removed.
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated"
NSArray <NSDictionary *> *jobs = (__bridge NSArray<NSDictionary *> *)(SMCopyAllJobDictionaries(kSMDomainUserLaunchd));
#pragma clang diagnostic pop
if (jobs.count == 0) {
return NO;
}
// Look for a launchd job with the same bundle ID as our launcher application
NSUInteger idx = [jobs indexOfObjectPassingTest:^BOOL(NSDictionary *job, NSUInteger idx, BOOL *stop) {
return [job[@"Label"] isEqualToString:kLauncherBundleID];
}];
return (idx != NSNotFound);
}


- (void)setShouldLaunchOnLogin:(BOOL)state
{
BOOL set = SMLoginItemSetEnabled((__bridge CFStringRef)(kLauncherBundleID), state);
if (!set) {
NSLog(@"Unable to toggle the login item");
}
}

@end
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
/* End PBXBuildFile section */

/* Begin PBXFileReference section */
0ADD25F01EF5750B00E095AB /* Mute Me Now Launcher.entitlements */ = {isa = PBXFileReference; lastKnownFileType = text.plist.entitlements; path = "Mute Me Now Launcher.entitlements"; sourceTree = "<group>"; };
11B86E751EDEAC970069D254 /* Mute Me Now Launcher.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = "Mute Me Now Launcher.app"; sourceTree = BUILT_PRODUCTS_DIR; };
11B86E781EDEAC970069D254 /* AppDelegate.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = AppDelegate.h; sourceTree = "<group>"; };
11B86E791EDEAC970069D254 /* AppDelegate.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = AppDelegate.m; sourceTree = "<group>"; };
Expand Down Expand Up @@ -56,6 +57,7 @@
11B86E771EDEAC970069D254 /* Mute Me Now Launcher */ = {
isa = PBXGroup;
children = (
0ADD25F01EF5750B00E095AB /* Mute Me Now Launcher.entitlements */,
11B86E781EDEAC970069D254 /* AppDelegate.h */,
11B86E791EDEAC970069D254 /* AppDelegate.m */,
11B86E7E1EDEAC970069D254 /* ViewController.h */,
Expand Down Expand Up @@ -264,7 +266,7 @@
isa = XCBuildConfiguration;
buildSettings = {
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
CODE_SIGN_ENTITLEMENTS = "";
CODE_SIGN_ENTITLEMENTS = "Mute Me Now Launcher/Mute Me Now Launcher.entitlements";
COMBINE_HIDPI_IMAGES = YES;
INFOPLIST_FILE = "Mute Me Now Launcher/Info.plist";
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/../Frameworks";
Expand All @@ -278,7 +280,7 @@
isa = XCBuildConfiguration;
buildSettings = {
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
CODE_SIGN_ENTITLEMENTS = "";
CODE_SIGN_ENTITLEMENTS = "Mute Me Now Launcher/Mute Me Now Launcher.entitlements";
COMBINE_HIDPI_IMAGES = YES;
INFOPLIST_FILE = "Mute Me Now Launcher/Info.plist";
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/../Frameworks";
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>com.apple.security.app-sandbox</key>
<true/>
</dict>
</plist>
18 changes: 6 additions & 12 deletions Mute Me Now/ViewController.m
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#import "ViewController.h"
#import "MMNLaunchAtLoginController.h"

static NSString *githubURL = @"https://github.com/pixel-point/mute-me";
static NSString *projectURL = @"https://muteme.pixelpoint.io/";
Expand All @@ -9,8 +10,11 @@ @implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];

BOOL state = [[NSUserDefaults standardUserDefaults] boolForKey:@"auto_login"];
[self.autoLoginState setState: !state];
// Bind "Launch at login" checkbox state to the corresponding value in the controller object
[self.autoLoginState bind:NSValueBinding
toObject:[MMNLaunchAtLoginController sharedController]
withKeyPath:@"shouldLaunchOnLogin"
options:nil];
}

-(void)viewDidAppear {
Expand All @@ -36,16 +40,6 @@ - (IBAction)onGithubPressed:(id)sender {
- (IBAction)onWebsitePressed:(id)sender {
[[NSWorkspace sharedWorkspace] openURL:[NSURL URLWithString:projectURL]];
}
- (IBAction)onLoginStartChanged:(id)sender {
NSInteger state = [self.autoLoginState state];
BOOL enableState = NO;
if(state == NSOnState) {
enableState = YES;
}
if(SMLoginItemSetEnabled((__bridge CFStringRef)@"Pixel-Point.Mute-Me-Now-Launcher", enableState)) {
[[NSUserDefaults standardUserDefaults] setBool:!enableState forKey:@"auto_login"];
}
}
- (IBAction)onMainWebsitePressed:(id)sender {
[[NSWorkspace sharedWorkspace] openURL:[NSURL URLWithString:companyURL]];
}
Expand Down
6 changes: 6 additions & 0 deletions Mute Me.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
objects = {

/* Begin PBXBuildFile section */
0A9EAC491EF54250002CA9B0 /* MMNLaunchAtLoginController.m in Sources */ = {isa = PBXBuildFile; fileRef = 0A9EAC481EF54250002CA9B0 /* MMNLaunchAtLoginController.m */; };
1141FDF21EEFF6DD00CA5B30 /* TouchButton.m in Sources */ = {isa = PBXBuildFile; fileRef = 1141FDF11EEFF6DD00CA5B30 /* TouchButton.m */; };
11B86E521EDE9B4D0069D254 /* AppDelegate.m in Sources */ = {isa = PBXBuildFile; fileRef = 11B86E511EDE9B4D0069D254 /* AppDelegate.m */; };
11B86E551EDE9B4D0069D254 /* main.m in Sources */ = {isa = PBXBuildFile; fileRef = 11B86E541EDE9B4D0069D254 /* main.m */; };
Expand Down Expand Up @@ -51,6 +52,8 @@
/* End PBXCopyFilesBuildPhase section */

/* Begin PBXFileReference section */
0A9EAC471EF54250002CA9B0 /* MMNLaunchAtLoginController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = MMNLaunchAtLoginController.h; sourceTree = "<group>"; };
0A9EAC481EF54250002CA9B0 /* MMNLaunchAtLoginController.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = MMNLaunchAtLoginController.m; sourceTree = "<group>"; };
1141FDF01EEFF6DD00CA5B30 /* TouchButton.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = TouchButton.h; sourceTree = "<group>"; };
1141FDF11EEFF6DD00CA5B30 /* TouchButton.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = TouchButton.m; sourceTree = "<group>"; };
1141FDF51EEFFA0000CA5B30 /* TouchDelegate.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = TouchDelegate.h; sourceTree = "<group>"; };
Expand Down Expand Up @@ -113,6 +116,8 @@
11B86E511EDE9B4D0069D254 /* AppDelegate.m */,
11B86E561EDE9B4D0069D254 /* ViewController.h */,
11B86E571EDE9B4D0069D254 /* ViewController.m */,
0A9EAC471EF54250002CA9B0 /* MMNLaunchAtLoginController.h */,
0A9EAC481EF54250002CA9B0 /* MMNLaunchAtLoginController.m */,
11B86E591EDE9B4D0069D254 /* Assets.xcassets */,
11B86E5B1EDE9B4D0069D254 /* Main.storyboard */,
11B86E5E1EDE9B4D0069D254 /* Info.plist */,
Expand Down Expand Up @@ -249,6 +254,7 @@
11B86E581EDE9B4D0069D254 /* ViewController.m in Sources */,
11B86E551EDE9B4D0069D254 /* main.m in Sources */,
11B86E521EDE9B4D0069D254 /* AppDelegate.m in Sources */,
0A9EAC491EF54250002CA9B0 /* MMNLaunchAtLoginController.m in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down