Is there a way to add (or subtract) minutes from the current time from within Applescript?
I’ve searched and found several possible solutions, but haven’t got anything to work yet.
Hi,
I don’t know exactly, what you want,
but this adds 30 minutes to the current date
set a to (current date) + 30 * minutes
the same line with the minus sign subtracts 30 minutes
Hello Sankt Gallen from Switzerland ,
Thank you for your reply.
It appears your solution adds 30 minutes to a variable…what I was intending was to actually change the system time on the Mac-mini by adding or subtracting 30 minutes. (or any increment of time)
In other words, after the script runs the system time has been adjusted by the desired amount.
thank you again Sankt Gallen!
This is possible with UI scripting,
the script sets the system time to t+30 minutes
tell application "System Preferences"
reveal anchor "DateTime" of pane id ¬
"com.apple.preference.datetime"
end tell
tell application "System Events"
tell process "System Preferences"
tell UI element 7 of group 1 of tab group 1 of window 1
set currentDate to value
set value to currentDate - 30 * minutes
end tell
click button "Save" of group 1 of tab group 1 of window 1
end tell
end tell
Hi,
Another way is to use unix ‘date’ command. The bad part about it is that you need to enter the admin password or hide it in the script. If interested, write back.
gl,
Sankt Gallen, Switzerland
Registered: 2006-10-21
Posts: 563
E-mail PM Website Re: Adding 30 minutes to the system timemacminicrazy wrote:
In other words, after the script runs the system time has been adjusted by the desired amount.
This is possible with UI scripting,
the script sets the system time to t+30 minutes
Applescript:
tell application “System Preferences”
reveal anchor “DateTime” of pane id ¬
“com.apple.preference.datetime”
end tell
tell application “System Events”
tell process “System Preferences”
tell UI element 7 of group 1 of tab group 1 of window 1
set currentDate to value
set value to currentDate - 30 * minutes
end tell
click button “Save” of group 1 of tab group 1 of window 1
end tell
end tell
Thanks Sankt Gallen, you opened up a whole new facet of Applescripting for me with UI.
When I run your example I get:
Applescript Error: System Events got an error: NSReceiverEvaluationScriptError: 4
and the word ‘value’ is highlighted on this line “set currentDate to value”
Any ideas?
David
Hi David,
UI scripting is tricky.
Maybe different languages (I use english) or a different system version can cause the script to fail
On my machine the script works fine.
Probably you can find out what’s going wrong downloading Apple’s UIElementInspector, to check the UI elements
Remember to “Enable access for assistive devices”.
You might also be interested in PreFab’s UI Browser.
Hey, thanks for all your help
I guess I have to ‘dive in’ to UI to figure this out…
Bruce, After I enabled “access for assistive devices”, the script started working.
This is similar question that i’ve been trying to do for a long time. I originally was trying to set it up through automator but anyways heres what i’m trying to do:
Turn off: “Set date & time automatically”
Then set date and time to whatever, lets say 1/1/2001 or 2/7/2007
Then in a seperate script, turn back on “Set date & time automatically”
You guys really seem to know what you’re doing, hope this isn’t too hard
-Phil
Hi Phil,
something like this, you will be prompted to choose the mode
property theDate : "2/7/2007" -- must fit to current international date format settings
tell application "System Preferences"
reveal anchor "DateTime" of pane id ¬
"com.apple.preference.datetime"
end tell
set mode to button returned of (display dialog "Automatic or manual time" buttons {"Cancel", "Automatic", "Manual"})
set D to date theDate
tell application "System Events"
tell process "System Preferences"
tell group 1 of tab group 1 of window 1
if mode is "Manual" then
if value of checkbox 1 is 1 then click checkbox 1
set value of UI element 6 to D
click button "Save"
else
if value of checkbox 1 is 0 then click checkbox 1
end if
end tell
end tell
end tell
thank you so much! Everythings working great now!! You have no idea how much more productive this will make my lab work, thanks again!