OS X LaunchAgent - Run when desktop ready? - macos

I've created a bash script which does some complex stuff, including some bits of Applescript which involves clicking pop ups, its to run on every new account created on the machine to set up some browser extensions for users (Adblock etc)
The script runs when manually invoked just fine, but when put as a LaunchAgent is erratic at best. It seems to run whilst the create account login/password screen is up - it opens a welcome PDF for a start off and sometimes when you log in, preview is already open without the file.
Othertimes you log in and nothing happens for a good 50 seconds. Occasionally its behaviour is erratic too.
The script needs to run as a launchagent as it runs once per new account - the script actually deletes the launchagent plist at the end so it can't run again. Is there anyway to delay the running of the script to have it start once the desktop is fully loaded? Perhaps once the dock has appeared. I'm going to assume its trying to run too early, it can't open Preview or Safari, it tries to delete the plist which is in use and then just ends up in a confused loop. Obviously not the cleanest way to run.

Related

how to cleanup launchctl when removing app

I writing an app for Mac, one of it's components is a launchctl agent that runs in background.
As mac apps are removed by dragging to trash (I can understand this logic but takes getting used to after years of windows dev'ing), I'm wondering is there anything I can do to cleanup after myself when the user removes the app?
specifically the running background process and its .plist file.
Edit:
After some more thinking about the issue, I have an idea to put a listener on the background process file, get notified if it's deleted, then remove itself from launchctl, delete plist and kill itself. can this work?

Common dialogue control seems to keep my programs running after I close them

I currently run a 64bit Windows 10 development box. I have old VB6 source that I have to unfortunately keep updated for the time being. For some reason all the sudden I noticed that one of the applications I have just built seemed to stay running in the background after the GUI was closed.
I started debugging the issue more and more until I found out that the only time it would seem to persistently stay open in the background after the GUI was closed was if I clicked the button on my form that would call the Common Dialog control to show the file open GUI. I only have to show the file open window and then hit cancel for this to happen.
This ONLY seems to happen on ONE of my dev machines (not the other). Every time I use that CD file open box I have to open task manager up and end the task. I also tried to make sure all forms were closed when my main form starts to Unload. Nothing seems to work or shed any clue on what the issue is. I have also double checked that the following files are now all the same coping them from the known working dev machine to my broken one and re-registering them.
COMDLG32.OCX
comdlg32.oca
comdlg32.dll
COMDLG32.DEP
Both machines are running the same exact OS Win10 Pro 64bit.
That does sound strange, getting different results on the two machines. Pragmatically, you can work around the problem (without actually understanding it) by making sure that you execute an End statement. (You can put it in the QueryUnload event to make sure it's hit if the user clicks the "X".)

Run a Applescript on a locked Mac by keypress or alternative options

I have an Applescript written for a specific purpose that launches an application and performs a couple of tasks. I would like to be able to run this script by pressing a key on my keyboard, however, while the Mac is locked. (Note: Mac is only locked, not "asleep")
I'm open to alternative options, if such (or more efficient) options exist.
The above scenario is my "ideal" configuration, the only thing I don't have flexibility on, is that the mac must stay locked for this process.
I have full administrative access to the Mac.
I would suggest you use ssh to "get into" your Mac while the screen is locked and then run your script from the shell. You would probably use osascript for that if it is Applescript, by the way.
In order to do this, you will need to ensure the sshd is running on your Mac by going to the Apple Menu, then System Preferences then Sharing and check the box beside Remote Login.
In order to ssh into your Mac, you could use another Mac, or a PC or the free iPhone/iPad app called Terminus which is brilliant for remotely controlling machines via ssh.
Rather than have to run a command when you log in, a technique which I often use is to have a user who can only execute a specific function and the very act of logging in does that function and then logs the user out again. So, for example, if I wanted a login that can reboot a machine, I would create a new user called reboot and instead of that user having bash in /etc/passwd as his shell, he would have /sbin/reboot as his shell. Just a thought - YMMV.
I've tried this same thing and when the Mac becomes locked, normal processing will not occur but slow to a crawl, so I had to turn my screens off instead of locking my Mac. There is no way around it.

How does Steam prompt me to delete game data when I delete the app?

I recently tried delete Steam.app from Mac OS X 10.10.5. As soon as I hit Command-Backspace to remove the application, this dialog appeared:
The text reads:
Steam still has some game content stored at /Users/apaidnerd/Application Support/Steam/SteamApps. Do you want to delete the game content as well?
[Leave files] [Reveal in Finder] [Delete files]
How did the Steam application cause this to happen? If I'm developing an app for Mac OS X, where is this documented?
You were close. It looks to me like the launch agent "com.valvesoftware.steamclean" is responsible for this. Unloading the "com.valvesoftware.steam.ipctool" agent/service may effectively disable the other one.
The steamclean agent's description plist file is in ~/Library/LaunchAgents. It doesn't poll. It has a WatchPaths key, which asks launchd to run it when that path changes. launchd uses file system notification techniques to monitor that, which also don't rely on polling.
When that happens, the agent runs the program ~/Library/Application Support/Steam/SteamApps/steamclean, which is not in the Steam.app bundle. It couldn't be, because the app bundle has been moved or renamed, so launchd would not be able to find the program for the agent. steamclean is what shows the dialog. You can confirm this by reproducing the dialog and leaving it open. Then, use Activity Monitor to list the processes and filter for "steam".
Figured it out. Steam has a launchctl agent that runs (com.valvesoftware.steam.ipctool) out of the /Applications/Steam.app/ directory. If I stop the daemon and remove the Steam.app, no dialog appears.
It must poll to see if its executable path still exists and prompts the user if goes missing. Also, the inactive-looking title bar implies that this dialog is indeed coming from some sort of background process.

Getting blank (black) screenshot with automated screen capture using task scheduler

I am trying to troubleshoot a software bug in which we think an app may not be launching every time it is called. To test this, I created a series of scheduled tasks to launch the app, take a screenshot, and then close the app. These tasks are all run through .bat files.
Everything works fine except for the screenshots. When run as a scheduled task, the .jpg from the screen capture always shows a black box. If I execute the .bat file normally, the screenshot looks fine.
I am using nircmd to send the screen capture command and save it to a folder. The command I am using is as follows:
nircmd.exe savescreenshot "C:\File Path\"~$currdate.MMddyyyy$-~$currtime.HHmmss$.jpg
I am using Windows 7.
The task created is probably running in its own logon session, even if it is running as the same user.
Make your scheduled task run as the currently logged on user.
Keith Brown wrote an old but still valid detailled explaination of the logon session SID. The relevant part is at the middle, under "http://www.microsoft.com/msj/0599/security/security0599.aspx"
You can also poke around the access rigths with EnumWinStaGui.

Resources