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

sergiobaschi

macrumors regular
Original poster
Nov 30, 2012
199
6
Gothenburg, Sweden
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?
 

ArtOfWarfare

macrumors G3
Nov 26, 2007
9,541
6,028
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?

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?
 

sergiobaschi

macrumors regular
Original poster
Nov 30, 2012
199
6
Gothenburg, Sweden
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?

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.

i'm curious about this as well.
my mini is connected to the TV 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.
 

mvmanolov

macrumors 6502a
Aug 27, 2013
858
5
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.

Here you go :D

tell application "Finder"
get bounds of window of desktop
--> {0, 0, 1920, 1080}
end tell
(*Width: 1920*)
(*Height: 1080*)
 

sergiobaschi

macrumors regular
Original poster
Nov 30, 2012
199
6
Gothenburg, Sweden
Here you go :D

tell application "Finder"
get bounds of window of desktop
--> {0, 0, 1920, 1080}
end tell
(*Width: 1920*)
(*Height: 1080*)

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.
 

mvmanolov

macrumors 6502a
Aug 27, 2013
858
5
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.

yep, but at least now you know :D
 

Nedanator

macrumors newbie
Apr 11, 2008
28
0
Derby, UK
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.
 

sergiobaschi

macrumors regular
Original poster
Nov 30, 2012
199
6
Gothenburg, Sweden
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.

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?
 

Nedanator

macrumors newbie
Apr 11, 2008
28
0
Derby, UK
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:

23/10/2014 11:38:08.191 WindowServer[110]: Display 0x1b488001 changed state to offline

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

23/10/2014 11:40:05.503 WindowServer[110]: Display 0x1b488001: GL mask 0x2; bounds (0, 0)[1920 x 1080], .........

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.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.