I got a small script that quit app when the computer is idle for X amount of time. It works fine but if I save it as an app and start it, I can't quit it. Result is I have to force quit my script app to restart my computer. Can I fix this? Is it the repeat loop that not the way to do it?


    # Action lorsque que l'ordinateur est sans activité pendant un certain temps
set cmd to "echo $((`ioreg -w 0 -c IOHIDSystem | sed -e '/HIDIdleTime/ !{ d' -e 't' -e '}' -e 's/.* = //g' -e 'q'` / 1000000000))"

# Définir ici le nombre de seconde en idle avant d'exécuter l'action 
set maxIdleAllowed to 90

# Conbien de fois vérifier le idle en seconde
set checkInterval to 5

# On loop indéfiniment
    set secsIdle to 0

        set answer to (do shell script cmd)
        set secsIdle to (answer as number)
    on error
        # May want to do something fancy here...
        exit repeat
    end try

    log secsIdle

    if secsIdle > maxIdleAllowed then
        # Changer le nom de l'application TextEdit pour votre application
        tell application "FileMaker Pro" to quit
    end if

    delay checkInterval
end repeat

I think this is what you're looking for: https://stackoverflow.com/questions/38530412/exit-infinite-loop-by-quitting-application#38531544

More info from Apple: https://developer.apple.com/library/content/documentation/AppleScript/Conceptual/AppleScriptLangGuide/conceptual/ASLR_about_handlers.html#//apple_ref/doc/uid/TP40000983-CH206-SW7

And another reference: https://scriptingosx.com/tag/idle/

Basically, instead of your repeat, you want to do an on idle, instead of the repeat

  • Link-only answers are frowned upon and discouraged as links may and do break, which can make a link-only answer worthless. Link-only answers also may be deleted just because they are link-only! Please add the relevant information so as to make the answer stand alone on it own merits without the absolute need for the links as it's presently written. – user3439894 Apr 11 '18 at 16:39

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .