• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag
 

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Considerable simplification should be possible
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Considerable simplification should be possible


  • Subject: Re: Considerable simplification should be possible
  • From: "koenig.yvan" <email@hidden>
  • Date: Mon, 28 Apr 2014 09:54:23 +0200


Le 28/04/2014 à 07:54, Lists <email@hidden> a écrit :

My iMac usually has 4 to 6 external hard drives attached but unmounted (so that a bunch of drives don't have to wake up each time some app puts up an open and save dialog). Every time I need to access one of these unmounted drives, I open Disk Utility, select the drive and click the mount button. Works every time but involves several steps, so I decided to automate the process.

I have a working script that I think is making too many shell calls and I would like to simplify. In particular the first shell call could get the disk identifiers (for example "disk0s2") as well as the volume names. I tried building two lists from the first shell call, but couldn't figure out how to associate the second list after the "choose from list"  part of the script.

The script that follows presents me with a list of attached hard drives from which I can choose a drive to mount, and mounts it after choosing. As I said, I think it could be simpler.

set theVolumeNamesList to {}
set HFSdiskList to do shell script "diskutil list | grep Apple_HFS"
set x to paragraphs of HFSdiskList
repeat with LVar in items of x
set thisVolumeName to word 3 of LVar
set the end of theVolumeNamesList to thisVolumeName
end repeat
set volumeChoice to (choose from list theVolumeNamesList with prompt "Choose a volume to mount.")
if the result is not false then
set volumeToMount to volumeChoice
set shellScpt to "diskutil list | grep " & volumeToMount & ""
set whatItem to (do shell script shellScpt) --the disk identifier is the last word of what's returned here
do shell script "diskutil mount " & (last word of whatItem)
end if

Thanks
J

the list stored in the variable x  resemble to that :
{"   2:                  Apple_HFS Macintosh HD            999.3 GB   disk0s2", "   2:                  Apple_HFS Samsung 32              31.1 GB    disk1s2"}
If as you do I extract the words of each list item I get :
{"2", "Apple_HFS", "Macintosh", "HD", "999.3", "GB", "disk0s2"}
{"2", "Apple_HFS", "Samsung", "32", "31.1", "GB", "disk1s2"}

(1) as you may see, word 3 is not really the name of the volumes
we may get the true names from words 3 thru -4 of LVar
CAUTION, the words separators aren't necessarily spaces. A comma for instance is a word separator.
This is why in the script belowI just use the offsets of firt and last word of the volume names to rebuild them.
(2) the volume IDs are available as last words of every LVar string
so we may extract them at the beginning.
We will be able to get them when they will be useful

Here is the edited beginning of the script

set theVolumeNamesList to {}
set theVolumeIDsList to {}
set HFSdiskList to do shell script "diskutil list | grep Apple_HFS"
set x to paragraphs of HFSdiskList
repeat with LVar in x
set nameWords to words 3 thru -4 of LVar
set thisVolumeName to text (offset of (item 1 of nameWords) in LVar) thru ((offset of (item -1 of nameWords) in LVar) - 1) of LVar & item -1 of nameWords
set end of theVolumeNamesList to thisVolumeName
set end of theVolumeIDsList to word -1 of LVar
end repeat
log theVolumeNamesList
--> {"Macintosh HD", "Samsung 32"}
theVolumeIDsList
--> {"disk0s2", "disk1s2"}

Yvan KOENIG (VALLAURIS, France) lundi 28 avril 2014 09:54:18


 _______________________________________________
Do not post admin requests to the list. They will be ignored.
AppleScript-Users mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:
Archives: http://lists.apple.com/archives/applescript-users

This email sent to email@hidden

  • Follow-Ups:
    • Re: Considerable simplification should be possible
      • From: Shane Stanley <email@hidden>
References: 
 >Considerable simplification should be possible (From: Lists <email@hidden>)

  • Prev by Date: Considerable simplification should be possible
  • Next by Date: Re: XMLlib library
  • Previous by thread: Considerable simplification should be possible
  • Next by thread: Re: Considerable simplification should be possible
  • Index(es):
    • Date
    • Thread