Hi, I am working on a script and I’ve hit a dead-end… I am trying to use the following command “set DisksMounted to list disks” to create a list of disks mounted on a users desktop. Now, the list of mounted items could contain 2, 3, 4, or 5 items. One of the mounted items would contain “ClassOF” but I don’t know which item it would be. I want to set a variable to the text of the item that contains “Classof” but I don’t know how to do this without knowing what item it is. I was going to use If statements as shown below but this will not work for users that have more or less mounted disks than those specified by the if statements.
set var_disk1 to (item 1 of DisksMounted as list) as text
set var_disk2 to (item 2 of DisksMounted as list) as text
set var_disk3 to (item 3 of DisksMounted as list) as text
if var_disk1 contains "CLASSOF" then
set year_name to characters 10 thru 11 of var_disk1 as string
end if
if var_disk2 contains "CLASSOF" then
set year_name to characters 10 thru 11 of var_disk2 as string
end if
if var_disk2 contains "CLASSOF" then
set year_name to characters 10 thru 11 of var_disk2 as string
end if
Any help is appreciated!
Browser: Firefox 2.0.0.12
Operating System: Mac OS X (10.4)
Although StefanK’s one-liners are much more succinct, I think this is what you were trying to do. This is how you can cycle through the items of a list (i.e. from the “list disks” command) and use an if statement to find what you want…
set DisksMounted to list disks
set year_name to ""
repeat with thisDisk in DisksMounted
if thisDisk contains "CLASSOF" then
set year_name to text 10 thru 11 of thisDisk
exit repeat
end if
end repeat
get year_name
I thought stefans did exactly what the OP was after.
The only bit I think is incorrect is the : 10 thru 11 of
I think that was put in by the OP as part of getting the right disk, not to get the the text of the disk, but I could be wrong on that? if not then a small edit:
tell application "Finder" to set year_name to text of (get name of 1st disk whose name contains "CLASSOF")
Yep thats exactly right! Sorry I wasn’t more clear. I need to get the last two of the ClassOf mount to know if they are classof2008 or classof2009 etc… The problem was that depending on how many shares get automounted when a user logs in, the Classof mount could be item 1,2,3, or 4 of the DisksMounted list so I need it to look at every item and compare it to “Classof.”
I know the code I use is not the cleanest or the most efficient; I’m just a noob trying to learn This forum is a great resource though! Thanks everyone for helping this newbie:cool: