From 5c3e63d30d87a94a417eb2e2d677b19c16df11c3 Mon Sep 17 00:00:00 2001 From: Yuriy Puchkov Date: Fri, 15 Jan 2021 21:16:49 +0200 Subject: [PATCH] fix - MacOS - startup (login items) feature (cherry picked from commit 97ac0b1da4a3340da8019c7736b99fe5cd9274e5) --- src/utils/confighandler.cpp | 41 ++++++++++++++++++++++++++++++++++++- 1 file changed, 40 insertions(+), 1 deletion(-) diff --git a/src/utils/confighandler.cpp b/src/utils/confighandler.cpp index 33cfd8bb..752cdbe1 100644 --- a/src/utils/confighandler.cpp +++ b/src/utils/confighandler.cpp @@ -19,10 +19,12 @@ #include "src/tools/capturetool.h" #include "src/utils/configshortcuts.h" #include +#include #include #include #include #include +#include #include ConfigHandler::ConfigHandler() @@ -309,7 +311,12 @@ void ConfigHandler::setKeepOpenAppLauncher(const bool keepOpen) bool ConfigHandler::startupLaunchValue() { +#if (defined(Q_OS_MAC) || defined(Q_OS_MAC64) || defined(Q_OS_MACOS) || \ + defined(Q_OS_MACX)) + bool res = false; +#else bool res = true; +#endif if (m_settings.contains(QStringLiteral("startupLaunch"))) { res = m_settings.value(QStringLiteral("startupLaunch")).toBool(); } @@ -341,7 +348,39 @@ bool ConfigHandler::verifyLaunchFile() void ConfigHandler::setStartupLaunch(const bool start) { -#if defined(Q_OS_LINUX) || defined(Q_OS_UNIX) + if (start == m_settings.value(QStringLiteral("startupLaunch")).toBool()) { + return; + } +#if (defined(Q_OS_MAC) || defined(Q_OS_MAC64) || defined(Q_OS_MACOS) || \ + defined(Q_OS_MACX)) + /* TODO - there should be more correct way via API, but didn't find it + without extra dependencies, there should be something like that: + https://stackoverflow.com/questions/3358410/programmatically-run-at-startup-on-mac-os-x + But files with this features differs on different MacOS versions and it + doesn't work not on a BigSur at lease. + */ + QProcess process; + if (start) { + process.start("osascript", + QStringList() + << "-e" + << "tell application \"System Events\" to make login " + "item at end with properties {name: " + "\"Flameshot\",path:\"/Applications/" + "flameshot.app\", hidden:false}"); + } else { + process.start("osascript", + QStringList() << "-e" + << "tell application \"System Events\" to " + "delete login item \"Flameshot\""); + } + if (!process.waitForFinished()) { + qWarning() << "Login items is changed. " << process.errorString(); + } else { + qWarning() << "Unable to change login items, error:" + << process.readAll(); + } +#elif defined(Q_OS_LINUX) || defined(Q_OS_UNIX) QString path = QStandardPaths::locate(QStandardPaths::GenericConfigLocation, "autostart/", QStandardPaths::LocateDirectory);