4

I've researched this thing for quite some time now but haven't found any good solution, time to ask for the wisdom of the crowd...

Here's what I'm trying to achieve: Only on workdays, as the first thing that greets me when I log back in (with the Macbook most probably coming from standby mode and not booting up), I want to start a script to shut down some windows and open my daily planning list. If I have not opened my Mac for a few days, it should not execute multiple times. The script itself doesn't really matter here, but I couldn't find a reliable solution for the execution hook anywhere.

I've dabbled with Automator and read through some tasks already:

Programmatically run at startup on Mac OS X? Running script upon login mac

But everything I see is somehow related to login items, which seem like they aren't executed when coming back up from standby.

I'm open to any solution, preferably something commandline or baked in (launchd etc.), but could also be an app.

Thanks in advance,

Dom

Community
  • 1
  • 1
endymi0n
  • 43
  • 1
  • 4

3 Answers3

2

This approach actually works fairly reliably for me upon testing against 10.11:

https://superuser.com/questions/229773/run-command-on-startup-login-mac-os-x#answer-229792

It doesn't solve all permission issues with running something as one user (root, for example) at or before the login screen, but if you can figure that out, it does run by the time the login screen is shown, and seems to work reliably after login, which is what you've actually asked for.

Copying here as even though the answer is also an answer to this question, the two questions are different:


From user 'Scott'...

To run a command on start up on OS X, you need to use launchd.

If you don't want to use Lingon, you need to create a launchd Property List. This is an XML file, so you can do it with your favourite text editor or alternatively you can use the Property List Editor that's installed with the Mac OS X Dev Tools. Create the following:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    <key>Label</key>
    <string>some.meaningful.name</string> <!-- org.mongodb.mongodb perhaps? -->

    <key>OnDemand</key>
    <false/>

    <key>UserName</key>
    <string>anAppropriateUser</string>

    <key>GroupName</key>
    <string>anAppropriateGroup</string>

    <key>ProgramArguments</key>
    <array>
            <string>/Applications/MongoDB/bin/mongod</string>
            <string>--dbpath</string>
            <string>/usr/local/mongo/data</string>
            <string>--fork</string>
            <string>--logpath</string>
            <string>/usr/local/mongo/log</string>
    </array>
</dict>
</plist>

Save this in /Library/LaunchAgents/some.meaningful.name.plist (you will need an administrator account and/or sudo), then open a terminal and do:

sudo launchctl load /Library/LaunchAgents/some.meaningful.name.plist

This will cause launchd to load the item which will cause it to start MongoDB on boot. As a bonus, launchd will monitor it and, if it exits for any reason, it will be re-started. To get rid of the item simply replace load in the above command with unload.

From user msanford...

This will attempt to run the application every 10 seconds, which works well for services that don't die. If this is for a script that runs once punctually (in my case, messaging once Slack on reboot) add <key>LaunchOnlyOnce</key><true/> to the dict.

Jan Kyu Peblik
  • 1,435
  • 14
  • 20
1

I've looked for this for ages, and have found no proper solution. It seems the macOS devs do not consider logging in from the lock screen the same as a "login".

The only solutions I've seen are inelegant polling processes like SleepWatcher...
http://www.bernhard-baehr.de/
...which further suggests there is no simple event/hook.

That approach should work, it's just a shame the OS is so terribly dumb about this.


Edit: I've added another answer with more promising information, but still feel this is a fundamental problem in vanilla macOS and believe this answer is still accurate.

Jan Kyu Peblik
  • 1,435
  • 14
  • 20
-3

I would have thought you would have a bunch of answers by now. The concept of "workday" needs a bit of definition. If you are just talking about M-F, or whatever, you could just consider running a script every day, which determines the day of week and does nothing on non-workdays. Do you know python? If so, you could write a program that runs every day (or even constantly), and keep track of the last day it did something. You can set an action for whatever constitutes your workday - even including holidays, and let it do what you want.

You could do the same in any shell programming language as well - bash, ksh, etc.

Applescript might we workable for some of the things that you want. I am too novice at Applescript to make any specific suggestions - it is a bit awkward as a programming language IMO.

Fred Mitchell
  • 2,145
  • 2
  • 21
  • 29