Skip to content

Instantly share code, notes, and snippets.

@januz
Created August 4, 2013 07:16
Show Gist options
  • Save januz/6149535 to your computer and use it in GitHub Desktop.
Save januz/6149535 to your computer and use it in GitHub Desktop.
on run argv
# fill in your working hours here
set start_time_morning to convert_minutes(8.0)
set finish_time_morning to convert_minutes(12.3)
set start_time_afternoon to convert_minutes(14.0)
set finish_time_afternoon to convert_minutes(18.0)
# -----------------------------------------
set start_time_morning_secs to start_time_morning * 3600
set finish_time_morning_secs to finish_time_morning * 3600
set start_time_afternoon_secs to start_time_afternoon * 3600
set finish_time_afternoon_secs to finish_time_afternoon * 3600
# get current time
set the_date to current date
set the_time to time of the_date
# set run time
if (count of argv) is 0 then
if the_time is greater than start_time_morning_secs and the_time is less than finish_time_morning_secs then
set run_time to (finish_time_morning_secs - the_time)
else if the_time is greater than finish_time_morning_secs and the_time is greater than start_time_afternoon_secs and the_time is not greater than finish_time_afternoon_secs then
set run_time to (finish_time_afternoon_secs - the_time)
else
tell application "System Events" to display dialog "The current time is outside of your working hours!"
error number -128
end if
set run_time to round (run_time / 60)
else if (count of argv) is 1 then
set the_argument to item 1 of argv
if the_argument does not contain "." then
set run_time to the_argument as integer
else if the_argument contains "." then
set finish_time_secs to convert_minutes(the_argument) * 3600
if finish_time_secs is greater than the_time then
set run_time to round ((finish_time_secs - the_time) / 60)
else
tell application "System Events" to display dialog "Chosen time is in the past!"
error number -128
end if
else
tell application "System Events" to display dialog "Time has wrong format!"
error number -128
end if
else
tell application "System Events" to display dialog "Too many arguments used!"
error number -128
end if
# get password
tell application "Usable Keychain Scripting"
tell current keychain
set myPass to (password of first generic item ¬
whose name contains "SelfControl")
-- eliminate invisible characters, or "gremlins," from password
set x to quoted form of myPass
set myPass to do shell script "echo " & x & " | perl -pe 's/[^[:print:]]//g'"
end tell
end tell
# start SelfControl
tell application "SelfControl" to activate
tell application "System Events"
tell process "SelfControl"
tell slider of window "SelfControl" to set value to run_time
click button "Start" of window "SelfControl"
end tell
tell window 1 of process "SecurityAgent"
with timeout of 15 seconds
repeat
set tryAgain to false
try
set value of text field 2 of scroll area 1 of group 1 to myPass
on error
delay 1
set tryAgain to true
end try
if not tryAgain then exit repeat
end repeat
click button 2 of group 2
end timeout
end tell
end tell
run_time
end run
on convert_minutes(input)
set s to input as string
set AppleScript's text item delimiters to "."
set the_hour to text item 1 of s
set the_minutes to text item 2 of s
set converted_minutes to ((the_minutes / 60 * 100) as integer) as string
set output to the_hour & "." & converted_minutes
return output
end convert_minutes
@nvrtdfrst
Copy link

So for Yosemite, the System Agent dialog box for the password has a different UI. This simple change will allow it to work in Yosemite:

For Yosemite, the SecurityAgent dialog box is different. This will work:

    set value of text field 2 to myPass
    click button 2

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment