How to detect?

How to detect whether a disk is local volume or not? I am trying the following script with no luck.

set TheVolumes to list disks
repeat with TheDisk in TheVolumes
	tell application "Finder"
		-- the following line works
		set local_status to local volume of disk "Macintosh HD"
		-- but the following line gets an error something like:
		-- Cannot make disk "Macintosh HD", an item.
		set local_status to local volume of disk TheDisk
	end tell
end repeat

What’s the difference?

: What’s the difference?
disk named TheDisk
Finder references are odd :wink:

: Finder references are odd :wink: – AND non-intuitive, AND often illogical.

You can also use ‘disk 1’s local volume’ as in… repeat with x from 1 to (count (list disks))

tell application "Finder" to 
        set {nom, local_status} to {disk x's name, disk x's local volume}
    display dialog nom & "     " & local_status

Andreas

: How to detect whether a disk is local volume or not? I am trying
: the following script with no luck.
: set TheVolumes to list disks
: repeat with TheDisk in TheVolumes
: tell application “Finder”
: – the following line works
: set local_status to local volume of disk “Macintosh
: HD”
: – but the following line gets an error something like: –
: Cannot make disk “Macintosh HD”, an item.
: set local_status to local volume of disk TheDisk
: end tell
: end repeat
: What’s the difference?

With the kind of repeat you’re using (‘repeat with theDisk in theVolumes’), the value of ‘theDisk’ is not the name of a disk but a reference to an item in ‘theVolumes’. In other words, it’s not “Macintosh HD” but something like ‘item 1 of {“Macintosh HD”, “RAM Disk”}’. This doesn’t make sence to the Finder when it’s applied directly to the keyword ‘file’, so it needs to be ‘dereferenced’ first. The offical way would be use the dereferencing operator, ‘contents of’:

set local_status to local volume of disk (contents of theDisk)

… but coercion is also very popular:

set local_status to local volume of disk (theDisk as string)

NG

: With the kind of repeat you’re using (‘repeat with theDisk in
: theVolumes’), the value of ‘theDisk’ is not the name of a disk
: but a reference to an item in ‘theVolumes’. In other words,
: it’s not “Macintosh HD” but something like ‘item 1
: of {“Macintosh HD”, “RAM Disk”}’. This
: doesn’t make sence to the Finder when it’s applied directly to
: the keyword ‘file’, so it needs to be ‘dereferenced’ first.
: The offical way would be use the dereferencing operator,
: ‘contents of’: set local_status to local volume of
: disk (contents of theDisk)
: … but coercion is also very popular: set local_status
: to local volume of disk (theDisk as string)
: NG

set local_status to local volume of disk named TheDisk
set local_status to local volume of disk (TheDisk as string)

Both worked well. I had even tried

set local_status to local volume of disk TheDisk as string

before, but, now I realize that the parentheses were missing. Thanks to everyone who replied.

: set local_status to local volume of disk named TheDisk
: set local_status to local volume of disk (TheDisk as
: string)
: Both worked well. I had even tried set local_status to
: local volume of disk TheDisk as string before, but, now
: I realize that the parentheses were missing. Thanks to
: everyone who replied.

I’m glad you got what you needed. :slight_smile:

A. deBugger’s reply was interesting. I’d always thought that ‘named’ was merely an optional term, but now it appears that it actually does something! I’m not sure exactly what, though.

‘first disk whose name is TheDisk’ also works - which is surprising, since ‘is’ and “=” don’t normally resolve references in comparisons. However, this seems to be an example of one reference (TheDisk) incorported into another (the Finder expression), which produces a single, combined reference. Here’s a hack to show the combined reference:

  set TheVolumes to list disks
  repeat with TheDisk in TheVolumes
    tell application "Finder"
      tell (first disk whose name is TheDisk)
        return it
      end tell
    end tell
  end repeat
  --> disk 1 of application "Finder" whose name = item 1 of {"Macintosh HD", "RAM Disk"}

I suppose using ‘named’ works in a similar way: ‘file named TheDisk’ would be an expression incorporating a reference, rather than attempt to use the reference itself as a name.

Just thinking out loud… :slight_smile:

NG

For anyone who might be following this thread here is an exchange of e-mailsÉ
From Cuneyt ++ RE: Finder references are odd :wink: – AND non-intuitive, AND often illogical.

++ You can also use “disk 1’s local volume” as in
++
++ repeat with x from 1 to (count (list disks))
++ tell application “Finder” to set {nom, local_status} to {disk x’s name, disk x’s local volume}
++ display dialog nom & " " & local_status
++ end repeat
What’s wrong with this code? A friend of my reports that this script gives Error -1700 Finder got an error: Can’t make “Jack’s Hard Disk” into an item. He has a single drive on his desktop. I too tried with a single drive with the same disk name without any error. The only difference is I have 9.1 he has 9.2.2.
tell application “Finder”
set TheDisks to (name of every disk of desktop) as list
set FolderList to {}
repeat with i from 1 to number of TheDisks
set TheDisk to item i of TheDisks
if (local volume of disk TheDisk) and (not ejectable of disk TheDisk) then
set FolderList to list folder TheDisk
end if
end repeat
end tell
Addendum to above posting:

Andreas, I tried your (following) code and it works properly on Mac OS 9.1 and AS 1.6.

… repeat with x from 1 to (count (list disks))
… tell application “Finder” to set {nom, local_status} to {disk x’s name, disk x’s local volume}
… display dialog nom & " " & local_status
… end repeat

Have you by a chance, tried the same code on Mac OS 9.2.2?

Best regards,

Cuneyt Ocaklilar

From Andreas

Hi Cuneyt

Two things…

  1. “myDisk” is just a string and not a Finder reference - so a meaningless word to the Finder. You need to add one word:
set FolderList to (list folder disk TheDisk) 
  1. Having that line within the repeat loop means that you will only get the list for the last disk found. You might want to use either:
set FolderList to FolderList & (list folder disk TheDisk) 

to get a list of the contents of ALL the disks in one long list, or:

copy (list folder disk TheDisk) to end of FolderList 

(probably more useful) to give a separate list for each disk (item 1, item 2, etc).

Look at the Result window in the Editor and it will be clear.

Finally - sorry, no - I have nothing beyond 8.6.

Andreas