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

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 (2 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


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.