Saturday, August 17, 2019

#1 2007-05-29 04:06:38 pm

horaliar
Member
Registered: 2006-06-01
Posts: 16

Run Applescript every 15 minutes

Hi, I need to run an applescript every 15 minutes. I have read several posts which suggest iCal, but I haven't found a way to do this. I have also read a few others that suggest cron, but I don't know the command to run an applescript from the terminal. Any ideas will be appreciated. Thanks!

Offline

 

#2 2007-05-29 04:10:37 pm

horaliar
Member
Registered: 2006-06-01
Posts: 16

Re: Run Applescript every 15 minutes

OK, just found the solution on another forum. A script is run when setting the alarm on a new event. I would still like more information, if anyone can provide, about running the applescript from the terminal window. I'm a Linux user, and I like to use every chance I have to run stuff from the terminal. Thanks!

Offline

 

#3 2007-05-29 04:13:46 pm

horaliar
Member
Registered: 2006-06-01
Posts: 16

Re: Run Applescript every 15 minutes

Just figured something out, iCal won't run the script every 15 minutes like I need to, it would run every day, week, etc., so I guess I do have to use crontab. I have scheduled events on a Linux web server using cron, for example, backing up a mysql database, but here I'm quite lost. Any ideas will be appreciated. Thanks!

Offline

 

#4 2007-05-29 04:54:11 pm

bevos
Member
From:: Auckland, New Zealand
Registered: 2007-01-30
Posts: 155
Website

Re: Run Applescript every 15 minutes

use the on idle. Create a stay open application. property values are remember, so its simple as checking last time with current time.

Applescript:

property currenttime : 0
property previousbreak : 0 as integer
property currentbreak : 0 as integer
on idle
   set currenttime to time of (current date)
   set currentbreak to (currenttime / 60 / 60 / 4) as integer
   if currentbreak is not equal to previousbreak then
       display dialog "It has been 15 minutes" --Do your script here
       set previousbreak to currentbreak
   end if
end idle

Have fun.

Bevan www.applescript.co.nz


Bevan, from www.applescript.co.nz, employ me here.

Offline

 

#5 2007-05-29 04:54:35 pm

James Nierodzik
Member
From:: Chicago
Registered: 2005-09-04
Posts: 1151

Re: Run Applescript every 15 minutes

To launch an AppleScript from the terminal you want to take a look at the man page for osascript

as for using cron while you can do that, I would suggest you take a look at using launchd as it is replacing cron.


Formerly known as myndcraft
(S+C) x (B+F)/T = V

Offline

 

#6 2007-05-29 05:02:51 pm

Craig Smith
Administrator
From:: Tacoma, WA
Registered: 2005-05-16
Posts: 1027
Website

Re: Run Applescript every 15 minutes

Running a script every 15 minutes using launchd is the simplest solution if you don't want a stay-open application.  This article will help get you started if you want to go that way.


Craig Smith

Offline

 

#7 2007-05-29 05:24:26 pm

bevos
Member
From:: Auckland, New Zealand
Registered: 2007-01-30
Posts: 155
Website

Re: Run Applescript every 15 minutes

I tripped out and didn't wait 15 minutes to test, My above script runs every 4 hours not 15 minutes, you times by 4 not divid

Applescript:

property currenttime : 0
property previousbreak : 0 as integer
property currentbreak : 0 as integer
on idle
   set currenttime to time of (current date)
   set currentbreak to (currenttime / 60 / 60 * 4) as integer
   if currentbreak is not equal to previousbreak then
       display dialog "It has been 15 minutes" --Do your script here
       set previousbreak to currentbreak
   end if
end idle

There's backgrounder from sentman.com if you want it running in the back ground
http://www.sentman.com/backgrounder/

have fun


Bevan, from www.applescript.co.nz, employ me here.

Offline

 

#8 2007-05-29 06:26:08 pm

Adam Bell
Administrator
From:: Nova Scotia, Canada
Registered: 2005-10-04
Posts: 4666

Re: Run Applescript every 15 minutes

To do what you have there, bevos, requires only this much:

Applescript:

on idle
   display dialog "It has been 15 minutes" giving up after 5
   return 15 * 60
end idle

where the return N is the length of time in seconds that the idle handler should wait before firing again. It loafs in the background in between. I the intermittent process takes very long, set the idle time a bit shorter because it starts again when the process before return N finishes. In the example, since the dialog takes 5 seconds if not acknowledged, then you might reduce the return time by that five seconds. This is not a clock, however.


Mac mini running 10.14.6

Offline

 

#9 2007-05-30 01:19:27 am

bevos
Member
From:: Auckland, New Zealand
Registered: 2007-01-30
Posts: 155
Website

Re: Run Applescript every 15 minutes

Learn something every day, return is cool. One I do checks the for week days, only run between 7am and 6pm, I guess I could work out the return, save the idle running all the time


Bevan, from www.applescript.co.nz, employ me here.

Offline

 

#10 2007-05-30 01:29:10 am

StefanK
Member
From:: St. Gallen, Switzerland
Registered: 2006-10-21
Posts: 11578
Website

Re: Run Applescript every 15 minutes

bevos wrote:

Applescript:


property previousbreak : 0 as integer
property currentbreak : 0 as integer


Hi bevos,

these coercions to integer are not needed, 0 in a property definition is a integer anyway


regards

Stefan

Offline

 

#11 2007-05-30 08:43:07 am

Adam Bell
Administrator
From:: Nova Scotia, Canada
Registered: 2005-10-04
Posts: 4666

Re: Run Applescript every 15 minutes

Craig Smith wrote:

Running a script every 15 minutes using launchd is the simplest solution if you don't want a stay-open application.  This article will help get you started if you want to go that way.


I was running a launchd initiated script on the hour, Craig, but went back to an on idle formulation for several reasons: first was that it didn't fire reliably on the hour, and secondly it seemed to run at such a low priority (no matter how I set that) that it would take much too long to run. Once an on idle script starts, it finishes without interruption.


Mac mini running 10.14.6

Offline

 

#12 2007-05-30 12:45:36 pm

horaliar
Member
Registered: 2006-06-01
Posts: 16

Re: Run Applescript every 15 minutes

Craig, I have a question about launchd, how do you indicate when to run it? I read "Getting Started with lauchd" from the developer connection in the Apple website, and I didn't find how to do this, just one example on how to run a program every day at 3:15. I'm pretty sure you've seen this plist, but here is a portion of it so you can see it:

<key>LowPriorityIO</key>
<true />
<key>Nice</key>
<inteer>1</intefer>
<key>StartCalendarInterval</key>
<dict>
   <key>Hour</key>
   <integer>3</integer>
   <key>Minute</key>
   <integer>15</integer>
</dict>
etc.

I'm imagining that if you place a 0 in the Hour intefer, the program will be run every day at 12:15 a.m.
Also, once you have created the plist file, how to you tell launchd to run it? Do you place something in LaunchAgests? I am completely new to MACs, so I get lost very often. Thanks!

Offline

 

#13 2007-05-30 01:09:42 pm

StefanK
Member
From:: St. Gallen, Switzerland
Registered: 2006-10-21
Posts: 11578
Website

Re: Run Applescript every 15 minutes

Hi,

you need something like this

[code]<?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>com.myAgent</string>
    <key>LowPriorityIO</key>
    <true/>
    <key>Program</key>
    <string>/usr/bin/osascript</string>
    <key>ProgramArguments</key>
    <array>
        <string>osascript</string>
        <string>full/path/to/script.scpt</string>
    </array>
    <key>StartInterval</key>
    <integer>900</integer>
</dict>
</plist>[/code]
StartInterval 900 is run every 15 minutes


regards

Stefan

Offline

 

#14 2007-05-30 02:00:01 pm

horaliar
Member
Registered: 2006-06-01
Posts: 16

Re: Run Applescript every 15 minutes

I'll try it out. thanks!

Offline

 

#15 2007-05-30 02:17:47 pm

Craig Smith
Administrator
From:: Tacoma, WA
Registered: 2005-05-16
Posts: 1027
Website

Re: Run Applescript every 15 minutes

When you have both your .plist and AppleScript files ready to go, you must put them both in their final resting places so you can get the paths correct for them. The .plist file needs to go in a certain folder so it can be automatically loaded at computer startup. (You can name the .plist file anything that you want. Even the .plist file extension is supposedly optional, but I prefer not to tempt fate, so I saved mine as WatchCB.plist.) The folder in which to save the .plist file must be titled LaunchAgents and must be located in your user's Library folder (yourHD:Users:YOURUSERNAME:Library:). The LaunchAgents folder is not normally installed with the system, so you will need to create it.  Once the .plist file is in its folder, it needs to be loaded (from the Terminal) so that it will run forever until you unload it.

To load the agent from the Terminal, you need to use the syntax, launchctl load -w FULLPATHTOTHE.PLISTFILE so that it will run. As long as the .plist file is in your user's LaunchAgents folder, and the Disabled key is false, it will run every time you start up your machine. The Disabled key is NOT required, so you can ignore it if you wish, and the agent will still load every time you power up the machine.

Good luck,


Craig Smith

Offline

 

#16 2007-05-30 02:24:42 pm

horaliar
Member
Registered: 2006-06-01
Posts: 16

Re: Run Applescript every 15 minutes

Thanks Craig. I have created the plist file b modifying the code provided by StefanK. I'll follow your instructions and let you guys know how it goes, I would like to get this script running automatically as soon as possible. Thanks!

Offline

 

#17 2007-05-30 02:46:35 pm

horaliar
Member
Registered: 2006-06-01
Posts: 16

Re: Run Applescript every 15 minutes

I got the following error when I typed in the command suggested:

M060601:/usr/bin prepress$ launchctl load -w /Users/prepress/Library/LaunchAgents/save-mail.plist
M060601:/usr/bin prepress$ Workaround Bonjour: Unknown error: 0

Any ideas?

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)