[Automator] Using an ls command and then getting the finder items?

So i want to make an automator app that looks for all external hard drives and then ejects them. Unfortunatly to my experimentation you cant use the finder actions to search in the root directory so i simply added a shell script with


ls /Volumes


which works.


  • then i have a Filter finder items so it doesnt try to eject the Macintosh HD
  • then i have eject Disk.


The problem lies in that the shell script does not spit out finder items to the filter so the filter throws a warning because it isnt supplied the required data.


My question is, how do i get the shell script to pass down finder items to the filter so everything else runs through smoothly?



EDIT: i have python installed if that helps for simplicity 😝 i also know a small bit of C

Thanks

Tom Copcutt

MacBook Air, OS X Mavericks (10.9.2)

Posted on Mar 19, 2014 7:33 AM

Reply
16 replies

Mar 19, 2014 7:57 AM in response to TETHON

How about using 'mount'


mount | awk '

$0 ~ " on / " { next }

$0 ~ "hfs" {print $1}

'


This should give you a list of HFS formated partitions but exclude the boot disk. You should be able to pass this to diskutil umount


diskutil umount $(mount | awk ' $0 ~ " on / " {next} $0 ~ "hfs" { print $1 }' )


which should unmount all the HFS formated, but exclude the boot disk.

If you want other disk formats to be unmounted, then you could just add additiona $0 ~ "xyz" { print $1 } statements to the awk command script

Mar 19, 2014 8:13 AM in response to BobHarris

so i put FAT32 on the third line instead of hfs to eject FAT32? or just FAT

(the format is MS-DOS (FAT32))


what i got from it is that the code looks like this. It compiles, but doesnt do anything. I see what its doing, but dont completely get the syntax


mount | awk '

$0 ~ " on / " { next }

$0 ~ "fat32" {print $1}

'

diskutil umount $mount | awk '

$0 ~ " on / " {next}

$0 ~ "fat32" { print $1 }

'

(also is it meant to be umount or unmount?)



Thanks so much for your help

Mar 19, 2014 8:12 AM in response to TETHON

Issue the 'mount' command from an Applications -> Utilities -> Terminal session, and see what it says the format of the different volumes you have mounted.


All that 'awk' is doing is using search strings on each line to decide to skip (next) or print the 1st field which is the device name.


Looking at the 'mount' output for yourself will tell you what you can search for.

Mar 19, 2014 8:46 AM in response to TETHON

TETHON wrote:


So i want to make an automator app that looks for all external hard drives and then ejects them.



You can do this with Applescript without looking for the external drives:


tell application "Finder" to eject every disk


(if you must have an Automator App, then just put this in the Run AppleScript Action)



You can also use find in Terminal:


find /dev -name "disk[1-9]" -exec diskutil eject {} \;


See: http://stackoverflow.com/questions/2049758/is-there-a-way-to-eject-all-external- hard-drives-from-the-command-line-os-x

Mar 19, 2014 9:18 AM in response to Tony T1

Where did he say that? He only stated that he wanted to avoid ejecting the HD, which isn't possible in any case.


If avoiding .dmg is the issue, one way would be to hard code the possible disk names by using a list like this:


(*

Replace the example volume names with your own volume names inside the curly brackets below;

Names must be quoted and comma separated as shown.

Spaces in names ARE allowed. You can name as many or as few volumes as you wish.

*)


set gDiskList to {"External1 500GB", "Time Machine HD", "External2 HD", "External3 45GB"}


repeat with i in gDiskList

set i to quoted form of i

try

do shell script "diskutil umount " & i

end try

endrepeat

Mar 19, 2014 10:17 AM in response to Tony T1

Disk images fall in the no-man's land, being neither "internal", "external" nor indeed "hard" drives; they're just "mounted volumes". Nothing in the context of the OPs suggested these were a consideration. But in any case, I've already hinted at how one might go about doing that with hdiutil.


Until we hear from the OP, I don't see any further use in debating this point.

Mar 19, 2014 11:30 PM in response to TETHON

Hello


If it is safe to assume you're ejecting every device node mounted on mount point in /Volumes, you'd try this:


#!/bin/bash

for dev in $(mount | perl -lne 'print $1 if m%(^/dev/[^[:space:]]+) on /Volumes/.*%io')
do
    diskutil eject "$dev" >/dev/null 2>&1 &
done


* If you're unmounting and not ejecting it, issue diskutil unmount instead:


#!/bin/bash

for dev in $(mount | perl -lne 'print $1 if m%(^/dev/[^[:space:]]+) on /Volumes/.*%io')
do
    diskutil unmount "$dev" >/dev/null 2>&1 &
done


Regards,

H

Mar 20, 2014 4:10 AM in response to Hiroto

Hiroto wrote:


Hello


If it is safe to assume you're ejecting every device node mounted on mount point in /Volumes


Purely as an intellectual exercise, I decided to write a script to see if I could distinguish dmgs from other devices in /Volumes.


The following script appears to work to unmount every "external" (sic "ejectable") device except DMGs, though my testing hasn't been robust enough to guarantee it (at least one problem would be if you had more than 99 mounted devices, it would fail).


It could also be used to do the reverse - unmount only DMGs and nothing else - by adding 'not' to the conditional in the 'compareAndEject' handler.



getEveryDisk()

set diskList to the result


set origDeLims to AppleScript'stext item delimiters

set AppleScript'stext item delimiters to return

set myDMGNames to {}

set x to {}

try

set x to (do shell script "hdiutil info | grep '/dev'") as text

end try

set AppleScript'stext item delimiters to origDeLims


if x is not equal to {} then


repeat with i from 1 to count of paragraphs in x

set z to getDMGName(paragraph i in x)

set end of myDMGNames to z


end repeat

compareAndEject(myDMGNames, diskList)


else

#add a # to the next line and remove the # from the following line if not on 10.9

display notification "No mounted disk images!"


#display dialog "No mounted disk images!"

endif



on getEveryDisk()

set origDeLims to AppleScript'stext item delimiters

set AppleScript'stext item delimiters to "/"

set allMounts to do shell script "diskutil list | grep /dev"

set allMounts to result as string

set allMounts to allMounts as text

set AppleScript'stext item delimiters to origDeLims


set diskList to {}

repeat with i from 1 to count of paragraphs in allMounts


set the end of diskList to (allMounts's paragraph i)


endrepeat

return diskList


end getEveryDisk



on getDMGName(aParagraph)

set a to items 1 thru 13 in aParagraph as list

set numList to {"1", "2", "3", "4", "5", "6", "7", "8", "9", "0"}

repeat


if numList contains last item of a then

exit repeat


else

set b to the reverse of a

set a to the rest of b

set a to the reverse of a

end if

end repeat

set a to a as string

return a

end getDMGName


on compareAndEject(listDmgs, listDisks)

try

repeat with i in listDisks

if i is in listDmgs then

try

do shell script "diskutil unmountDisk " & i

end try

end if

end repeat

end try


end compareAndEject

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

[Automator] Using an ls command and then getting the finder items?

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.