delete file from root volume

Discussion in 'Mac Programming' started by Jiggy1965, Apr 20, 2011.

  1. macrumors newbie

    Joined:
    Oct 7, 2008
    #1
    Code:
    tell application "Finder"
       delete file "Macintosh HD:file.jpg"
    end tell
    I can use this code to delete a file from the root of my startup disc, which by default is called 'Macintosh HD'. But if I want to share this applescript app to other users and they happen to have a different name for 'Macintosh HD' how do I script it that it looks for the file in the root of their self renamed harddrive? No matter ware the app is placed?

    Is there some code like 'delete file "_root: file.jpg" which would do that?
     
  2. macrumors 6502

    Joined:
    Nov 27, 2007
    Location:
    Crestview, Fl
    #2
    Instead of using AppleScript, I would suggest using a terminal command. Based on the info you provided, I would use:
    Code:
    sudo rm -rf /file.jpg
     
  3. macrumors 65816

    jiminaus

    Joined:
    Dec 16, 2010
    Location:
    Sydney
    #3
    I hope this is just a simplification of your actual problem because nothing should ever be written into the root folder, except maybe in some very rare circumstances, and only if the user explicitly chose to put it there via a save panel.
     
  4. thread starter macrumors newbie

    Joined:
    Oct 7, 2008
    #4
    Yes, well, I know :) It are just three log files which a plugin leaves behind in Macintosh HD every time it is used. The application plugin might not be perfect that way ;) But I know it's save to delete those (and I want to delete those cause they build up in size quickly (again: not perfect ;) ).

    But anyway, back to my script: how do I delete something from Macintosh HD when the user might have renamed it to something else?
     
  5. macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #5
    Your question was already answered by Big Dave. The root of the file system can always be accessed via / when you're using POSIX paths. You can specify something is a POSIX path in applescript:
    http://docs.info.apple.com/article.html?path=AppleScript/2.1/en/as208.html

    Between what Big Dave gave you and the information in that link you should be good to go.

    -Lee
     
  6. thread starter macrumors newbie

    Joined:
    Oct 7, 2008
    #6
    I know, but it wouldn't be possible in applescript? Thought it to be convenient for non-technical users to just double click on the applescript application and the files would be gone instead of them typing terminal commands ;)

    I've tried this:
    Code:
    tell application "Finder"
    	delete file "/testfile.ai"
    end tell
    but then I get an error dat the file cannot be found.
     
  7. macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #7
    I posted a link that included information about using POSIX paths in AppleScript so you could have it both ways.

    -Lee
     
  8. macrumors 68030

    jared_kipe

    Joined:
    Dec 8, 2003
    Location:
    Seattle
    #8
    I don't use applescript but the documentation you linked to doesn't seem to "have it both ways." It takes a : separated applescript file description and returns the POSIX / separated version.

    That said, reading between the lines seems to indicate if you want to remove "/testfile.txt" then the applescript version would be "HD:testfile.txt"
     
  9. macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #9
    Oops. Misread. In any event, it seems like you could:
    do shell script "rm /theFile.txt"

    Then you don't have to worry about one path type or another.

    -Lee
     
  10. macrumors 6502

    Joined:
    Apr 29, 2010
    #10
    That command will do what you want it to do, but you probably don't need the "r" in "-rf". Why because you don't need to recursion when you remove a file from your current directory. But you would need "-r" if you were going to remove a directory that contained subdirectories.
     
  11. macrumors 68030

    jared_kipe

    Joined:
    Dec 8, 2003
    Location:
    Seattle
    #11
    Well if we're picking nits, I doubt the sudo is even applicable.

    But seriously, the -rf flag is important to remember because it is the most general, and it doesn't matter if it is a file, or a folder, or a folder that looks like a file.
     
  12. macrumors 603

    notjustjay

    Joined:
    Sep 19, 2003
    Location:
    Canada, eh?
    #12
    Agreed, but it is also the most potentially damaging, so newbie users need to be very careful!

    For example:

    rm -rf /file.jpg

    Will remove one file called "file.jpg".

    rm -rf / file.jpg

    Will remove your entire hard drive!

    Note the one (tragically) misplaced space.
     
  13. macrumors 68030

    jared_kipe

    Joined:
    Dec 8, 2003
    Location:
    Seattle
    #13
    All I can say is 1) newbies probably shouldn't be doing these sorts of things, or be extra careful and think things through before hitting <enter>, and 2) I've never EVER made a mistake like that (and I am either a newbie or at least once was).

    And also, it is roughly akin to saying people learning to drive a stick should have their reverse gears removed because it is just one gear away from all of the forward gears and they might accidentally back into a car.
     
  14. Bill McEnaney, Apr 20, 2011
    Last edited: Apr 20, 2011

    macrumors 6502

    Joined:
    Apr 29, 2010
    #14
    Nope, it'll delete everything that's already stored on the hard disk that's in the hard drive. :)

    In the old days when I learned Cobol, computer operators put disk packs into hard drives. The packs looked a little like cake dishes with clear plastic covers. If you looked through the cover, you would see each of about five hard disks sitting horizontally on the same spindle. When the computer operators needed to put a different customers files into the hard drive, they just replaced the current disk pack with the other customer's disk pack.

    A professor told me that one day, when a computer operator didn't notice a piece of paper that fell into a disk pack, everyone heard an awful high-pitched noise when a read-write head touched the paper and crashed into a hard disk. The dutiful computer operator got a backup disk pack, put it into the same drive, and bang, another head-crash. So both damaged disk packs were useless until they got repaired.
     
  15. macrumors 6502

    Joined:
    Apr 29, 2010
    #15
    And you don't need the "f" in "-rf". Ooh, I love to pick nits. ;)
     
  16. macrumors 65816

    jiminaus

    Joined:
    Dec 16, 2010
    Location:
    Sydney
    #16
    A saved shell script the executable bit(s) set is as double-clickable as a saved AppleScript.

    In regards to sudo, under 10.6.7 I can't write to / without it.

    Code:
    [b]/$[/b] ls -del /
    drwxr-xr-x  32 root  admin  1156 15 Apr 15:15 /
    
    [b]/$[/b] touch /a
    touch: /a: Permission denied
    
    [b]/$[/b] sudo touch /a
    
    [b]/$[/b] ls -del a
    -rw-r--r--  1 root  admin     0B 21 Apr 06:48 a
    
    [b]/$[/b] rm /a
    override rw-r--r--  root/admin for /a? y
    rm: /a: Permission denied
    
    [b]/$[/b] sudo rm /a
    
    [b]/$[/b] ls -del a
    ls: a: No such file or directory
    
     
  17. macrumors 6502

    Joined:
    Nov 27, 2007
    Location:
    Crestview, Fl
    #17
    Wow guys. I have never seen one of my "one liners" get examined so extensively.

    Granted:
    Code:
    sudo rm -rf /file.jpg
    This is risky given the permissions/location/action but the OP wanted to remove a file from /. I guess a safer way to remove the file is to drag and drop to trash?
     
  18. macrumors 603

    notjustjay

    Joined:
    Sep 19, 2003
    Location:
    Canada, eh?
    #18
    Well, I was really just pointing out that terminal commands are not for the beginner user, especially not anything with the words "sudo", "delete" and "recursive" in the same sentence! My advice to anyone is don't use a command unless you know exactly what it is you're typing in, as that will at least help you understand if you're about to make a mistake or not due to a typo.

    I would recommend that if it was just to delete a single file, to remove the "r" in the command:

    Code:
    sudo rm -f /file.jpg
    That way the worst that can happen is that the single file gets deleted -- which is what you want, anyway.

    As for the trash, that's an option too, to move the files into ~/.Trash/ :)
     
  19. macrumors 6502

    Joined:
    Apr 29, 2010
    #19
    Maybe everybody needs to use rm's "-i" directive because it makes the computer ask you whether you're sure you want to delete what you're trying to delete.
     
  20. thread starter macrumors newbie

    Joined:
    Oct 7, 2008
    #20
    To go back to my applescript editor way to do this.... ;-)

    I've read the link and understand dat in applescript I have to use colon pathnames like this?

    Code:
    tell application "Finder"
    	delete file "HD:testfile.ai"
    end tell
    This would remove the file from any harddisk's root location no matter what the harddrive is called?

    Or should the POSIX slash way also work in Applescript like this?
    Code:
    tell application "Finder"
    	delete file "/testfile.ai"
    end tell
    And if so, why doesn't it work when I run this script and do I get an 'error requesting file'?
     
  21. macrumors 68030

    jared_kipe

    Joined:
    Dec 8, 2003
    Location:
    Seattle
    #21
    Well you never know on that one, the whole reason for this script might have been to delete a stubborn file programatically. Most of the time people don't write scripts to delete files that they could just lasso+command+delete in Finder.
     
  22. macrumors member

    Joined:
    Dec 28, 2010
    Location:
    Scotland
    #22
    If you want to do it in Applescript without using UNIX style paths, try this.

    Code:
    tell application "Finder"
    	delete file "deleteme" of startup disk
    end tell
    
    Which moves the file to the trash, the same as this:
    Code:
    tell application "Finder"
    	move file "deleteme" of startup disk to trash
    end tell
    
    
     
  23. KnightWRX, Apr 24, 2011
    Last edited: Apr 24, 2011

    macrumors Pentium

    KnightWRX

    Joined:
    Jan 28, 2009
    Location:
    Quebec, Canada
    #23
    Why Applescript ? And have the users double click something ? Just use standard bourne shell script :

    Code:
    #!/bin/sh
    FILETODELETE="/filename"
    
    if [ -f $FILETODELETE ]; then
           #no need for -r, -r is for recursive, this is just a file, not a dir.
           rm -f $FILETODELETE 
    fi
    
    Just load that up in root's crontab so that cron executes it periodically. No need to double click anything, no need for user interaction and no harm done to the system.

    OS X is a Unix system. No need to get fancy to do easy tasks like this.

    Or let's get fancy. Let's make it so that we can delete multiple files specified in the variable so we don't need no clunky copy/pasted code all over :

    Code:
    #!/bin/sh
    FILETODELETE="afile bfile cfile"
    
    for FILE in $FILETODELETE
    do
    	if [ -f $FILE ]; then
    		rm -f $FILE
    	fi
    done
    
    Now, of course that requires editing the code each time you want to add/remove a file. Which would be easier to push ? A new script or a new crontab ? Well let's say a crontab is easier to push, we could also just delete whatever is passed on the command line :

    Code:
    #!/bin/sh
    FILETODELETE=$@
    
    for FILE in $FILETODELETE
    do
    	if [ -f $FILE ]; then
    		rm -f $FILE
    	fi
    done
    
    Now, let's just add some logging in case someone pushes the wrong file and we want to see what it's doing :

    Code:
    #!/bin/sh
    FILETODELETE="afile bfile cfile"
    
    for FILE in $FILETODELETE
    do
    	if [ -f $FILE ]; then
                    # Log a message and do the erasing
    		logger Erasing $FILE as it is present
    		rm -f $FILE
    	fi
    done
    
    Now we get some nice log outputs of the activity of this script which will run from cron :

    Code:
    
    Apr 24 11:20:03 host user[2185]: Erasing afile as it is present
    Apr 24 11:20:03 host user[2187]: Erasing bfile as it is present
    Apr 24 11:20:03 host user[2189]: Erasing cfile as it is present
    
    Just push it out to cron multiple times per day as no harm is done if the files aren't there :

    Code:
    # delete plug-in log files 6 times per day, at HH:15
    15 */4 * * *       /path/to/script/erase-log.sh >/dev/null 2>&1
    
     
  24. macrumors 6502a

    Joined:
    Oct 13, 2007
    #24
    Wow... an awful lot of talk here, and no one has suggested the most obvious answer:
    Code:
    get path to startup disk
    This is built into AppleScript, and will always get you the AppleScript-style path to the startup volume.

    Of course there are still problems with this whole exercise...
     

Share This Page