Skip to content

Commit

Permalink
[macOS] simplify restarting app as root with Apple Script
Browse files Browse the repository at this point in the history
  • Loading branch information
kambala-decapitator committed Feb 19, 2024
1 parent 5e8ec44 commit a22b2b0
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 46 deletions.
11 changes: 0 additions & 11 deletions dmg_osx/main.scpt.txt

This file was deleted.

24 changes: 0 additions & 24 deletions dmg_osx/template.app/Contents/MacOS/askPass.js

This file was deleted.

17 changes: 6 additions & 11 deletions main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,16 @@

#ifdef Q_OS_MAC
#include "privileges_unix.h"

#include <QLatin1String>
#include <QProcess>
#else
#include "privileges.h"
#endif

#include <QApplication>
#include <QFileInfo>
#include <QDesktopServices>
#include <QProcess>
#include <QProxyStyle>
#include <QNetworkProxy>
#include <QDebug>
Expand All @@ -55,18 +57,11 @@ int main(int argc, char *argv[])
// If not running with root privileges, relaunch executable with sudo.
if (getuid() != 0 && app.arguments().contains("--elevated") == false)
{
QString askPassCommand = QCoreApplication::applicationDirPath() + "/askPass.js";

QProcessEnvironment env = QProcessEnvironment::systemEnvironment();
env.insert("SUDO_ASKPASS", askPassCommand);
const QLatin1String appleScript{"do shell script \"sudo %1\" with administrator privileges"};

QProcess myProcess;
myProcess.setProcessEnvironment(env);
myProcess.setProgram("sudo");
myProcess.setArguments(QStringList()
<< "-A"
<< QCoreApplication::applicationFilePath()
<< "--elevated");
myProcess.setProgram(QLatin1String{"osascript"});
myProcess.setArguments({"-e", appleScript.arg(QCoreApplication::applicationFilePath())});
bool success = myProcess.startDetached();

if (success)
Expand Down

0 comments on commit a22b2b0

Please sign in to comment.