Trigger based on idle time / user activity

I assume given all the other time based options, I have to just be missing it somewhere. I want to setup a trigger so that I can auto stop my music from playing if I haven't been around my computer for an 30mins.

Hi @chaosdrop,

This is achievable using BTT's "Trigger On Script Output Change" Trigger.

Steps:

  1. In the "Automations, Named & Other Triggers" trigger category, create a "Trigger On Script Output Change" Trigger.

  2. Add this JS code to the trigger's configuration:

    // reads the number of nanoseconds since the last user interaction and converts it to seconds
    let shellScript = `ioreg -c IOHIDSystem | /usr/bin/awk '/HIDIdleTime/ {printf "%.3f\\n", $NF/1000000000; exit}'`;
    
    // run it and get back something like "12.345\n"
    let result = await runShellScript({ script: shellScript });
    
    // turn that into a JS number
    let idleSeconds = parseFloat(result);
    
    // calculate the idleTimeLimit by multiplying the number of minutes to wait times 60
    let idleTimeLimit = 30 * 60;
    
    // return a JS Boolean: true if > idleTimeLimit, false otherwise
    return idleSeconds > idleTimeLimit;
    
  3. Set the "Condition Check Interval" to a number, e.g. 10.

  4. Toggle on the "Always Trigger For Initial Value" setting.

    Your settings should now look like this:

  5. Under "Actions Executed On Change To TRUE", add a "Run Apple Script (async in background)" Action.

  6. Set the script in your new AppleScript action to:

    tell application "Music"
    	if player state is playing then
    		pause
    	end if
    end tell
    

For your convenience, here's the JSON configuration of the above setup:

[
  {
    "BTTActionCategory" : 0,
    "BTTLastUpdatedAt" : 1745075841.2363601,
    "BTTTriggerType" : 796,
    "BTTTriggerTypeDescriptionReadOnly" : "Trigger On Script Output Change",
    "BTTTriggerClass" : "BTTTriggerTypeOtherTriggers",
    "BTTUUID" : "9D157D8C-DFF3-40FF-90A9-7451C277C68A",
    "BTTPredefinedActionType" : 366,
    "BTTPredefinedActionName" : "Empty Placeholder",
    "BTTATCTriggerForInitialValue" : 1,
    "BTTATCCheckInterval" : 10,
    "BTTRepeatRate" : 10,
    "BTTEnabled" : 1,
    "BTTEnabled2" : 1,
    "BTTOrder" : 1,
    "BTTAdditionalActions" : [
      {
        "BTTActionCategory" : 0,
        "BTTLastUpdatedAt" : 1745075035.877655,
        "BTTTriggerParentUUID" : "9D157D8C-DFF3-40FF-90A9-7451C277C68A",
        "BTTIsPureAction" : true,
        "BTTTriggerClass" : "BTTTriggerTypeOtherTriggers",
        "BTTUUID" : "C72B318A-3438-4956-AB85-A1296C2E05FC",
        "BTTPredefinedActionType" : 195,
        "BTTPredefinedActionName" : "Run Apple Script (async in background)",
        "BTTAdditionalActionData" : {
          "BTTScriptType" : 0,
          "BTTAppleScriptUsePath" : false,
          "BTTScriptLocation" : 0,
          "BTTAppleScriptRunInBackground" : true,
          "BTTAppleScriptString" : "tell application \"Music\"\r\tif player state is playing then\r\t\tpause\r\tend if\rend tell"
        },
        "BTTInlineAppleScript" : "tell application \"Music\"\r\tif player state is playing then\r\t\tpause\r\tend if\rend tell",
        "BTTEnabled" : 1,
        "BTTEnabled2" : 1,
        "BTTOrder" : 531
      }
    ],
    "BTTAdditionalData" : "eyJCVFRNb25pdG9yZWRKUyI6IlwvXC8gYnVpbGQgeW91ciBzaGVsbCBjb21tYW5kIGV4YWN0bHkgYXMgYmVmb3JlLCB3aXRoIFxcXFxuIHNvIGF3ayBwcmludHMgYSBuZXdsaW5lXG5sZXQgc2hlbGxTY3JpcHQgPSBgaW9yZWcgLWMgSU9ISURTeXN0ZW0gXFxcbnwgXC91c3JcL2JpblwvYXdrICdcL0hJRElkbGVUaW1lXC8ge3ByaW50ZiBcIiUuM2ZcXFxcblwiLCAkTkZcLzEwMDAwMDAwMDA7IGV4aXR9J2A7XG5cblwvXC8gcnVuIGl0IGFuZCBnZXQgYmFjayBzb21ldGhpbmcgbGlrZSBcIjEyLjM0NVxcblwiXG5sZXQgcmVzdWx0ID0gYXdhaXQgcnVuU2hlbGxTY3JpcHQoeyBzY3JpcHQ6IHNoZWxsU2NyaXB0IH0pO1xuXG5cL1wvIHR1cm4gdGhhdCBpbnRvIGEgSlMgbnVtYmVyXG5sZXQgaWRsZVNlY29uZHMgPSBwYXJzZUZsb2F0KHJlc3VsdCk7XG5cblwvXC8gY2FsY3VsYXRlIHRoZSBpZGxlVGltZUxpbWl0IGJ5IG11bHRpcGx5aW5nIHRoZSBudW1iZXIgb2YgbWludXRlcyB0byB3YWl0IHRpbWVzIDYwXG5sZXQgaWRsZVRpbWVMaW1pdCA9IDMwICogNjA7XG5cblwvXC8gcmV0dXJuIGEgSlMgQm9vbGVhbjogdHJ1ZSBpZiA+IGlkbGVUaW1lTGltaXQsIGZhbHNlIG90aGVyd2lzZVxucmV0dXJuIGlkbGVTZWNvbmRzID4gaWRsZVRpbWVMaW1pdDsifQ=="
  }
]

I hope this helps!

Thank you, that looks like it'll work great! Though definitely not something I could have figured out on my own.

1 Like