I’m trying to come up with something that will make Safari close the frontmost page then reopen it a user-specified number of hours/days later.
I could create a list of preset intervals, but I think it would extra cool if I could somehow click on a mini-calendar, the sort of thing you see in web forms.
Can this sort of thing be done via a script dialog, or is that a whole nuther kettle of fish?
Would the secret be to somehow involve iCal? I wouldn’t want the user to have to create an event manually…
set onTime to "10:01 AM" -- time you want to open URL
set offTime to "9:45 PM" -- time you want to close URL
repeat
set theTime to time string of (current date)
set {tid, text item delimiters} to {text item delimiters, ":"}
set curHour to text item 1 of theTime -- getting current hour
set theTime to text items 2 thru -1 of theTime as text
set curMin to text item 1 of theTime -- getting current min
set onHour to text item 1 of onTime -- gettin on hour
set offHour to text item 1 of offTime -- getting off hour
set onTime to text items 2 thru -1 of onTime as text
set offTime to text items 2 thru -1 of offTime as text
set {tid, text item delimiters} to {text item delimiters, space}
set curAP to text items 2 thru -1 of theTime as text --setting AM or PM current
set onMin to text item 1 of onTime -- getting on min
set offMin to text item 2 of offTime -- getting off min
set onAP to text items 2 thru -1 of onTime as text -- setting AM or PM on
set offAP to text items 2 thru -1 of offTime as text -- setting AM or PM off
set text item delimiters to tid
if curAP = onAP then
if curMin = onMin then
if curHour = onHour then
tell application "Safari"
open location "http://google.com" -- URL you want to open
end tell
end if
end if
end if
if curAP = offAP then
if curMin = offMin then
if curHour = offHour then
tell application "Safari" to close every window
end if
end if
end if
delay 60 -- delay before script checks time again.
end repeat
This code is untested and there is probably a better way. If this doesn’t work it will at least set you in the right direction for a strictly applescript solution. If you want to use ical create and even with alarm and have the alarm run an applescript that opens or closes safari.
set onTime to "11:52 PM"
set offTime to "11:53 PM"
repeat
set theTime to time string of (current date)
set curHour to text 1 thru ((offset of ":" in theTime) - 1) of theTime -- gets current hour
set theTime to text ((offset of ":" in theTime) + 1) thru -1 of theTime
set curMin to text 1 thru ((offset of ":" in theTime) - 1) of theTime -- gets current min
set curAP to text ((offset of " " in theTime) + 1) thru -1 of theTime -- gets current AM or PM
set onHour to text 1 thru ((offset of ":" in onTime) - 1) of onTime -- gets on hour
set offHour to text 1 thru ((offset of ":" in offTime) - 1) of onTime -- gets on hour
set onTime to text ((offset of ":" in onTime) + 1) thru -1 of onTime
set offTime to text ((offset of ":" in offTime) + 1) thru -1 of offTime
set onMin to text 1 thru ((offset of " " in onTime) - 1) of onTime -- gets on min
set offMin to text 1 thru ((offset of " " in offTime) - 1) of offTime -- gets off min
set onAP to text ((offset of " " in onTime) + 1) thru -1 of onTime -- gets on AM or PM
set offAP to text ((offset of " " in offTime) + 1) thru -1 of offTime -- gets off AM or PM
if curAP = onAP then
if curMin = onMin then
if curHour = onHour then
tell application "Safari"
open location "http://google.com"
end tell
end if
end if
end if
if curAP = offAP then
if curMin = offMin then
if curHour = offHour then
tell application "Safari" to close every window
end if
end if
end if
delay 5
end repeat
-- command out repeat to test for correct variables.
display dialog curHour & " " & curMin & " " & curAP
display dialog onHour & " " & onMin & " " & onAP
display dialog offHour & " " & offMin & " " & offAP
I know StefanK might want to slap me on the scripting had for not using text item delimiters… but shhhh I tested this one on my machine and it works perfect for me.
Thanks man.
My wording wasn’t clear. I want to have the script make Safari close the page, then reopen it later.
The big point being the open and close times will not be preordained. They’ll have to be captured on the fly.
Here’s what I want to do.
Have the script copy the address of the current (frontmost) webpage.
Close it.
Ask when to reopen it. (1 hour? 1 day? 1 week?)–I guess it would be easier to supply the choices instead of letting the user enter them
For the asking part you could use Pashua to display a calendar.
You can select whether you want to show an actual calendar + clock, or a textual display, like in iCal.
That ui element is called ‘date’.
Sorry for the confusion.
Here’s a configuration string for a date widget:
set config to "
d.type = date
d.label = Example date
d.default = 2007-05-30 17:00
d.time = 1
d.textual = 1
"
The ‘textual’ attribute determines the display: 1 = iCal-style widget, 0 = actual calendar.
The ‘time’ attribute determines whether you can set time. When ‘textual’= 0 then ‘time’=1 shows an analog clock.
Open the example script in the download, and replace its configuration string with this one.
And check this thread for a solution to a problem with dates in Pashua when running 10.6.