vivek
vivek

Reputation: 975

How to automate an app to run daily in OSX

I am very new to AppleScript programming. This is my code to open an app at 6 PM. I saved this code an as application, and now I want to open this application automatically daily at 6 PM without any user interaction. I want to do this programmatically; no cron job, no Automator, no calendar, no user preferences settings. I want code to trigger it. Is it possible?

set targetTime to "6:00:00 PM"


display dialog targetTime -- wait until we reach the target date/time
repeat while (current date) is less than date targetTime
    -- should be 60

end repeat


tell application "Myapp"

    activate

end tell

-- get the time in the desired format
on getTimeInHoursAndMinutes()

    -- Get the "hour"
    set timeStr to time string of (current date)
    set Pos to offset of ":" in timeStr
    set theHour to characters 1 thru (Pos - 1) of timeStr as string
    set timeStr to characters (Pos + 1) through end of timeStr as string

    -- Get the "minute"
    set Pos to offset of ":" in timeStr
    set theMin to characters 1 thru (Pos - 1) of timeStr as string
    set timeStr to characters (Pos + 1) through end of timeStr as string

    --Get "AM or PM"
    set Pos to offset of " " in timeStr
    set theSfx to characters (Pos + 1) through end of timeStr as string

    return (**strong text**theHour & ":" & theMin & " " & theSfx) as string
end getTimeInHoursAndMinutes

Upvotes: 1

Views: 540

Answers (2)

regulus6633
regulus6633

Reputation: 19032

While I appreciate Mark Setchell's answer, since you're already using applescript then just calculate the seconds until 6PM in applescript. There's no need to have a special shell script just to calculate time. Try this...

set targetTime to "6:00:00 PM"
display dialog targetTime -- wait until we reach the target date/time

set curTime to current date
set futureTime to date (targetTime)
set secondsToFutureTime to futureTime - curTime

-- check if it's after 6PM on the current day
-- and if so then get 6PM on the next day
if secondsToFutureTime is less than 0 then
    set futureTime to futureTime + (1 * days)
    set secondsToFutureTime to futureTime - curTime
end if

delay secondsToFutureTime

tell application "Myapp" to activate

Upvotes: 3

Mark Setchell
Mark Setchell

Reputation: 207445

The proper way to do this is with launchctl and launchd, by the way.

I am no Applescript expert at all but it is generally considered poor practice to sit busy-waiting thrashing the CPU repeatedly checking the time - it is normally better to work out how long it is till you want to do something and then just sleep without consuming any CPU till that time.

I normally do things in the bash shell as it seems simpler to me, so I would convert the times to seconds since the Unix Epoch (1st Jan 1970) and then everything is simple, whole numbers of seconds. So, if you go in Terminal and type

date +%s
1420711428

it is currently 1420711428 seconds since the Epoch. If I want to know the time in seconds since the Epoch at 18:00 tonight, I just type this in Terminal:

date -j 1800.00 +%s
1420740000

Now, i can see I need to wait 1420740000 - 1420711428 seconds till 18:00. I can do that easily in a shell script, and also convert your 18:00:00 into the format that the date command expects like this:

#!/bin/bash
#
# Get seconds till time specified by parameter
# Usage:
# till 18:00:00
# 
# Get current time in seconds since epoch
now=$(/bin/date +%s)
#echo now: $now

# Get passed in time in seconds since epoch
# Remove first colon and change second to a dot, so 18:00:00 becomes 1800.00 like "date" expects
t=${1/:/}
t=${t/:/.}
then=$(/bin/date -j $t +%s)
#echo then: $then

# Calculate difference
diff=$((then-now))
echo $diff

I then save that as till and make it executable with

chmod +x till

then I can do

./till 18:00:00
28282

and see it is 28282 seconds till 18:00:00.

So, I would paste the script above inside your Applescript and then use

do shell script "<pasted code>"

and simply wait the number of seconds till you want, or change the last line of the script to sleep $diff so the script doesn't finish till your timer expires.

Upvotes: 1

Related Questions