I wrote a small script to sync one folder to another as a Javascript for Automation script.
If I run it directly from terminal:
$ osascript -l JavaScript myscript.js
then it works as supposed - the referenced shell script is run, and then a dialog popup with the output of the shell script is shown, and then the notification is shown in the top right corner.
Now, I need this script to have full disk access to run it via launchd
. For that, I compile the script into an application:
osacompile -l JavaScript -o myapp.app myscript.js
Now, if I run the resultant app:
open myapp.app
everything works (the app requests access to external volume, as should be), but unfortunately, the notification is not shown in the top right corner. It seems like macOS does not register the app for shown notifications (it does not appear in the notifications pane in System preferences).
How can I solve this bug with notifications? Thank you!
The myscript.js
content:
const app = Application.currentApplication();
app.includeStandardAdditions = true;
console.log("Start sync-archive");
const home = app.systemAttribute("HOME");
const script = home + "/.launchd/sync-archive/sync-archive.sh";
const res = app.doShellScript("zsh " + script);
app.displayDialog(res);
if (res.includes("success")) {
app.displayNotification("completed successfully", {
withTitle: "sync-archive",
});
console.log("Finish sync-archive: success");
} else {
app.displayAlert("sync-archive failed", {
message: "Please see " + script + " and logfiles",
as: "warning",
});
console.log("Finish sync-archive: failure");
}
.app
.launchd
should be able to execute it by way ofosascript
, and you can giveosascript
full disk access if it needs it. But seeing as the only thing your.js
. script does is execute a shell script, a better option would be to havelaunchd
execute that instead, after which the result can be displayed however you wish, either by way of a shell script, a JXA script, or an inline call toosascript
.launchd
, it does not have "Full Disk Access", that is, cannot read external volumes. Giving the access tozsh
is considered a security risk. It was an unpleasant surprise for me, as I thought before thatlaunchd
is simply likecron
. However, it is not. And only by compiling an app and giving the app the access, I can run the task successfully.