AppleScript not working from crontab but it works from terminal

Viewed 29

I have the following AppleScript that when we run it on the terminal it works, but when it is triggered by the crontab, it doesn't work.

When it is triggered by crontab, the only step that gets completed is the focus of the app.

tell application "SelfControl" to activate


set myPass to do shell script "security find-generic-password -w -s 'My Test' -a 'account_dummy'"


tell application "System Events"
    delay 0.5
    tell process "SelfControl"
        delay 0.5
        tell slider of window "SelfControl" to set value to 1.0
        delay 0.5
        click button "Start Block" of window "SelfControl"
    end tell
    delay 0.5
    repeat while exists (processes where name is "SecurityAgent")
        delay 0.5
        tell process "SecurityAgent"
            
            set frontmost to true
            if (count of windows) > 0 then
                set window_name to name of front window
            end if
            try
                keystroke myPass
                delay 0.1
                keystroke return
                delay 0.1
            on error
                -- do nothing to skip the error
            end try
        end tell
        delay 0.5
    end repeat
end tell

The goal is to schedule the app self-control https://selfcontrolapp.com/

This is how it has been added to the crontab:

*/5 14-23 * * 1,2,3,4,5 osascript /Users/cristobal/Library/Mobile\ Documents/com~apple~ScriptEditor2/Documents/selfcontrol.scpt

Every 5 minutes run is for testing, as the block time is only 1 minute, those values will be updated once I get this working.

Thanks for the help.


Edit for some clarification, I have added some logs and it turns out that the problem is coming from the line where it is getting the password.

This is the error logged:

execution error: security: SecKeychainSearchCopyNext: The specified item could not be found in the keychain. (44)
0 Answers
Related