Go Back   The macosxhints Forums > OS X Help Requests > AppleScript



Reply
 
Thread Tools Rating: Thread Rating: 11 votes, 5.00 average. Display Modes
Old 01-29-2012, 02:04 AM   #1
webcakes
Prospect
 
Join Date: Oct 2009
Posts: 17
script to wake up screen?

Hi,

I'm looking for a script that can wake my screen. I do have WOL working, but that just wakes up my system, the screen itself remains black.

I have been searching the internet and found 2 suggested approaches, one is sending a keystroke (e.g. tell application "System Events" key code 59), and another approach is to activate the screensaver. I've tried both type of scripts and although I can get them working when my screen is on, but when I try them with my screen in sleep mode, they fail to wake my screen. (I'm launching them via a bash script)

It looks I'm getting a bit closer when I compile them as an application. Still my screen remains black, but when I then manually activate, I see that a message box has popped up that says 'press run to run this script.'

Any ideas here?



I'm on an iMac with Lion 10.7.2
webcakes is offline   Reply With Quote
Old 01-29-2012, 02:11 AM   #2
NaOH
Hall of Famer
 
Join Date: Dec 2007
Posts: 3,649
Quote:
Originally Posted by webcakes
It looks I'm getting a bit closer when I compile them as an application. Still my screen remains black, but when I then manually activate, I see that a message box has popped up that says 'press run to run this script.'

Sounds like you may have made one minor mistake when you saved the script as an application. Open the script, then use the File menu to select Save As. In the dialog, set the File Format pop-up menu to Application, then be sure to uncheck the box for Startup Screen (you can uncheck the others, too, but the startup screen option is the hiccup you ran into before).
NaOH is offline   Reply With Quote
Old 01-29-2012, 05:33 AM   #3
webcakes
Prospect
 
Join Date: Oct 2009
Posts: 17
Quote:
Originally Posted by NaOH
be sure to uncheck the box for Startup Screen

it was unchecked. Problem is that this application behaves differently depending on the state of the screen.
- if I launch it via a bash script (with an open command) when the screen is on, it works perfect: screensaver kicks inn, no pop up box
- if I launch it via the same bash script, put my screen in sleep mode (with ctrl-shift-eject), then nothing happens. If I then wake my screen manually (with a mouse click), then I see the message box 'press run to run this script'
(the bash script has a sleep command to allow me the time to manually put my screen in sleep mode before the app starts)
webcakes is offline   Reply With Quote
Old 01-29-2012, 11:13 AM   #4
Hal Itosis
Hall of Famer
 
Join Date: Apr 2002
Posts: 3,315
Why even use an application at all?
Why not save as a 'regular' script?
Then...
/usr/bin/osascript -e /path/to/the/applescript.scpt
__________________

chown -R us /your\ base/

-HI-

.
Hal Itosis is offline   Reply With Quote
Old 01-30-2012, 01:57 PM   #5
webcakes
Prospect
 
Join Date: Oct 2009
Posts: 17
Quote:
Originally Posted by Hal Itosis
Why even use an application at all?
Why not save as a 'regular' script?
Then...
/usr/bin/osascript -e /path/to/the/applescript.scpt

Sure, I've tried that too. But the effect is the same: the script runs, but it does not activate my screen. It just remains black
webcakes is offline   Reply With Quote
Old 01-30-2012, 02:06 PM   #6
fracai
MVP
 
Join Date: May 2004
Posts: 2,013
Try using the program suggested in this hint to move and click the mouse.

If you only use "CGPostMouseEvent( pt, 1, 1, 0 );", ie. with a 0 as the final argument, you'll only move the mouse without clicking it.

You should then be able to call that program from your script. I'm fairly certain a mouse move will wake the screen.
fracai is offline   Reply With Quote
Old 01-30-2012, 02:20 PM   #7
webcakes
Prospect
 
Join Date: Oct 2009
Posts: 17
thanks for this suggestion.

I tried it out, but unfortunately, the screen remains black.... (when I wake it then myself, I see that the cursor is positioned on the spot I specified with click)

Maybe a similar piece of code would exist to wake the screen (but I guess that this applescript forum may not be the place to ask that)
webcakes is offline   Reply With Quote
Old 01-30-2012, 05:17 PM   #8
Hal Itosis
Hall of Famer
 
Join Date: Apr 2002
Posts: 3,315
Quote:
Originally Posted by Hal Itosis
Why even use an application at all?
Why not save as a 'regular' script?
Then...
/usr/bin/osascript -e /path/to/the/applescript.scpt

Quote:
Originally Posted by webcakes
Sure, I've tried that too. But the effect is the same: the script runs, but it does not activate my screen. It just remains black

Perhaps, but it certainly eliminated that whole "message box" (Startup Screen) business mentioned in your first two posts... didn't it?



[note: my post didn't claim it would solve everything... i merely asked: why use an app at all? ]
__________________

chown -R us /your\ base/

-HI-

.
Hal Itosis is offline   Reply With Quote
Old 01-31-2012, 12:52 PM   #9
webcakes
Prospect
 
Join Date: Oct 2009
Posts: 17
Quote:
Originally Posted by Hal Itosis
Perhaps, but it certainly eliminated that whole "message box"

Sure it did !
your help is appreciated
webcakes is offline   Reply With Quote
Old 02-01-2012, 11:35 AM   #10
tlarkin
League Commissioner
 
Join Date: Mar 2003
Location: Kansas City
Posts: 11,347
desktop or laptop? Look at the pmset command

http://developer.apple.com/library/m...1/pmset.1.html
__________________
sudo make me a sammich

http://www.tlarkin.com

"It just told me what I already knew, that I'm a great and amazing guy, didn't I tell you baby, I'm Zaphod Beeblebrox."
tlarkin is offline   Reply With Quote
Old 02-01-2012, 12:44 PM   #11
Hal Itosis
Hall of Famer
 
Join Date: Apr 2002
Posts: 3,315
Quote:
Originally Posted by tlarkin
desktop or laptop? Look at the pmset command

http://developer.apple.com/library/m...1/pmset.1.html

While pmset can certainly change the timing of displaysleep (as well as several other brightness params), i think the problem here is this: once a display has gone to sleep, it then takes some sort of hardware stimulus to toggle that state from sleep to awake. [software simulation of mouse movements and key presses simply doesn't suffice]

Apparently there is no display-wake or un-black facility available via software.
__________________

chown -R us /your\ base/

-HI-

.

Last edited by Hal Itosis; 02-01-2012 at 12:46 PM.
Hal Itosis is offline   Reply With Quote
Old 02-01-2012, 01:40 PM   #12
fracai
MVP
 
Join Date: May 2004
Posts: 2,013
It must be possible by software for the reason that a VNC connection wakes the display. I've never been connected by Screen Sharing to a blank display so it may just be the act of connecting.

Try looking at VNC Snapshot or VNC Capture (perl).

Note that you'd have to enable Screen Sharing or start your own VNC server. Starting your own server may be easier if you don't want to always have Screen Sharing enabled.
fracai is offline   Reply With Quote
Old 02-01-2012, 02:12 PM   #13
webcakes
Prospect
 
Join Date: Oct 2009
Posts: 17
Quote:
Originally Posted by fracai
It must be possible by software for the reason that a VNC connection wakes the display. I've never been connected by Screen Sharing to a blank display so it may just be the act of connecting.

Try looking at VNC Snapshot or VNC Capture (perl).

Note that you'd have to enable Screen Sharing or start your own VNC server. Starting your own server may be easier if you don't want to always have Screen Sharing enabled.

I tried vncsnapshot, but get this error message: cannot execute binary file
(Screen sharing is enabled, and I gave vncsnapshot a chmod 755)
webcakes is offline   Reply With Quote
Old 02-01-2012, 01:53 PM   #14
fracai
MVP
 
Join Date: May 2004
Posts: 2,013
Also, the only reason I suggest the screenshot programs is they would allow you to skip opening the X11 application. If that's not a concern, you could also just use Screen Sharing, TightVNC, etc.
fracai is offline   Reply With Quote
Old 02-01-2012, 03:30 PM   #15
fracai
MVP
 
Join Date: May 2004
Posts: 2,013
The only Mac OS X binary that I see is for PPC. Are you running on PPC or using something older than Lion? I'm not sure if Rosetta works with command line binaries anyway.

I can compile an Intel binary for you in a few hours if no one else gets to it first.
fracai is offline   Reply With Quote
Old 02-01-2012, 04:51 PM   #16
fracai
MVP
 
Join Date: May 2004
Posts: 2,013
So VNCSnapshot doesn't build cleanly straight from source. I'll still try to get this for you if you're interested, but it'll take a bit and I might not get to it very soon.

You might try installing VNC Capture instead. It should be pretty easy (look up how to use CPAN).

Scratch that, I had already created a patch a ways back that lets it build. I just need to check that it actually functions. Give me an hour or two... hopefully :-)
fracai is offline   Reply With Quote
Old 02-01-2012, 08:55 PM   #17
fracai
MVP
 
Join Date: May 2004
Posts: 2,013
OK, here's a static binary of vncsnapshot and vncpasswd. It was compiled for 10.7, if you're using another version, maybe someone else can compile it for you. I don't have access to any older systems anymore.

And, I was able to use vncsnapshot to wake my display! I set a hot corner to sleep the display, executed the command below, and dragged my mouse to the sleep corner. 5 seconds later, the display woke up.
Code:
sleep 5 && vncsnapshot --passwd password-file localhost /dev/null
The "passwd" stuff is something you'll have to setup as well. By default, Mac OS X Screen Sharing uses a different authentication method than vanilla VNC. But, you can allow other VNC clients to connect by entering a password under "Computer Settings" in the Screen Sharing service configuration pane.

The security of this is admittedly not great, mostly because of the 8 character limitation on VNC passwords, and I'm not sure how the password is sent. I know it's obfuscated, I don't think it's encrypted. It's usually recommended that VNC sessions are tunneled over SSH. It'd probably be quicker and safer to use something like TightVNC to start up a temporary display, though that may not be enough to wake the hardware screen (I haven't tested this). A temporary display would also allow you to use a one time, randomized password for each wake.

So, set enable Screen Sharing, set a VNC password there, and create a password file like this:
Code:
vncpasswd password-file
Then, you should be able to use the vncsnapshot command to wake the display. In the above example, I'm just throwing away the image. In my tests vncsnapshot captured an image that was the correct size, but all gray.

Also, waking the screen worked on my laptop, but didn't on my iMac. Both were running 10.7.2. Hopefully it'll work for you.

Let me know if you need anything else. This is a really convoluted way to wake the display. There's gotta be a better way.


Oh, and if anyone is interested, the patch that enabled me to build vncsnapshot is below. You'll also need libjpeg and zlib as discussed in the README.
Code:
--- sockets.cxx.orig	2004-09-08 20:22:33.000000000 -0400
+++ sockets.cxx	2009-10-22 18:15:51.000000000 -0400
@@ -39,8 +39,11 @@
 #endif
 
 #ifdef __APPLE__
+#ifndef _SOCKLEN_T
+#define _SOCKLEN_T
 typedef int socklen_t;
 #endif
+#endif
 
 extern "C" {
 #include "vncsnapshot.h"
fracai is offline   Reply With Quote
Old 02-01-2012, 08:13 PM   #18
hayne
Site Admin
 
Join Date: Jan 2002
Location: Montreal
Posts: 31,958
For what it's worth, the freely available application "Caffeine": http://www.lightheadsw.com/caffeine/
will wake the screen.
So maybe your script could just launch Caffeine.
__________________
hayne.net/macosx.html
hayne is offline   Reply With Quote
Old 02-02-2012, 07:51 PM   #19
fracai
MVP
 
Join Date: May 2004
Posts: 2,013
Problem solved.

https://github.com/bigkm/SleepDisplay

Running the command on its own sleeps the display. Pass -wake to wake it back up.

It worked on my Air running 10.7.2. I did see some claims that code similar to this cause kernel panics on PPC hardware.

P.S. I found this after a search turned up a Hints Forum thread for sleeping the display. I started looking for similar code and documentation to do the opposite and stumbled on the github project. Turns out it's just a simple matter of sending false instead of true to the request.
fracai is offline   Reply With Quote
Old 02-03-2012, 02:40 PM   #20
tlarkin
League Commissioner
 
Join Date: Mar 2003
Location: Kansas City
Posts: 11,347
I wonder how the ARDagent.app does it then, because you can clearly share the screen when you remote into a computer with ARD Admin. Looking at the ARDAgent in /System/Library/CoreServices/RemoteManagement/ARDAgent.app/Content/Support there are some other apps in there. Not sure if this will help or not, I cannot get the screen to wake, but I know ARD Admin can.
__________________
sudo make me a sammich

http://www.tlarkin.com

"It just told me what I already knew, that I'm a great and amazing guy, didn't I tell you baby, I'm Zaphod Beeblebrox."
tlarkin is offline   Reply With Quote
Reply

Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump



All times are GMT -5. The time now is 07:42 AM.


Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2013, vBulletin Solutions, Inc.
Site design © IDG Consumer & SMB; individuals retain copyright of their postings
but consent to the possible use of their material in other areas of IDG Consumer & SMB.