Mounting an unmounted external firewire hard drive

Here is what I’m hoping to accomplish. I’m working on a backup up scheme. I have a firewire hard drive attached to my mac. Most of the time the partitions of the drive are not mounted even though the drive is turned on. I would like to create an applescript that checks to see if a specific partition is mounted or not. If it is not, the script would then mount that partition.

This seems like it would be a relatively easy thing to do, but all the results to my searches on MacScripter only show how to mount network drives, not ones attached directly to the computer.

If anyone can help, I would appreciate it.

Thanks

Model: Dual 2.5GHz G5
Browser: Safari
Operating System: Mac OS X (10.4)

Feel free to modify this script of mine. If you need help let me know.

set disklist to {}

set vollist to textToList((do shell script "disktool -l"), return)

repeat with i from 1 to number of items in vollist
	--display dialog ((i as string) & " of " & number of items in vollist as string) as string giving up after 1 buttons {" "}
	set this_item to item i of vollist as string
	if this_item does not contain "volName = ''" then
		set this_item_list to textToList(this_item, "'")
		--display dialog item ((count of items in this_item_list) - 1) of this_item_list as string
		if item 8 of this_item_list is not "'" then
			set end of disklist to item 8 of this_item_list
		end if
	end if
end repeat
-- check for volume count
if the disklist is {} then
	error "No volumes are connected to this computer."
else if the (count of the disklist) is greater than 1 then
	-- choose volume
	set this_Volumes to (choose from list disklist with prompt "Pick the volume to mount:" without empty selection allowed and multiple selections allowed) as string
	if this_Volumes is "false" then error "User Cancelled!"
else
	set this_Volumes to item 1 of the disklist
end if

--set theVol to characters 1 thru ((count of characters in (this_Volumes as string)) - 1) of (this_Volumes as string) as string
set theVol to this_Volumes

set done to false
repeat with i from 1 to number of items in vollist
	--display dialog ((i as string) & " of " & number of items in vollist as string) as string giving up after 1 buttons {" "}
	if done is false then
		set this_item to item i of vollist as string
		if this_item contains theVol then
			set this_item_list to textToList(this_item, "'")
			--display dialog item ((count of items in this_item_list) - 1) of this_item_list as string
			if item 8 of this_item_list is not "'" then
				set end of disklist to item 8 of this_item_list
			end if
		end if
	end if
end repeat

set theVolff to "gone"
set done to false
repeat with i from 1 to number of items in vollist
	--display dialog ((i as string) & " of " & number of items in vollist as string) as string giving up after 1 buttons {" "}
	if done is false then
		set this_item to item i of vollist as string
		set this_item_list to textToList(this_item, "'")
		--display dialog item ((count of items in this_item_list) - 1) of this_item_list as string
		if (item ((count of items in this_item_list) - 1) of this_item_list) is theVol then
			set theVolff to item 2 of this_item_list
			set done to true
		end if
	end if
end repeat

if done is false then error ("Volume " & quoted form of theVol & " not found!")

set unmountme to button returned of (display dialog "Mount volume " & quoted form of theVol & "?" & return & "Depending on file system this may take a while." buttons {"No", "Yes"} default button 2)
if unmountme is "Yes" then
	do shell script ("disktool -m " & theVolff)
end if

on textToList(theText, theSep)
	set soFar to {}
	set textSoFar to theText
	repeat until theSep is not in textSoFar
		set thePos to the offset of theSep in textSoFar
		set thenewPos to thePos
		if thenewPos is 1 then set thenewPos to 2
		set nextBit to text 1 through (thenewPos - 1) of textSoFar
		set textSoFar to text (thePos + 1) through -1 of textSoFar
		copy nextBit to the end of soFar
	end repeat
	copy textSoFar to the end of soFar
	return soFar
end textToList

Thanks! I’m sure this will be a great help. I’ll try it out tommorow at work. Once again, thanks.

That is an awesome script, but I am a newb to this scripting.

How would I go about reversing this script so as I could do exactly the same thing but “un-mount” a drive ?

This is the best script that I have managed to find so far :smiley:

Hi clearview,

unmounting is much easier, because you needn’t determine the internal device number of the volume

tell application "System Events" to set theDisks to name of disks whose (startup is false and name is not "Network")
try
	set theDisk to item 1 of (choose from list theDisks with prompt "Choose a disk to unmount")
	do shell script "diskutil unmount " & quoted form of ("/Volumes/" & theDisk)
	display dialog result buttons {"OK"} default button 1 giving up after 3
end try

These scripts are great but the first script for mounting the disks seems to have trouble when being used within an automator action.

The error I am getting is: expected “end” but found “on” ?

It is in the last paragraph of the script

on textToList(theText, theSep)
set soFar to {}
set textSoFar to theText
repeat until theSep is not in textSoFar
set thePos to the offset of theSep in textSoFar
set thenewPos to thePos
if thenewPos is 1 then set thenewPos to 2
set nextBit to text 1 through (thenewPos - 1) of textSoFar
set textSoFar to text (thePos + 1) through -1 of textSoFar
copy nextBit to the end of soFar
end repeat
copy textSoFar to the end of soFar
return soFar
end textToList

I don’t mean to ask you to sort this out for me but I really can not begin to fathom this script as I am so new to applescript. I am only 4 days into this !

Hi,

I have written a similar script.
You can choose only one of the volumes, which are really unmounted

set allVolumes to paragraphs of (do shell script "disktool -l") -- list of all available volumes
set {devList, nameList} to {{}, {}}

set {TID, text item delimiters} to {text item delimiters, "'"}
repeat with i in allVolumes
	set {d, n} to {text item 2 of i, text item 8 of i}
	if n is not "" then
		set {end of devList, end of nameList} to {d, n}
	end if
end repeat
set text item delimiters to TID

set mountedVolumes to paragraphs of (do shell script "ls /Volumes") -- list of mounted volumes
set unmountedVolumes to {}
repeat with i in nameList
	if contents of i is not in mountedVolumes then set end of unmountedVolumes to contents of i
end repeat
if unmountedVolumes is not {} then
	set theVolume to (choose from list unmountedVolumes with prompt "Pick the volume to mount:") -- show only unmounted disks
	if theVolume is false then return
	set theVolume to item 1 of the theVolume
	repeat with i from 1 to count nameList
		if item i of nameList is theVolume then
			set theVolume to item i of devList
			exit repeat
		end if
	end repeat
	do shell script "diskutil mount " & theVolume
else
	display dialog "No unmounted volumes available" buttons {"OK"} default button 1
end if

OK, I’ll add my 2 cents…
I noticed in the above mounting scripts that you use disktool to find your information. The man page of disktool says it is deprecated. As such here’s a script which uses diskutil to get most of its information. This script allows you to mount or unmount a volume.

-- this script allows you to either 1) mount a local volume or 2) unmount any volume (local or network)

-- choose to mount or unmount a volume
set tempVar to display dialog "Mount or Unmount a volume?" & return & return & "Note: only local volumes can be mounted but any volume can be unmounted (local or network)." buttons {"Cancel", "Unmount", "Mount"} default button 3 with icon 1
set inAction to button returned of tempVar

try
	if inAction is "Mount" then
		set {deviceList, nameList} to {{}, {}}
		-- get device identifiers and volume names of all local volumes
		set allVolumes to words of (do shell script "diskutil list")
		repeat with i from 1 to (count of allVolumes)
			if (item i of allVolumes) contains "disk" and (count of text items of (item i of allVolumes)) > 6 and (item 6 of (text items of (item i of allVolumes))) is "s" then
				set volName to (do shell script "diskutil info " & (item i of allVolumes) & " | awk -F'   *' '/Volume Name/ {print $3}'")
				if volName is not "" then
					set end of nameList to volName
					set end of deviceList to (item i of allVolumes)
				end if
			end if
		end repeat
		-- get a list of mounted volumes
		set mountedVolumes to paragraphs of (do shell script "ls /Volumes")
		-- find out which volumes are unmounted
		set unmountedVolumes to {}
		repeat with i in nameList
			if contents of i is not in mountedVolumes then set end of unmountedVolumes to contents of i
		end repeat
		-- mount an unmounted volume by picking from a list
		if unmountedVolumes is not {} then
			set theVolume to (choose from list unmountedVolumes with prompt "Pick the volume to mount:")
			if theVolume is false then return
			set theVolume to item 1 of the theVolume
			repeat with i from 1 to (count of nameList)
				if item i of nameList is theVolume then
					set theVolume to item i of deviceList
					exit repeat
				end if
			end repeat
			do shell script "diskutil mount " & theVolume
		else
			display dialog "There are no unmounted volumes available to mount." buttons {"OK"} default button 1 with icon 1
		end if
		
	else if inAction is "Unmount" then
		tell application "System Events" to set theDisks to name of disks whose (startup is false and name is not "Network")
		set chosenDisk to (choose from list theDisks with prompt "Choose a disk to unmount")
		if chosenDisk is not false then
			set theDisk to (item 1 of chosenDisk)
			tell application "System Events" to set isLocal to local volume of disk theDisk
			if isLocal then
				do shell script "diskutil unmount " & quoted form of ("/Volumes/" & theDisk)
			else
				tell application "Finder" to eject disk theDisk
			end if
		end if
	end if
on error the error_message number the error_number
	display dialog "Error: " & the error_number & ". " & the error_message buttons {"OK"} default button 1
end try

Why is this script so much longer and what is it doing differently ?

Thanks for chipping in by the way. I think I need all the help I can get with this Applescripting;)

You have to read the instructions in the post and it should be explained… but here it is.

  1. it both mounts and unmounts in one script, whereas the others are written in 2 separate scripts.
  2. it uses disk utility instead of disk tool. That’s because disk tool has been deprecated ie. Apple will discontinue including it with the OS at some point.
  3. it will unmount both local volumes and networked volumes that you have mounted on your computer, others won’t unmount network volumes because that needs a separate command

The reason I posted it is because the others used disk tool which means those scripts won’t work in the future.

Ah, thankyou.

I did not realise that apple would be dropping disktool at some point.

This is good then as if I upgrade to 10.5 then the script will still work.

Apologies, but with all this code to be looking at i did read the instructions at the top of the post but seem to have stored the information somewhere within a temporary non functioning part of my brain.

“Ah, Light dawns on marble head”

Thankyou both so much, I think I am a bit more educated now :slight_smile:

I am having trouble with another script that I am trying to construct.

Would you mind helping me some more. I think I am 90% there but I think the script made need some further refinement ?

Model: G4 FW800
AppleScript: 2.1.1
Browser: Safari 419.3
Operating System: Mac OS X (10.4)

Hi. Sorry to bump such an old thread, but I have a question, and it seems to me that it’d be better to post about it here.

It’s about the exact same issue (mounting FireWire drives via AppleScript), but under Mac OS 9 (AppleScript 1.8.3) instead of Mac OS X. Since there seems to be no pre-OS-X forum or any other subforum I could ask about this, this one seemed to be the closest one to being adequate.

I tried adapting the OS X script provided by others here to make it work with OS 9, but the main issue I’m facing is trying to replace the OS X calls to “diskutil” and “disktool”, which aren’t runnable in an OS 9 environment.

I’m not sure if people with what would be now considered old scripting knowledge would happen to still frequent or lurk around these forums, but, if at all possible, any tip or help towards that goal would be greatly appreciated. Anything counts, even pointing me towards a different website or source.

The closest I found was how to mount network drives using the “mount” command, but I don’t think that can be used for FireWire volumes, as well. Any ideas?

Thanks for hearing me out!

Little bump. I’m still stuck at this issue. I know it’s technically possible to mount the FireWire drives back because the FW HDD itself is detected under Mac OS 9’s Apple System Profiler, and rebooting into Mac OS X Tiger/Leopard triggers a built-in 1st party script of some sort that mounts them back, proving hardware is not part of the issue.

Again, any help on getting this accomplished without diskutil/disktool or anything else OS-X-restricted would be greatly appreciated.