Applescript to detect when tv is turned off?

Discussion in 'Mac Programming' started by sergiobaschi, Aug 19, 2014.

  1. sergiobaschi macrumors regular

    Joined:
    Nov 30, 2012
    Location:
    Gothenburg, Sweden
    #1
    Suppose I have a Mac Mini connected to my TV – how can i programmatically know when the TV has been turned off?

    As I don't have a mac connected to the TV right now (awaiting the new Mini), it's hard to write e right code...

    Any takers?

    Is it maybe as easy as getting the size of the desktop?
     
  2. ArtOfWarfare macrumors 604

    ArtOfWarfare

    Joined:
    Nov 26, 2007
    #2
    How is the Mac Mini connected to the TV? Is this via something like AirPlay or is it being plugged into the Mac Mini just like any other screen?
     
  3. mvmanolov macrumors 6502a

    Joined:
    Aug 27, 2013
    #3
    i'm curious about this as well.
    my mini is connected to the TV via HDMI.
     
  4. sergiobaschi thread starter macrumors regular

    Joined:
    Nov 30, 2012
    Location:
    Gothenburg, Sweden
    #4
    I don't have the Mini yet, then I would be able to try it out my self. But it will be connected via HDMI.

    Maybe you could give it a try, using this piece of code:
    Code:
    delay 20
    
    tell application "Finder"
    	set desktopsize to bounds of window of desktop
    end tell
    
    log "Width: " & item 3 of desktopsize
    log "Height: " & item 4 of desktopsize
    
    Open the AppleScript editor, paste the code, click the "Responses" (equal to my "Svar" button, see this pic). Click "Run" and turn your tv off. The script will wait 20 seconds before it checks the size of the desktop.

    To be sure, 30 seconds after you activated the script, turn the tv back on again, and please share the result.
     
  5. mvmanolov macrumors 6502a

    Joined:
    Aug 27, 2013
    #5
    Here you go :D

    tell application "Finder"
    get bounds of window of desktop
    --> {0, 0, 1920, 1080}
    end tell
    (*Width: 1920*)
    (*Height: 1080*)
     
  6. sergiobaschi thread starter macrumors regular

    Joined:
    Nov 30, 2012
    Location:
    Gothenburg, Sweden
    #7
    Thanks for trying :) Unfortunately it didn't help much.

    I've seen that post, but when I run the code, the return isn't as obvious as one might think. But I suppose that's the way to go, using a shell command. I'll dig in to it.
     
  7. mvmanolov macrumors 6502a

    Joined:
    Aug 27, 2013
    #8
    yep, but at least now you know :D
     
  8. Nedanator macrumors newbie

    Joined:
    Apr 11, 2008
    Location:
    Derby, UK
    #9
    Assuming that at some future date you do end up with a solution, what is it you then plan to do?

    if TV off -> turn TV on?

    Because if that is the case, I have written an AppleScript / Arduino programme that allows you to control your TV from your keyboard using IR.

    Using an App called Scenario, it also supports putting the TV to sleep (off) when I put my MacBook to sleep, and then switches it back on again when I wake the Mac.
     
  9. sergiobaschi thread starter macrumors regular

    Joined:
    Nov 30, 2012
    Location:
    Gothenburg, Sweden
    #10
    That's not what I had intended. My plan was to schedule the computer to do some stuff, but only if the TV is turned off. I just got a new Mac Mini, so now I can actually try it myself.

    But in your script, how do you notice if the TV is off / on?
     
  10. Nedanator macrumors newbie

    Joined:
    Apr 11, 2008
    Location:
    Derby, UK
    #11
    I don't notice if the TV is on or off.

    You see, I use my TV as my main display for my MacBook. If the main display is not on, the MacBook is asleep, and thus I can't do anything with it.

    Of course, I can turn the TV off without putting the computer to sleep, but for me that wouldn't be very useful.

    If you do want your Mac Mini to do stuff when the screen is switched off, then getting the screen size is a good start, but getting it to be reliable means the script will have to be on all the time, constantly reading the screen size. Instead, you could make a launchAgent to run on new entries to the windowServer log file. For instance, if I open up the display on my MacBook so that I have two displays running, do a search for 'windowServer' in the Console, so I only see windowServer messages, and then switch off my TV, and then switch it back on again. Among a whole load of other messages, I find these:

    from reading more of the messages, I can see that display 0x1b488001 is my TV because of this message.

    1920x1080 is my TVs resolution, as opposed to 1366x768 for my MacBook Air's display.

    You can use an App like Plisterine or Lingon to make launchAgents. I would suggest starting by locating the log file that contains the windowServer messages; creating a launchAgent to run on new entries; having that launchAgent run a script that will poll the new entries to the file and match your display's hex address with the word 'offline'; and if true, perform the stuff you want the machine to do.

    As it happens, I'll be receiving one of the new Mac Minis in a few days. When that happens, I may well find that having the machine do stuff while the display is off is beneficial to me, if that is the case, then I will probably end up writing a system that does exactly that, but for now, it is not beneficial to me.

    Stray question:
    Is the thing you're trying to schedule, the shutdown time of the machine. i.e., if you turn off the display, the machine also shutsdown?

    Because if that's the case, you can do it the other way around. When I'm done for the night. I run a script that sets the shutdown time for the computer, and sets the TVs sleep timer for the same time. That way, they both switch off at the same time.
     

Share This Page