Run script or app at logon for all users on the Mac

0

I want my Mac to auto-trash some cache-files from Adobe for all the users that log on to the computer. I am really new to this, so I'm sorry if my question is obvious. I have made a script and/or app that does the job. I read that I should make a .plist file and put it in /Library/LaunchAgents. I found a example plist-file online, but it doesn't seem to work. It might be that I completely misunderstood or that there is just some example text I need to replace or that I made a mistake in the text I did replace. Any suggestions would be very welcomed. I'm stuck.

This is the plist-file I saved under LaunchAgents:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
          <key>RunAtLoad</key>
          <true/>
          <key>Label</key>
          <string>user.script.runatload</string>
          <key>ProgramArguments</key>
          <array>
                    <string>osascript</string>
                    <string>/Library/Application Support/folder/cleanup.scpt</string>
          </array>
</dict>
</plist>

Kirsti

Posted 2015-04-24T16:19:48.097

Reputation: 1

What are you actually trying to achieve? As it stands, this looks like an XY Problem

– Tetsujin – 2015-04-24T18:38:15.583

Check if escaping the space in Application Support helps. And if you haven't already either restart the mac or load the plist using launchctl, the plists in /Library/LaunchAgents get read (not executed) on boot normally I believe, so they may not have been picked up simply by logging out and back in. Also maybe choose a more descriptive label than user.script.runatload, though that's not your problem. – Camden Narzt – 2015-04-24T21:50:34.093

@Tetsujin Sorry I can see it was a clumsy way to ask, but like I said in the beginning, I want my Mac to automatically empty some folders for all users that log in. I already made a script or an app that does this job, but I don't know how to launch it when any user log in. It works if I set it up under my own login items, but I don't want to make all the users do that. Is there a simpler way to do this from the beginning? – Kirsti – 2015-04-26T10:53:42.033

@CamdenNarzt Thanks for the input. :) I tried changing the path for the script to /Library/cleanup/cleanup.scpt but still nothing happened after a reboot. I don't know anything about launchctl, and can't seem to make any sense of what I find online. Does it do the same as a restart would? Any other ideas or suggestions? – Kirsti – 2015-04-26T10:56:39.140

@CamdenNarzt or anybody else; can it be related to the fact that my plist-file doesn't look like the other ones, just like a white document. It was saved from textedit as .rtf and I just changed it manually to .plist. – Kirsti – 2015-04-26T11:05:04.450

Absolutely, you need to create it as a plain text doc (in TextEdit press ⌘+shift+T) then save as .plist. An rtf doc isn't always a flat file, and it always contains unwanted formatting characters, which won't work in this situation. – Camden Narzt – 2015-04-26T16:07:56.607

@CamdenNarzt Thanks again for your help. Can you believe it still doesn't work?? I was able to covert to plain text and save as plist, but still nothing happens. Still my plist-file doesn't look like the others, just like a plain doc, my plist-extention is hidden, but the others are showing. Could it still be a problem there? Or any other suggestions? – Kirsti – 2015-04-27T19:18:41.727

Ok, your plist is valid xml, your contents look fine to me, I'm assuming that the problem is still in the way the file is saved to disk. If you right click the file and "get info" what is the name under the "Name & Extension" heading? Does it have .plist as the last extension? If so, I'll have to start suggesting some command line commands to run to figure out what's up. – Camden Narzt – 2015-04-27T22:45:32.807

@CamdenNarzt Under Name & Extension it says "user.script.runatload.plist" and if I check "hide extension" the .plist disappears. Any command line to help me further would be very much appreciated. :) You don't think it could have anything to do with the placement of the files? Where is it common to place files like these? – Kirsti – 2015-05-06T15:43:28.913

@CamdenNarzt In Textedit when saving as plist, there is an option what code to use, it is default set to Unicode (UTF-8), is this correct? also, I left all these parameters like the were in my example, does any of them need to be changed?

      <key>RunAtLoad</key>
      <true/>
      <key>Label</key>
      <string>user.script.runatload</string>
      <key>ProgramArguments</key>
      <array>
 – Kirsti  – 2015-05-06T16:08:19.687

@CamdenNarzt sorry for lots of comments here, but I was too late to edit. Could it be that I do need Xcode to create a plist-file? – Kirsti – 2015-05-06T16:31:35.420

You don't have to use xcode to create a plist, I never do. The filename is fine, the file needs to be in /Library/LaunchAgents/ (which is the library folder in the root of your hard-drive not the one in your home dir) like you said in the original question. Try running the following commands on the command line: file /Library/LaunchAgents/user.script.runatload.plist and ls -l /Library/LaunchAgents/user.script.runatload.plist and copy the results here. And if you run cat /Library/LaunchAgents/user.script.runatload.plist do the contents of the file look right? – Camden Narzt – 2015-05-06T17:41:50.263

@CamdenNarzt First one gives me /Library/LaunchAgents/user.script.runatload.plist: XML document text Second one gives me: -rw-r--r--@ 1 n23403 wheel 602 May 7 15:11 /Library/LaunchAgents/user.script.runatload.plist – Kirsti – 2015-05-07T15:57:09.273

@CamdenNarzt Third one looks like my plist:

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">

<plist version="1.0"> <dict>           <key>RunAtLoad</key>           <true/>           <key>Label</key>           <string>user.script.runatload</string>           <key>ProgramArguments</key>           <array>                     <string>osascript</string>                     <string>/Library/cleanup/cleanup.scpt</string>           </array> </dict> – Kirsti – 2015-05-07T16:01:09.900

The semi colon on the end of the doctype line shouldn't be there and the </plist> closing tag is missing at the end, everything else looks ok. – Camden Narzt – 2015-05-07T16:23:20.060

@CamdenNarzt - I don't know why it came out like that on my comment. The semicolon isn't really there and I suppose I forgot to copy/paste the last line with the </plist> It still looks like the first text I pasted in, I doubled check by running the command again, it all looks right. Are we running out of ideas? – Kirsti – 2015-05-08T15:34:35.370

Slowly, yes. For the meantime add a logging key/value pair such as <key>StandardOutPath</key><string>/tmp/launchagent.log</string><key>StandardErrorPath</key><string>/tmp/launchagent_err.log</string> , create those files with touch /tmp/launchagent{,_err}.log and then make sure your script emits output before any other action, even if it's just an echo hi. Then reboot and log in and check the logs' contents. – Camden Narzt – 2015-05-08T22:31:06.417

I'm sorry I'm not much of a programmer, so you need to explain things with baby steps. Where do I put all these things? In the plist? Does it matter where in the file it is? And where to I check the log, is it the normal log for the mac? What does it mean "create those files with.." – Kirsti – 2015-05-09T17:44:16.853

Yes, they go in the plist, after the </array> line would be fine. touch is another command line utility that creates files, so that they can be written to. You can check the logs with any program, be it cat on the command line or TextEdit, the logs are at the paths: /tmp/launchagent_err.log and /tmp/launchagent.log. – Camden Narzt – 2015-05-11T00:35:17.260

@CamdenNarzt So, babysteps, I put the text "<key>StandardOutPath</key><string>/tmp/launchagent.log</string><key>StandardErr‌​orPath</key><string>/tmp/launchagent_err.log</string>" in my plist and restarted my computer. Then I went to terminal on my mac and wrote "cat /tmp/launchagent_err.log" This is what comes out: cat: /tmp/launchagent_err.log: No such file or directory. Was this the right thing to do, it doesn't seem like it? – Kirsti – 2015-05-14T12:27:21.743

almost, for some reason launchd can't create its own log files, which is why I mentioned touch, that program creates the file for launchd; which should be able to use it thereafter. So try touch /tmp/launchagent{,_err}.log and since you didn't mention updating your script to echo anything at the beginning, I'll remind you to do that too. – Camden Narzt – 2015-05-15T01:29:54.647

Also can you run launchctl list | fgrep runatload on the command line and let me know what gets returned? – Camden Narzt – 2015-05-15T01:33:52.853

@CamdenNarzt I am sorry I'm quite useless at this. What exactly do I write where to make an echo? I just realized, is it important to mention that my script is made with apple script? – Kirsti – 2015-05-19T18:31:56.750

ok, I kinda skipped over that detail, I was thinking in bash script. In applescript you'll need to insert the line do shell script "echo hi" before anything else, unless you have a line at the beginning that starts with: #!/ then put it after that line. – Camden Narzt – 2015-05-19T18:36:18.570

@CamdenNarzt Nothing really happens, here's a copy from terminal: Last login: Wed May 20 14:40:27 on console computername:~ username$ touch /tmp/launchagent{,_err}.log computername:~ username$ launchctl list | fgrep runatload computername:~ username$ – Kirsti – 2015-05-20T12:45:23.193

Ok so that means the plist isn't being picked up by launchd on boot. Which is very strange indeed since it has universal read permissions. If you run launchctl load /Library/LaunchAgents/user.script.runatload.plist and launchctl start user.script.runatload does anything get written to the log? – Camden Narzt – 2015-05-20T18:04:42.193

No answers