PDA

View Full Version : delete file from root volume




Jiggy1965
Apr 20, 2011, 07:19 AM
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?



Big Dave
Apr 20, 2011, 07:24 AM
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?

Instead of using AppleScript, I would suggest using a terminal command. Based on the info you provided, I would use:
sudo rm -rf /file.jpg

jiminaus
Apr 20, 2011, 07:39 AM
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?

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.

Jiggy1965
Apr 20, 2011, 07:58 AM
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?

lee1210
Apr 20, 2011, 08:45 AM
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?

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

Jiggy1965
Apr 20, 2011, 09:49 AM
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:
tell application "Finder"
delete file "/testfile.ai"
end tell

but then I get an error dat the file cannot be found.

lee1210
Apr 20, 2011, 10:00 AM
I posted a link that included information about using POSIX paths in AppleScript so you could have it both ways.

-Lee

jared_kipe
Apr 20, 2011, 10:19 AM
I posted a link that included information about using POSIX paths in AppleScript so you could have it both ways.

-Lee

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"

lee1210
Apr 20, 2011, 10:29 AM
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"

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

Bill McEnaney
Apr 20, 2011, 11:03 AM
Instead of using AppleScript, I would suggest using a terminal command. Based on the info you provided, I would use:
sudo rm -rf /file.jpg
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.

jared_kipe
Apr 20, 2011, 11:09 AM
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.

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.

notjustjay
Apr 20, 2011, 11:16 AM
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.

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.

jared_kipe
Apr 20, 2011, 11:42 AM
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.

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.

Bill McEnaney
Apr 20, 2011, 02:56 PM
rm -rf / file.jpg

Will remove your entire hard drive!
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.

Bill McEnaney
Apr 20, 2011, 03:00 PM
Well if we're picking nits, I doubt the sudo is even applicable.
And you don't need the "f" in "-rf". Ooh, I love to pick nits. ;)

jiminaus
Apr 20, 2011, 03:52 PM
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.


/$ ls -del /
drwxr-xr-x 32 root admin 1156 15 Apr 15:15 /

/$ touch /a
touch: /a: Permission denied

/$ sudo touch /a

/$ ls -del a
-rw-r--r-- 1 root admin 0B 21 Apr 06:48 a

/$ rm /a
override rw-r--r-- root/admin for /a? y
rm: /a: Permission denied

/$ sudo rm /a

/$ ls -del a
ls: a: No such file or directory

Big Dave
Apr 20, 2011, 03:59 PM
Wow guys. I have never seen one of my "one liners" get examined so extensively.

Granted:
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?

notjustjay
Apr 20, 2011, 04:26 PM
Wow guys. I have never seen one of my "one liners" get examined so extensively.

Granted:
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?

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:

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/ :)

Bill McEnaney
Apr 20, 2011, 05:28 PM
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.

Jiggy1965
Apr 20, 2011, 05:38 PM
I posted a link that included information about using POSIX paths in AppleScript so you could have it both ways.

-Lee

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?

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?
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'?

jared_kipe
Apr 24, 2011, 09:53 AM
And you don't need the "f" in "-rf". Ooh, I love to pick nits. ;)

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.

McGordon
Apr 24, 2011, 10:05 AM
If you want to do it in Applescript without using UNIX style paths, try this.


tell application "Finder"
delete file "deleteme" of startup disk
end tell


Which moves the file to the trash, the same as this:

tell application "Finder"
move file "deleteme" of startup disk to trash
end tell

KnightWRX
Apr 24, 2011, 10:12 AM
Why Applescript ? And have the users double click something ? Just use standard bourne shell script :


#!/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 :


#!/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 :


#!/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 :


#!/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 :



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 :


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

larkost
Apr 24, 2011, 12:39 PM
Wow... an awful lot of talk here, and no one has suggested the most obvious answer:
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...