Skip to content

Commit

Permalink
Implement autostart setting for macOS. Fixes borgbase#56
Browse files Browse the repository at this point in the history
  • Loading branch information
m3nu committed Dec 5, 2018
1 parent 9362abf commit a9d7e28
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
20 changes: 20 additions & 0 deletions src/vorta/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -219,3 +219,23 @@ def set_tray_icon(tray, active=False):
icon_name = f"icons/hdd-o{'-active' if active else ''}-{'light' if use_light_style else 'dark'}.png"
icon = QIcon(get_asset(icon_name))
tray.setIcon(icon)


def open_app_at_startup(enabled=True):
if sys.platform == 'darwin':
# From https://stackoverflow.com/questions/26213884/cocoa-add-app-to-startup-in-sandbox-using-pyobjc
from Foundation import NSDictionary
from Cocoa import NSBundle, NSURL
from CoreFoundation import kCFAllocatorDefault
from LaunchServices import (LSSharedFileListCreate, kLSSharedFileListSessionLoginItems,
LSSharedFileListInsertItemURL, kLSSharedFileListItemHidden,
kLSSharedFileListItemLast, LSSharedFileListItemRemove)
app_path = NSBundle.mainBundle().bundlePath()
url = NSURL.alloc().initFileURLWithPath_(app_path)
login_items = LSSharedFileListCreate(kCFAllocatorDefault, kLSSharedFileListSessionLoginItems, None)
props = NSDictionary.dictionaryWithObject_forKey_(True, kLSSharedFileListItemHidden)

new_item = LSSharedFileListInsertItemURL(login_items, kLSSharedFileListItemLast,
None, None, url, props, None)
if not enabled:
LSSharedFileListItemRemove(login_items, new_item)
5 changes: 4 additions & 1 deletion src/vorta/views/misc_tab.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from PyQt5 import uic
from PyQt5.QtWidgets import QCheckBox
from vorta.utils import get_asset
from vorta.utils import get_asset, open_app_at_startup
from vorta.models import SettingsModel
from vorta._version import __version__

Expand All @@ -26,3 +26,6 @@ def save_setting(self, key, new_value):
setting = SettingsModel.get(key=key)
setting.value = bool(new_value)
setting.save()

if key == 'autostart':
open_app_at_startup(new_value)

0 comments on commit a9d7e28

Please sign in to comment.