Perform an Applescript every week at a certain time automatically?

Hi,
I’m fairly new to Applescripting, and i work alot with the Apple XServes, but i’ve found out (through unwanted experience) that the XServes can actually lose track of the time.euro.apple.com date and time ntp, and become out of sync with it. I’ve found a couple of scripts that work perfectly if i run them manually, but i need them to be run automatically every week at a certain time. I’ve investigated the cron and launchd dimensions of it all, but to no avail!! i got sooo confused with it i gave up, but that seems to be the only way of doing it. Is it an application i need to create or is it just a script? Whatever it is, it needs to run in the background and require (very) little cpu usage, as these serevrs are ingesting full HD video, and so require enough power for that!
Any help will be greatly appreciated!!
Cheers
Dan

Model: MacBook
Browser: Safari 419.3
Operating System: Mac OS X (10.4)

Dan:

A Stay Open Application would be the simplest, and requires extremely little CPU usage. You do get an icon in your Dock, which may be OK; at least that way you know it is running in the background for you.

Take a quick look at the article linked in the above paragraph and try this:

on idle
	--Put time script here
	return (7 * days)
end idle

That should do the trick, and the script should repeat itself every 7 days until you shut it down. If your system ever goes offline, you can set up the application to be a Startup Item and never have to worry about it again.

Hi,

an stay open script is the easiest way, but a launchd agent is the best
take a look at this installer
You can hard-code the schedule part of the installer

<key>StartCalendarInterval</key> <dict> <key>Hour</key> <integer>" & hr & "</integer> <key>Minute</key> <integer>" & mn & "</integer> </dict>
with this, it includes the weekday (0=sunday) and triggers the script (simple complied AppleScript) for example every monday at 12:00 (pm)

<key>StartCalendarInterval</key> <dict> <key>Hour</key> <integer>12</integer> <key>Minute</key> <integer>0</integer> <key>Weekday</key> <integer>1</integer> </dict>

thanks guys.

I’v downloaded Launchd Editor.app which is freeware and creates the plist file i need but never seems to save it in the correct place. does the plist need to be in a specific place or can it be anywhere? and if i change the plist info i have here with the one from your code, should it work straight away?

Cheers

Dan

the plist file must be in ~/Library/LaunchAgents or in /Library/LaunchAgents
and the agent must be loaded with launchctl

Hi Stefan,

i tried using your script but to no avail…i’m just getting so confused with things!!

i just need to know where the files need to be placed (e.g. the script file (and can this be an applescript file?) and the .plist file). i’m sorry abot this, but i’m just soooo confused!! i’ve tried several things but can’t get it to work. it’s onlky worked a couple of times but i don’t know how!!

here is the applescript i have so far;

property pListLabel : "tester2" -- name of the launchd agent (without .plist extension)
property triggeredScriptName : "Sync Clock.scpt" -- name of the script to execute
property hr : "13" -- execute script every day at hr:mn (24 hr mode)
property mn : "24"

set triggeredScriptLocation to path to scripts folder as Unicode text -- the folder (~/Library/Scripts/) to put in the script to execute

-------

set userLibraryPath to (path to home folder as Unicode text) & "Library:"
try
	set LaunchAgentsFolder to (userLibraryPath & "LaunchAgents:") as alias
on error
	tell application "Finder" to set LaunchAgentsFolder to make new folder at userLibraryPath with properties {name:"LaunchAgents"}
end try
set pListfile to ((LaunchAgentsFolder as Unicode text) & pListLabel & ".plist")

set PLIST_File to "<?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>" & pListLabel & "</string>
   <key>LowPriorityIO</key>
   <true/>
   <key>Program</key>
   <string>/Users/danielsinclair/Library/Scripts/Clock Sync.scpt</string>
   <key>ProgramArguments</key>
   <array>
       <string>/Users/danielsinclair/Desktop/Sync Clock.app</string>
       <string>" & POSIX path of triggeredScriptLocation & triggeredScriptName & "</string>
   </array>
   <key>StartCalendarInterval</key>
   <dict>
       <key>Hour</key>
       <integer>" & hr & "</integer>
       <key>Minute</key>
       <integer>" & mn & "</integer>
   </dict>
</dict>
</plist>
"

set loadBut to button returned of (display dialog "Load or Unload plist file" buttons {"Cancel", "Unload", "Load"} default button 3)
if loadBut is "Load" then
	try
		set ff to open for access file pListfile with write permission
		write PLIST_File to ff as «class utf8»
		close access ff
	on error
		try
			close access file target
		end try
		display dialog pListLabel & " couldn't be created" buttons {"Cancel"} default button 1
	end try
	launchctl("load", pListfile, pListLabel)
else
	set launchdFiles to list folder LaunchAgentsFolder without invisibles
	try
		set unloadFile to item 1 of (choose from list launchdFiles with prompt "choose a file to unload:" default items {pListLabel & ".plist"})
		launchctl("unload", ((LaunchAgentsFolder as Unicode text) & unloadFile), text 1 thru -7 of unloadFile)
	end try
end if

----------------------
on launchctl(load, pListfile, N)
	try
		set launchctlCmd to "launchctl " & load & " -w " & quoted form of POSIX path of pListfile
		do shell script launchctlCmd
		if load is "unload" then do shell script "rm " & quoted form of POSIX path of pListfile
		display dialog N & " successfully " & load & "ed" buttons {"OK"} default button 1
	on error
		display dialog load & " failed" buttons {"OK"} default button 1 with icon stop
	end try
end launchctl

HELP!!

thanks

Dan

o.k. no problem,
please tell me, when exactly is the weekly recurrence of the script?

Stefan,

at the moment i’m just trying to get it to work, and then i will personailse it to each of my custmores needs as and when i configure the XServes. Is there no other software around that will do all of this for me?! i’ve managed to get a few freeware apps that run the app in the background etc etc but nothing that seems to be up-to-scratch!

Thanks

Dan

try this version,
adjust, hr, mn and wkday
you will be prompted to choose the .scpt file, then it will be copied to the proper place.

Note: in a scheduled script called by launchd no user interaction is possible


property pListLabel : "tester2" -- name of the launchd agent (without .plist extension)

property hr : "18" -- execute script every day at hr:mn (24 hr mode)
property mn : "0"
property wkday : "1" -- values "0" (sunday) - "6" (saturday)

------
set triggeredScriptLocation to path to scripts folder as Unicode text -- the folder (~/Library/Scripts/) to put in the script to execute
set thescript to choose file of type "com.apple.applescript.script" with prompt "Choose script file"

tell application "Finder" to set scriptPath to duplicate thescript to folder triggeredScriptLocation replacing yes
set triggeredScriptName to name of (info for (scriptPath as alias))
set userLibraryPath to (path to home folder as Unicode text) & "Library:"
try
	set LaunchAgentsFolder to (userLibraryPath & "LaunchAgents:") as alias
on error
	tell application "Finder" to set LaunchAgentsFolder to make new folder at userLibraryPath with properties {name:"LaunchAgents"}
end try
set pListfile to ((LaunchAgentsFolder as Unicode text) & pListLabel & ".plist")

set PLIST_File to "<?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>" & pListLabel & "</string>
	<key>LowPriorityIO</key>
	<true/>
	<key>Program</key>
	<string>/usr/bin/osascript</string>
	<key>ProgramArguments</key>
	<array>
		<string>osascript</string>
		<string>" & POSIX path of triggeredScriptLocation & triggeredScriptName & "</string>
	</array>
	<key>StartCalendarInterval</key>
	<dict>
		<key>Hour</key>
		<integer>" & hr & "</integer>
		<key>Minute</key>
		<integer>" & mn & "</integer>
		<key>Weekday</key>
		<integer>" & wkday & "</integer>
	</dict>
</dict>
</plist>
"

set loadBut to button returned of (display dialog "Load or Unload plist file" buttons {"Cancel", "Unload", "Load"})
if loadBut is "Load" then
	try
		set ff to open for access file pListfile with write permission
		write PLIST_File to ff as «class utf8»
		close access ff
	on error
		try
			close access file target
		end try
		display dialog pListLabel & " couldn't be created" buttons {"Cancel"} default button 1
	end try
	launchctl("load", pListfile, pListLabel)
else
	set launchdFiles to list folder LaunchAgentsFolder without invisibles
	try
		set unloadFile to item 1 of (choose from list launchdFiles with prompt "choose a file to unload:" default items {pListLabel & ".plist"})
		launchctl("unload", ((LaunchAgentsFolder as Unicode text) & unloadFile), text 1 thru -7 of unloadFile)
	end try
end if

----------------------
on launchctl(load, pListfile, N)
	try
		set launchctlCmd to "launchctl " & load & " -w " & quoted form of POSIX path of pListfile
		do shell script launchctlCmd
		if load is "unload" then do shell script "rm " & quoted form of POSIX path of pListfile
		display dialog N & " successfully " & load & "ed" buttons {"OK"} default button 1
	on error
		display dialog load & " failed" buttons {"OK"} default button 1 with icon stop
	end try
end launchctl

Stefan,

THANK YOU THANK YOU THANK YOU THANK YOU!!! It works a treat!! you saved me a great deal of problems!!

I can’t thank you enough!!

Do you have an iChat address?!

Cheers

Dan

You can find it in my profile