Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

bingefeller

macrumors 6502a
Original poster
Jun 25, 2007
618
42
Northern Ireland
A strange request - but I'm trying to work out how long I spend with a certain app open. Is there any way that I can do this, without using a third party app?
 
A strange request - but I'm trying to work out how long I spend with a certain app open. Is there any way that I can do this, without using a third party app?
You can try this :
  • Open AppleScript Editor
  • Paste in the following code. I'm using VLC as an example, make sure to replace it with the name of that certain app!
Code:
global startDate
set startDate to (current date)
tell application "VLC" to activate
on idle
    if application "VLC" is not running then
        set stopDate to (current date)
        set timeInSec to stopDate - startDate
        set timePassed to do shell script "python -c 'import datetime; print str(datetime.timedelta(seconds=" & timeInSec & "))'"
        display notification "The application \"VLC\" ran for " & timePassed & " in h:mm:ss."
        tell me to quit
    end if
    return 1
end idle
  • Make sure you save the script as a Stay-Open application (i.e., with the option to "Stay open after run handler" enabled)
 
  • Like
Reactions: bingefeller
You're welcome.

I hate to be a pain but, is there anyway that I will add up all the number of hours and minutes I have used the application for?

Say I use the app for 30 minutes one day, when I quit the app the script will tell me that the app has been running for 30 minutes. Say the next day I use it for 15 minutes - can I get the script to tell me that the app has been running for a total of 45 minutes?

Many thanks!
 
I hate to be a pain but, is there anyway that I will add up all the number of hours and minutes I have used the application for?

Say I use the app for 30 minutes one day, when I quit the app the script will tell me that the app has been running for 30 minutes. Say the next day I use it for 15 minutes - can I get the script to tell me that the app has been running for a total of 45 minutes?

Many thanks!

Try this :

Code:
property totalTimeInSec : 0
global startDate
set startDate to (current date)
tell application "VLC" to activate
on idle
    if application "VLC" is not running then
        set stopDate to (current date)
        set timeInSec to stopDate - startDate
        set totalTimeInSec to totalTimeInSec + timeInSec
        set timePassed to do shell script "python -c 'import datetime; print str(datetime.timedelta(seconds=" & timeInSec & "))'"
        set totalTimePassed to do shell script "python -c 'import datetime; print str(datetime.timedelta(seconds=" & totalTimeInSec & "))'"
        display notification "Application \"VLC\" ran for " & timePassed & return & "Total: " & totalTimePassed
        tell me to quit
    end if
    return 1
end idle
 
  • Like
Reactions: bingefeller
Try this :

Code:
property totalTimeInSec : 0
global startDate
set startDate to (current date)
tell application "VLC" to activate
on idle
    if application "VLC" is not running then
        set stopDate to (current date)
        set timeInSec to stopDate - startDate
        set totalTimeInSec to totalTimeInSec + timeInSec
        set timePassed to do shell script "python -c 'import datetime; print str(datetime.timedelta(seconds=" & timeInSec & "))'"
        set totalTimePassed to do shell script "python -c 'import datetime; print str(datetime.timedelta(seconds=" & totalTimeInSec & "))'"
        display notification "Application \"VLC\" ran for " & timePassed & return & "Total: " & totalTimePassed
        tell me to quit
    end if
    return 1
end idle

Once again, Kryten2, many thanks!
 
Once again, Kryten2, many thanks!
No problem, glad to help. A word of caution though! The value set by a property definition is not reset each time the script is run; instead, it persists until the script is recompiled. Meaning you've been using the script for a few days and decide to change something then the value will be reset to the initial value for the property eg 0. Might I ask what app you're monitoring and do you expect the value to go beyond 24 Hours?
 
No problem, glad to help. A word of caution though! The value set by a property definition is not reset each time the script is run; instead, it persists until the script is recompiled. Meaning you've been using the script for a few days and decide to change something then the value will be reset to the initial value for the property eg 0. Might I ask what app you're monitoring and do you expect the value to go beyond 24 Hours?

It's for Textual, an IRC app. I wont be using it for more than 24 hours.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.