"Eject" Keystroke?

I’m trying to write a script that simulates the keystroke combination “Shift” + “Control” + “Eject”.

I can’t try to figure out how to pass the keystroke for “Eject”.

So far, I have this:


tell application "System Events"
	keystroke ?EJECT? using control down & shift down
end tell

Hi,

there is no key code for the eject key, therefore it is not accessible with AppleScript

do shell script "drutil eject -drive internal"

for internal

or plain

shell script "drutil eject "

or “One of the following keywords: internal, external, usb,
firewire, atapi, scsi.”

And how do you handle the modifier keys? :wink:

Doh.
Good point.:rolleyes:

What are they for anyway?

** Ah tell the display to sleep

That key combo doesn’t do anything on my computer, but assuming the intention is to put the computer to sleep…

tell application "Finder" to sleep

subtle but important difference:
⌃⇧⏏ puts the display to sleep
⌥⌘⏏ (the same as your script) puts the computer to sleep

Ah, I get it.

You can’t reference the eject key, and there is no syntax to directly sleep the display. Who knew this would be so difficult…

The only solutions I found searching involve shell scripts that temporarily set the display sleep to the shortest setting and then wait that amount of time and set the settings back to what you had. Seems a little clunky to me.

It is clunky, Matt. The shortest pmset for display sleep is 1, which means one minute. Setting it to zero means “never”.

Well, at least I’m not the only one who’s tried to do this.

Has anyone ever found a programmatic way to sleep the display which doesn’t involve waiting for one minute to have pmset do its thing?

I found mentions of a hack that only worked pre-Leopard which used a ‘magic number’ to fake pmset into instantly sleeping the display. But that doesn’t work.

I have a handy script to do the opposite (fake pressing the shift key to wake up the display):

This widget works, but I don’t know how to programatically invoke it:
http://www.chriskarcher.net/software/displaysleeper

There’s something mentioned at the bottom of the comments on that page by yet another person (“Julia Truchsess”) who wants this functionality:

http://www.dynamicallyloaded.com/products/osx/sleepdisplay/index.html

Sadly, the page is 404’d. But I’ll probably write to them and ask about it.

Follow-up: Chris Karcher kindly sent me a command-line executable that works great.

It uses the method call documented here:

http://www.cocoabuilder.com/archive/cocoa/191807-sleep-display.html

Would it be possible to share this with us? I’d be interested in this as well. :smiley:

Update: A 29k universal (!) binary (13k if you slim it down) is available from here: http://nickmomrik.com/2007/12/17/lock-screen-and-sleep-display-with-quicksilver/

It’s possible to use it from AS as well as call the binary in bash/Terminal (SleepDisplay.app/Contents/MacOS/sleepdisplay)

Works on 10.5.x as well as 10.6.x.

btw: Is turning off the display the same as setting the brightness to 0?

If yes, then this universal binary http://mattdanger.net/2008/12/adjust-mac-os-x-display-brightness-from-the-terminal/ has worked for me under 10.5.x as well as 10.6.x.
Depending on the path where you’d locate the binary, it could be as easy as:

do shell script "/usr/local/bin/brightness 0"

Another possible solution might be to use dockables http://getdockables.com and launching the sleep display app.

I don’t think so because even manually changing my brightness on my Cinema Displays doesn’t turn them the back lighting completely off like it does on a MacBook. Also the display brightness doesn’t work on my Mac Mini at home that has generic DVI monitors plugged into it but display sleeping stops the video from outputting (i.e. control+shift+eject).

Here’s a download link for the SleepDisplay application. It’s tiny, works instantly, and works under OS 10.6 and 10.7 (haven’t tried it under more recent versions). I use it to kill the display on my living room iMac when I run the “Movie” or “Bedtime” macros on my home automation system.

http://www.pragmaticdesigns.com/SleepDisplay.zip

Hi Julia,

I don’t know exactly when they added these to unix, but here’s an example:

do shell script "pmset displaysleepnow"
say 1
say 2
say 3
do shell script "caffeinate -u"

gl,
kel