Skip to content

Instantly share code, notes, and snippets.

@ricardo-valerio
Last active August 3, 2021 14:08
Show Gist options
  • Save ricardo-valerio/d7bef0c7ff49bf3734d374ce056a8089 to your computer and use it in GitHub Desktop.
Save ricardo-valerio/d7bef0c7ff49bf3734d374ce056a8089 to your computer and use it in GitHub Desktop.
#!/usr/bin/python3
from platform import system as platform_name
from os import system
import ctypes
platforms_dictionary = {
"Windows": {
"open" : 'ctypes.windll.WINMM.mciSendStringW(u"set cdaudio door open", None, 0, None)',
"close": 'ctypes.windll.WINMM.mciSendStringW(u"set cdaudio door closed", None, 0, None)'
},
"Darwin": {
"open" : 'system("drutil tray open")',
"close": 'system("drutil tray closed")'
},
"Linux": {
"open" : 'system("eject cdrom")',
"close": 'system("eject -t cdrom")'
},
"NetBSD": {
"open" : 'system("eject cd")',
"close": 'system("eject -t cd")'
},
"FreeBSD": {
"open" : 'system("sudo cdcontrol eject")',
"close": 'system("sudo cdcontrol close")'
}
}
if platform_name() in platforms_dictionary:
exec(platforms_dictionary[platform_name()]["open"])
exec(platforms_dictionary[platform_name()]["close"])
else:
print("Sorry, no OS found")
@TakesTheBiscuit
Copy link

Hey i like this fork from the original; i found for Windows if you want to use a different drive, in my case L: i had to alias the drive with an OPEN command first then i was able to open/close the tray afterwards ; perhaps another way to achieve the same?
See: https://gist.github.com/TakesTheBiscuit/a2e3d36c1d20731821fdb41b3831406e

@TakesTheBiscuit
Copy link

https://www.faqforge.com/linux/eject-cddvd-tray-command-line-linux/

eject /dev/cdrom
eject /dev/cdrw
eject /dev/dvd
eject /dev/dvdrom
eject /dev/dvdrw

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment