How to eject a cd?

Feb 25, 2006 at 5:23am

How to eject a cd?

I’m developing a standalone which samples audio from cd’s and i’m having a hard time working out a nice way for the user to be able to eject the CD from within the app.

I searched and found this shell method in the archive:

echo %s | sudo diskutil eject ‘/Volumes/%s’

Where the first symbol is the root password and the second symbol the volume name.

I don’t really want the user to have to enter their password just to eject a CD. Is there a method using applescript or something even more simple?

p.s. i don’t have a clue about applescript so please go easy on me if you have an example.

thanks,

john.

#24609
Feb 25, 2006 at 9:07am

… not of any interest for you, but i made a script for [pyext] that
works on windows. Any CD could be open, read, and ejected. The only
thing we canno’t do is to read .cdda files (windows specific files for
audio-cd) because python thread seems too slow. But i guess making an
installer that put python 2.4, then pyext, then the cd module on your
client’s machine is not what you expect !

Anyway, if there’s no solution with AS, take a look at koders.com.
There’s some examples of how to do this in Java. Some on forums say it’s
not possible without external lib. Some say yes with sun’s api only.
Worth a look.

best

f.e

If you plan to make it for windows, here’s the source for handling cd
eject in c++ :

1. #include
2. #include
3. #include
4. #include
5.
6. // Prototypes
7.
8. BOOL EjectVolume(TCHAR cDriveLetter);
9.
10. HANDLE OpenVolume(TCHAR cDriveLetter);
11. BOOL LockVolume(HANDLE hVolume);
12. BOOL DismountVolume(HANDLE hVolume);
13. BOOL PreventRemovalOfVolume(HANDLE hVolume, BOOL fPrevent);
14. BOOL AutoEjectVolume(HANDLE hVolume);
15. BOOL CloseVolume(HANDLE hVolume);
16.
17. LPTSTR szVolumeFormat = TEXT(“\\.\%c:”);
18. LPTSTR szRootFormat = TEXT(“%c:\”);
19. LPTSTR szErrorFormat = TEXT(“Error %d: %sn”);
20.
21. |void| ReportError(LPTSTR szMsg)
22. {
23. _tprintf(szErrorFormat, GetLastError(), szMsg);
24. }
25.
26. HANDLE OpenVolume(TCHAR cDriveLetter)
27. {
28. HANDLE hVolume;
29. UINT uDriveType;
30. TCHAR szVolumeName[8];
31. TCHAR szRootName[5];
32. DWORD dwAccessFlags;
33.
34. wsprintf(szRootName, szRootFormat, cDriveLetter);
35.
36. uDriveType = GetDriveType(szRootName);
37. |switch|(uDriveType) {
38. |case| DRIVE_REMOVABLE:
39. dwAccessFlags = GENERIC_READ | GENERIC_WRITE;
40. |break|;
41. |case| DRIVE_CDROM:
42. dwAccessFlags = GENERIC_READ;
43. |break|;
44. |default|:
45. _tprintf(TEXT(“Cannot eject. Drive type is incorrect.n”));
46. |return| INVALID_HANDLE_VALUE;
47. }
48.
49. wsprintf(szVolumeName, szVolumeFormat, cDriveLetter);
50.
51. hVolume = CreateFile( szVolumeName,
52. dwAccessFlags,
53. FILE_SHARE_READ | FILE_SHARE_WRITE,
54. NULL,
55. OPEN_EXISTING,
56. 0,
57. NULL );
58. |if| (hVolume == INVALID_HANDLE_VALUE)
59. ReportError(TEXT(“CreateFile”));
60.
61. |return| hVolume;
62. }
63.
64. BOOL CloseVolume(HANDLE hVolume)
65. {
66. |return| CloseHandle(hVolume);
67. }
68.
69. #define LOCK_TIMEOUT 10000 // 10 Seconds
70. #define LOCK_RETRIES 20
71.
72. BOOL LockVolume(HANDLE hVolume)
73. {
74. DWORD dwBytesReturned;
75. DWORD dwSleepAmount;
76. |int| nTryCount;
77.
78. dwSleepAmount = LOCK_TIMEOUT / LOCK_RETRIES;
79.
80. // Do this in a loop until a timeout period has expired
81. |for| (nTryCount = 0; nTryCount < LOCK_RETRIES; nTryCount++) {
82. |if| (DeviceIoControl(hVolume,
83. FSCTL_LOCK_VOLUME,
84. NULL, 0,
85. NULL, 0,
86. &dwBytesReturned,
87. NULL))
88. |return| TRUE;
89.
90. Sleep(dwSleepAmount);
91. }
92.
93. |return| FALSE;
94. }
95.
96. BOOL DismountVolume(HANDLE hVolume)
97. {
98. DWORD dwBytesReturned;
99.
100. |return| DeviceIoControl( hVolume,
101. FSCTL_DISMOUNT_VOLUME,
102. NULL, 0,
103. NULL, 0,
104. &dwBytesReturned,
105. NULL);
106. }
107.
108. BOOL PreventRemovalOfVolume(HANDLE hVolume, BOOL fPreventRemoval)
109. {
110. DWORD dwBytesReturned;
111. PREVENT_MEDIA_REMOVAL PMRBuffer;
112.
113. PMRBuffer.PreventMediaRemoval = fPreventRemoval;
114.
115. |return| DeviceIoControl( hVolume,
116. IOCTL_STORAGE_MEDIA_REMOVAL,
117. &PMRBuffer,
|sizeof|(PREVENT_MEDIA_REMOVAL),
118. NULL, 0,
119. &dwBytesReturned,
120. NULL);
121. }
122.
123. AutoEjectVolume(HANDLE hVolume)
124. {
125. DWORD dwBytesReturned;
126.
127. |return| DeviceIoControl( hVolume,
128. IOCTL_STORAGE_EJECT_MEDIA,
129. NULL, 0,
130. NULL, 0,
131. &dwBytesReturned,
132. NULL);
133. }
134.
135. BOOL EjectVolume(TCHAR cDriveLetter)
136. {
137. HANDLE hVolume;
138.
139. BOOL fRemoveSafely = FALSE;
140. BOOL fAutoEject = FALSE;
141.
142. // Open the volume.
143. hVolume = OpenVolume(cDriveLetter);
144. |if| (hVolume == INVALID_HANDLE_VALUE)
145. |return| FALSE;
146.
147. // Lock and dismount the volume.
148. |if| (LockVolume(hVolume) && DismountVolume(hVolume)) {
149. fRemoveSafely = TRUE;
150.
151. // Set prevent removal to false and eject the volume.
152. |if| (PreventRemovalOfVolume(hVolume, FALSE) &&
153. AutoEjectVolume(hVolume))
154. fAutoEject = TRUE;
155. }
156.
157. // Close the volume so other processes can use the drive.
158. |if| (!CloseVolume(hVolume))
159. |return| FALSE;
160.
161. |if| (fAutoEject)
162. printf(“Media in Drive %c has been ejected safely.n”,
163. cDriveLetter);
164. |else| {
165. |if| (fRemoveSafely)
166. printf(“Media in Drive %c can be safely removed.n”,
167. cDriveLetter);
168. }
169.
170. |return| TRUE;
171. }
172.
173. |void| Usage()
174. {
175. printf(“Usage: Eject nn”);
176. |return| ;
177. }
178.
179. |void| main(|int| argc, |char| * argv[])
180. {
181. |if| (argc != 2) {
182. Usage();
183. |return| ;
184. }
185.
186. |if| (!EjectVolume(argv[1][0]))
187. printf(“Failure ejecting drive %c.n”, argv[1][0]);
188.
189. |return| ;
190. }

#71455
Feb 25, 2006 at 9:35am

John,

take a look at http://www.osxfaq.com/tips/unix-tricks/week22/
wednesday.ws use disktool rather than diskutil – this doesn’t require
root access but you’ll have to run disktool -l to find out the name
of the cd – then you just use disktool -e disk1 (if it was disk1).

Hope that helps.

Bren.

#71456
Feb 25, 2006 at 3:13pm

Thanks very much Bren using disktool works for me.

I’m not quite sure how i’m going to parse the data from the disktool -l I don’t want to accidently unmount anything other than the cd….

best,

john.

#71457
Feb 25, 2006 at 3:32pm

Is the internal CD always disk1 ?…….

#71458
Feb 25, 2006 at 6:18pm

#71459
Feb 25, 2006 at 8:31pm

On Feb 25, 2006, at 5:23 AM, Leafcutter John wrote:

> I’m developing a standalone which samples audio from cd’s and i’m
> having a hard time working out a nice way for the user to be able
> to eject the CD from within the app.
>
> I searched and found this shell method in the archive:
> echo %s | sudo diskutil eject ‘/Volumes/%s’
>
> Where the first symbol is the root password and the second symbol
> the volume name.
>
> I don’t really want the user to have to enter their password just
> to eject a CD.

You do not need to use your password to eject a CD-ROM. Just
“diskutil eject ‘the_path’”.

- John

#71460
Feb 25, 2006 at 9:02pm

> On a mac, since 10.3 you can use the drutil CLI: “drutil tray eject”
> will do the trick.
>
> HTH,
> ej

right, that is a [shell] job :)

#71461
Feb 25, 2006 at 10:42pm

Thanks for the help everyone, it’s much appreciated.

Emmanuel’s method is simple and perfect for what i want.

best wishes to all,

john.

#71462

You must be logged in to reply to this topic.