Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

Help with file delete location

As of now, I have a LaunchAgents plist that enacts the script below at midnight. The code only deletes specified files on the Admin desktop. I would like it to wipe said files off any regular users desktop. If it searches and clears the Admin too, thats ok, as long as it works and the regular priveledge users. Also, in the plist below, what would I change to make it work twice a day. (noon and midnight)



tell application "System Events"

-- find all pdf files that were created between 1 hour and 26 hours ago that start with face_sheet

set lowerLimit to ((current date) - 1 * days - 2 * hours)

set upperLimit to ((current date) - 1 * hours)

delete (files of desktop folder of user domain whose type identifier is "com.adobe.pdf" and creation datelowerLimit and creation date < upperLimit and name starts with "face_sheet_")

end tell


<?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>Label</key>

<string>user.delete.daily</string>

<key>ProgramArguments</key>

<array>

<string>osascript</string>

<string>/path/to/script file.scpt</string>

</array>

<key>StartCalendarInterval</key>

<dict>

<key>Hour</key>

<integer>0</integer>

<key>Minute</key>

<integer>0</integer>

</dict>

</dict>

</plist>

MacBook Pro, OS X Mountain Lion (10.8.2), 2.3 GHz i5, 4GB RAM, OCZ 120GB SSD

Posted on Feb 27, 2013 2:27 PM

Reply
Question marked as Best reply

Posted on Feb 27, 2013 3:44 PM

I think you're missing two things about LaunchAgents.


First off, if you put a .plist in a user's ~/Library/LaunchAgents directory then that script only fires for that user.


Put the .plist in /Library/LaunchAgents, though, and it applies to every user. So at first glance your solution would be to move the .plist into /Library/LaunchAgents and you're done - at least as far as making sure the script applies to every user.


The second issue, though, is that LaunchAgents only run when a user is logged in. If no user is logged in at midnight then the script isn't going to do anything.


If you want the script to run when no user is logged in then it needs to be setup as a LaunchDaemon, not a LaunchAgent, but then you have other issues related to accessing multiple user's home directories at once, which is a particular problem if the user's home directories are mounted. Without knowing more about the setup it's hard to be more specific.

3 replies
Question marked as Best reply

Feb 27, 2013 3:44 PM in response to MacMan240

I think you're missing two things about LaunchAgents.


First off, if you put a .plist in a user's ~/Library/LaunchAgents directory then that script only fires for that user.


Put the .plist in /Library/LaunchAgents, though, and it applies to every user. So at first glance your solution would be to move the .plist into /Library/LaunchAgents and you're done - at least as far as making sure the script applies to every user.


The second issue, though, is that LaunchAgents only run when a user is logged in. If no user is logged in at midnight then the script isn't going to do anything.


If you want the script to run when no user is logged in then it needs to be setup as a LaunchDaemon, not a LaunchAgent, but then you have other issues related to accessing multiple user's home directories at once, which is a particular problem if the user's home directories are mounted. Without knowing more about the setup it's hard to be more specific.

Feb 27, 2013 5:02 PM in response to MacMan240

It only needs to be run for one user who is almost always logged in.


Then put it in the ~/Library/LaunchAgents directory for that user and it'll only apply to him/her.


Any idea how to make the script run twice a day though?


Do you care at what time it runs? or just that it runs twice a day?

If you just want it to run twice a day, based on the user's login time, use the StartInterval key with a 43,200 value - that's 12 hours, so it will run 12 hours after the user logs in, and every 12 hours after that.


If you want multiple distinct times then add additional StartCalendarInterval keys, like:


<key>StartCalendarInterval</key>

<dict>

<key>Hour</key>

<integer>0</integer>

<key>Minute</key>

<integer>0</integer>

</dict>

<dict>

<key>Hour</key>

<integer>12</integer>

<key>Minute</key>

<integer>15</integer>

</dict>


The above example will run at midnight (00:00) and 12:15pm every day.

Help with file delete location

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple ID.