Help me write a script in OSX
October 26, 2009 9:54 AM   Subscribe

How do I create a file-handling script in OSX?

I want to create a script that will throw whatever is in my CF card in the trash, empty the recycle bin and then eject the card.
I frequently transfer pictures from my card to my computer and with lightroom 2 they don't automatically erase off the card so I need to do it by hand.
I find that if I trash the files on the card and DON'T empty the recycle bin the data stays held up in the card and eventually have to format it.

Are there any other options? If not, how would I write a script. I tried "recording" a script in Applescript Editor but that didn't work at all.

Thanks!
posted by Thrillhouse to Computers & Internet (3 answers total) 1 user marked this as a favorite
 
Yes, you can write a shell script to take whatever actions you like and name it "whatever.command". The script would probably be these commands:
#!/bin/sh
DRIVENAME="the name of the of the volume on the card"
rm -rf /Volumes/$DRIVENAME/*
umount /Volumes/$DRIVENAME

posted by mkb at 9:59 AM on October 26, 2009


> rm -rf /Volumes/$DRIVENAME/*

But sweet Jesus watch out with this one. 'Cause:

rm -rf /

will pretty much completely nuke everything -- and I do mean literally every single file -- on your computer. So, if you were to somehow end up with:

rm -rf / Volumes/$DRIVENAME/*

( ... note the space ... )

Then goodbye, drive!

A safer way might be to check Automator. I'm pretty sure it can do this kind of thing with its arm tied behind its back.
posted by WCityMike at 1:33 PM on October 26, 2009


AppleScript is a quirky language, and can be hard to use even for a professional programmer. But for simple things like this it does pretty well. Here's a script that does what you want, assuming you know the name of your disk ahead of time:
tell application "Finder"
    set CFDisk to disk "My CF Disk Name"
    delete items of CFDisk
    empty trash -- Danger Will Robinson!
    eject CFDisk
end tell
This too is a little unsafe, since it will empty the trash (for all disks) without asking confirmation. Here's one that does:
tell application "Finder"
    set itemsInTrash to (number of items of trash)
    set CFDisk to disk "My CF Disk Name"
    
    if (itemsInTrash > 0) then
        (* Undocumented subtlety here: we get OK and Cancel by default, 
           and hitting cancel aborts the script *)
        display dialog (itemsInTrash as string) & " items already in trash will be erased. Proceed?"
    end if
    delete items of CFDisk
    empty trash
    eject CFDisk
end tell
I've tested the above scripts, but offer no warrantee.
posted by serathen at 3:59 PM on October 26, 2009


« Older Antivirus with on/off switches   |   How can I talk like a 17th century tavern wench... Newer »
This thread is closed to new comments.