Do I need to use Automator or Applescript?

Discussion in 'Mac Programming' started by MacLadybug, Mar 1, 2010.

  1. MacLadybug macrumors 6502a

    MacLadybug

    Joined:
    Jun 6, 2008
    #1
    I would like to be able to (with one click) automate the process of dumping my trash from the terminal... Right now I do it using these commands:
    Type "cd ~/.Trash" (then I hit enter)
    Type "rm -r *" (hit enter again)

    I've never written a script or a workflow... I can't seem to figure out how which app would be better for this or how to write it. If someone would show me how, then I'm sure I can figure out how to write other ones on my own.

    Is there something I could read that would explain everything step-by-step?

    Any help would be appreciated.

    Thanks.
     
  2. TuffLuffJimmy macrumors G3

    TuffLuffJimmy

    Joined:
    Apr 6, 2007
    Location:
    Portland, OR
    #2
    There is no difference between the GUI method of emptying the trash and the command line.

    Your way is kinda unnecessary already.

    I haven't used AppleScript in a while buy it'd be something like:

    Do shellscript "rm -r* ~/.Trash"
     
  3. fredthefool macrumors newbie

    Joined:
    Jun 4, 2008
    #3
    In AppleScript you can empty the trash this way:

    Code:
    tell application "Finder"
    	empty
    end tell
    
     
  4. MacLadybug thread starter macrumors 6502a

    MacLadybug

    Joined:
    Jun 6, 2008
    #4
    Sometimes dumping the trash can take forever...

    I don't have to do the cd ~/.Trash part first?

    I use that method of trash emptying when I have a ton in the trash and I don't want to wait around for it to empty. I can have thousands of items in it and it takes a second.
     
  5. TuffLuffJimmy macrumors G3

    TuffLuffJimmy

    Joined:
    Apr 6, 2007
    Location:
    Portland, OR
    #5
    I repeat: There is no difference, other than the placebo effect, of emptying the trash from the command line vs a GUI method.


    There is NO difference.

    There is no need for "cd ~/.Trash" because you can select the directory while running the command. Kill two birds with one stone, really.
     
  6. MacLadybug thread starter macrumors 6502a

    MacLadybug

    Joined:
    Jun 6, 2008
    #6
    Thanks. I'm always afraid I might be missing something when it comes to these things. So many times, certain things are just understood. But in this areana I am a real novice and I want to be sure I understand. Not to mention it would suck if erased my whole hard drive... if that were possible.

    Anyway, thanks for taking the time to help.
     
  7. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #7
    Except that the command given:
    Code:
    Do shellscript "rm -r* ~/.Trash"
    
    is wrong.

    It's exceedingly dangerous to make guesses about command-line recursive deletion. You can kill a lot more than two birds.

    I recommend this single line that combines the original two lines:
    Code:
    cd ~/.Trash; rm -r *
    
    It can be stored in a simple text file, then copied and pasted into Terminal.

    There are several other ways of doing this, too, but given the command-line experience level, copy-paste seems the safest.
     
  8. MacLadybug thread starter macrumors 6502a

    MacLadybug

    Joined:
    Jun 6, 2008
  9. TuffLuffJimmy macrumors G3

    TuffLuffJimmy

    Joined:
    Apr 6, 2007
    Location:
    Portland, OR
    #9
    I did state that my AppleScript was a little rusty. I'm not near a computer I can test this at anyway.


    if someone who is obviously not familiar with the command line, let alone bash, is willing to do such simple tasks with it, then I'm not willing to give the obvious disclaimers.

    An AppleScript would be about as safe, if it's copied and pasted. Again I'm not near a computer, but one could test my code and see if it works correctly.
    Code:
    do shell script "cd ~/.Trash; rm -r *"
     
  10. MacLadybug thread starter macrumors 6502a

    MacLadybug

    Joined:
    Jun 6, 2008
    #10
    Makes a perfect droplet!

    Thanks all... I used: do shell script "cd ~/.Trash; rm -r *" and made the script I wanted... saved it as an .app and now I have the perfect droplet on my finder window to dump big volumes of trash in seconds. Works perfectly!

    It also gave me a better general understanding of how to write these commands to use scripts... which I hadn't done before and have wanted to learn more about them and how to create them. It's a start anyway. Thanks for your patience.
     
  11. TuffLuffJimmy macrumors G3

    TuffLuffJimmy

    Joined:
    Apr 6, 2007
    Location:
    Portland, OR
    #11
    Just remember that your script does nothing that the normal method of emptying the trash does.
     
  12. MacLadybug thread starter macrumors 6502a

    MacLadybug

    Joined:
    Jun 6, 2008
    #12
    Yep, but when I have a butt load of stuff and it takes 15 minutes to empty... the script does it instantly in one click.
     
  13. TuffLuffJimmy macrumors G3

    TuffLuffJimmy

    Joined:
    Apr 6, 2007
    Location:
    Portland, OR
    #13
    It actually doesn't. Just because there's no GUI telling you it's taking 15 minutes doesn't mean it isn't. The command line is just the GUI free method, it takes just as long.
     
  14. Nugget macrumors 65816

    Nugget

    Joined:
    Nov 24, 2002
    Location:
    Houston Texas USA
    #14
    Quoted for truth and for the humorous turn of phrase. :)

    This command would be significantly improved by substituting the ; for a &&.

    When separating two commands with a semi-colon, the shell merely runs the two commands in series. Using an && to separate the commands will cause the shell to only run the second command if the first command is successful.

    Imagine a scenario where there is no ~/.Trash directory, due to corruption or the $HOME being mangled or some other reason. With the ; separator, the cd command would fail but the system would happily still run the following rm command in the user's home directory or current working directory.

    I suggest:

    Code:
     cd ~/.Trash && rm -r *
    Although I don't see anything wrong with:

    Code:
    rm -r ~/.Trash/*
    as a single-command alternative.
     
  15. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #15
    ^^^
    I completely agree. Well described.


    One addendum:
    The trash on other disks isn't emptied when ~/.Trash/* is removed. Examples: internal partition, external HD, USB thumb drive, network share, Firewire drive. I mention this only for completeness.
     
  16. PipPip macrumors newbie

    Joined:
    Nov 9, 2008
    Location:
    UK
    #16
    Just one question .... If it takes 15 minutes to empty via the GUI, are you sure you don't have "secure delete" option set, as that will then take a lot longer in the GUI, whereas the "rm" command is a straight delete so is always quicker.
     
  17. MacLadybug thread starter macrumors 6502a

    MacLadybug

    Joined:
    Jun 6, 2008
    #17
    Yes I use Secure Empty...

    So, now I've changed the Script to:
    do shell script "srm -vrmf ~/.Trash/*"

    Seems to work fine.
     
  18. TuffLuffJimmy macrumors G3

    TuffLuffJimmy

    Joined:
    Apr 6, 2007
    Location:
    Portland, OR
    #18
    Again. The script already ships with OS X and it works more efficiently, because it doesn't have to run in AppleScript. If you would just use the Finder you could save yourself some minor resources.
     
  19. MacLadybug thread starter macrumors 6502a

    MacLadybug

    Joined:
    Jun 6, 2008
    #19
    After all the fiddleing around, you are 100% correct. I can't shortcut how long it takes for a bunch of junk to "secure" empty. If there is a bunch of stuff (movies, etc.) it's going to take just as long when it's done "securely". So it's just click the secure empty button in trash or set finder preferences to "secure empty" trash and wait it out. I guess there is no way to just flush it fast cause it overwrites the space.

    Thanks for the patience. I did learn a lot a few new things along the way. I had never tried to work with Applescripts before, so this was new and interesting anyway.
     
  20. TuffLuffJimmy macrumors G3

    TuffLuffJimmy

    Joined:
    Apr 6, 2007
    Location:
    Portland, OR
    #20
    If you're interested in AppleScript this is a good place to start. If you're interested in shell scripts here is a good place to start.
     
  21. MacLadybug thread starter macrumors 6502a

    MacLadybug

    Joined:
    Jun 6, 2008
    #21
    Thanks for the references. I'll definitely order one of those to get started.
     

Share This Page