Detecting All Disc Burners Snippet Failsafe ?

Hi everyone !

I have this snippet but I’m not sure if it’s 100% working all the time, (It does on my Mac with 1 burner)

repeat with Burner in paragraphs of (do shell script "drutil list | grep -v SupportLevel")
	if "R" is in Burner then display dialog Burner
end repeat

The script originally was this :

repeat with Burner in paragraphs of  (do shell script "/usr/bin/drutil list | /usr/bin/grep -v SupportLevel ; echo test ; exit 0")
	if "R" is in Burner then display dialog Burner
end repeat

I know this isn’t the right place to ask but can one test them on there Mac ? (maybe with more than 1 burner?) And is the script on the top the same as the one on the bottom ? or has echo & exit 0 something to do with it?

Thanks in advance

They both work on my machine in two different configurations: one writer drive (internal DVD-ROM/CD-R (basic, non-Super drive)) and two writer drives (the same internal plus an external, USB DVD-RW). But there are some potential bugs.

The main flaw is the assumption that the letter ‘R’ will occur in the drutil list output only if the drive is a recorder. While this is probably always the case for “writer” drives (e.g. CD-R or DVD-R or DVD+R, etc. in the product description), it may also be the case for read-only drives (CD-ROM or plain DVD drives) if there is an ‘R’ in any of the vendor name (e.g. PIONEER), the product description (e.g. “CD-ROM”, or the model number happens to have ‘R’), the revision (similar risk to description), the bus type (e.g. FIREWIRE?, seems more likely to be FireWire though), or the SupportLevel description (unlikely; only a handful of values seem to be produced, none have ‘R’).

To fix this assumption, I might instead egrep for an extended regexp like “<(CD-R|DVD[±]R|BD[±]R)(W>|>)” (the BD part is a guess on my part for how BluRay devices might be reported). That way, you are checking for a word like CD-R or DVD+R, which, while still technically possible, is less likely to occur in any of those other places (mostly because a DVD-ROM with a name that included a word like DVD+R would be confusing to people).

Then again, this is drutil. The dr is for Disc Recording. Maybe read-only drives will never show up in any of the output. Though there are drutil commands that could also work do on read-only drives (eject, tray, discinfo, etc.).

The original " ; echo test ; exit 0" should not hurt. You can remove the " ; echo test", but you might want to leave the " ; exit 0".

The “test” bit was probably there for the author’s testing of the AppleScript code (splitting into paragraphs, checking for ‘R’). It serves as an injected result that we know never contains ‘R’, so if it ever shows up in the final result (dialogs in this case), the filtering mechanism is flawed.

Technically, with the echo in there, the exit is nearly 100% superfluous. But without the echo, it would serve to prevent do shell script from throwing an AppleScript error in any situation where drutil list does not produce any output (or every line of the output contains “SupportLevel”).

Model: iBook G4 933
AppleScript: 1.10.7
Browser: Safari 4.0.3 (4531.9)
Operating System: Mac OS X (10.4)

Thanks for the input !

It seems to work fine in Terminal :slight_smile: but I hate to ask, “How can I get it working in AppleScript?”. If I escape the backslash with a quote, grep won’t find anything…

This works :

repeat with Burner in paragraphs of (do shell script "drutil list | egrep '\\<(CD-R|DVD[+-]R|BD[+-]R)(W\\>|\\>)' ; exit 0")
	display dialog Burner
end repeat

Yes, putting doubling the backslashes for AppleScript and wrapping the whole regexp in single quotes for the shell should do it.

Also, looking at it again, I noticed some ugliness in the regexp (repetitions of the end-of-word ˜>’ pattern, ˜R’, and the ˜[±]’ pattern). Here is your test script again with a revised regexp:

repeat with Burner in paragraphs of (do shell script "drutil list | egrep '\\<(CD-|(DVD|BD)[+-])RW?\\>' ; exit 0")
	display dialog Burner
end repeat

The trailing ˜D’ from DVD and BD could also be factored out, but that would (to me) reduce the readability of the regexp.

Here is my test rig:

set |extended regexp| to "\\<(CD-|(DVD|BD)[+-])RW?\\>"

set |test strings| to {"CD-ROM !!!FAIL; -R should not match because it is part of -ROM", "CD-R", "CD-RW", "CD+R ???FAIL; there was no CD+R?", "CD+RW ???FAIL; there was no CD+RW?", "DVD-ROM !!!FAIL; -ROM should not match", "DVD-R", "DVD+R", "DVD-RW", "DVD+RW", "BD-ROM !!!FAIL; -ROM should not match", "BD-R", "BD+R", "BD-RW", "BD+RW", "NewRay FAIL!!!; there is no -R", "XD-R FAIL!!!; XD is not a recognized 'type'"}

set |test lines| to ""
set |line number| to 1
repeat with |string| in |test strings|
	set |test lines| to |test lines| & |line number| & space & |string| & (ASCII character 10)
	set |line number| to |line number| + 1
end repeat

do shell script "printf %s " & quoted form of |test lines| & " | egrep " & quoted form of |extended regexp| without altering line endings

if result contains "FAIL" then
	display dialog result with title "FAILED"
end if

Really thanks Chrys ! Yeah, I needed two \, I was a bit confused and used " in the first place. Thanks for the new regex. I can’t read regular expressions yet but I’m working on it.

Thanks again