small and easy question [Delete file]

heya, im a bit new to this stuff and ive been browsing your forums and learning etc.
but i have a quick question
lets say i have a folder on my desktop, and inside that folder i have a certain file.

can i make a script to go in and delete the file within the folder?


if you know the name of the file yes!
you can hardcode that into your script if not just choose it using something like this

tell application "Finder"
	set p to (choose file) --alias
	delete p
end tell


welcome to MacScripter :slight_smile:

this is quite easy.
assuming the name of your folder is “myFolder” and the name of the file is “myFile”

tell application "Finder" to delete file "myFile" of folder "myFolder"


tell application "Finder" to delete file "myFolder:myFile"

Note: this works only, if the folder is on desktop (which is the “root” folder of the Finder),
otherwise you must specify the whole path


Note that the root folder is the startup disk and not the desktop. You shouldn’t use partial references as the root folder might change with AS versions.


ah ok cool. thanks for the fast replies!

Actually, Kel. Stefan’s right. If a path begins with a name other than that of a mounted disk, the Finder takes the first element in the path to be an item on the user’s desktop. It’s always done that. If a disk and a desktop item have the same name, the disk takes priority.

Hi Nigel,

This might be os specific then. Can you test this out and see which folder is deleted?

tell application "Finder"
	make new folder at desktop with properties {name:"myFolder"}
	duplicate result to startup disk
	delete item "myFolder"
end tell


Slight misunderstanding I think, kel. Nigel means that, if you have a folder on your desktop with the same name as that of a disk, then specifying item/folder will return the disk “ rather than the folder of the same name. (However, that doesn’t change the fact that the desktop is still Finder’s default object.)

tell application "Finder"
	make folder with properties {name:"Renaissance"} ” use an existing disk name
	{item "Renaissance", folder "Renaissance", item "Renaissance" of desktop}
end tell

--> {disk "Renaissance" of application "Finder", disk "Renaissance" of application "Finder", folder "Renaissance" of folder "Desktop" of folder "kai" of folder "Users" of startup disk of application "Finder"}

Hi Kai,

I see. Disks have priority.


The behaviour’s kinda contextual, Kel (hence some occasional confusion).

If a single object (folder/item) is specified by name, and a disk exists with that name, then that’s what is referenced. Otherwise, disks are generally ignored (unless, of course, the class disk is specified).

tell application "Finder"
	set single_ref to item "Renaissance"
	set multiple_ref to items whose name is "Renaissance"
	set pre_folder to {single_ref:single_ref, multiple_ref:multiple_ref}
	make folder with properties {name:"Renaissance"} -- use an existing disk name
	set single_ref to item "Renaissance"
	set multiple_ref to items whose name is "Renaissance"
	set post_folder to {single_ref:single_ref, multiple_ref:multiple_ref}
end tell

{pre_folder:pre_folder, post_folder:post_folder}

--> {pre_folder:{single_ref:disk "Renaissance" of application "Finder", multiple_ref:{}}, post_folder:{single_ref:disk "Renaissance" of application "Finder", multiple_ref:{folder "Renaissance" of desktop of application "Finder"}}}

Hi, Kel. You’re a Jaguar user, aren’t you?

When I run your test script on my Jaguar machine, I get the result you’re expecting, namely that the folder on the startup disk is deleted. In Tiger, the result is what I’m expecting “ that the folder on the desktop is deleted.

I think the reason I’ve never noticed the Jaguar behaviour before, despite years of using the system, is that it’s only slightly different from the behaviour on other systems and a conflict of names hasn’t occurred. The Jaguar Finder tries the root level of the startup disk after drawing a blank with disk names, then tries the desktop. With Tiger, OS 9, and (as I remember) OS 8, the Finder doesn’t try the startup disk at all.

Outside of Finder references, the first name in a path (if the path begins with a name not reserved by the system) is always taken to be that of a disk. If the path begins with a colon (HFS) or a slash (POSIX), the root folder is taken to be the startup disk. (In OS 9 and earlier, the root folder of a path beginning with a colon was the folder containing the application running the script. You could prepend more colons to specify a root folder further up the hierarchy. In OS 8, thanks to various bugs and quirks, it was possible “ though, of course, not a good idea “ to get an alias to the container folder of the application running the script with: ‘tell AppleScript’s text item delimiters to its middle Wednesday as alias’. :))

Hi Nigel,

When I jumped from pre-OSX to Jaguar, I had thought it strange that partial references would go from the root level of the startup disk to the desktop. I thought it was a UNIX thing. It was even stranger when nobody mentioned anything about it when I posted this behavior before. Bill Cheeseman’s articles were a good source of information on new bugs, but about that time he stopped doing reviews.

Your post should be an article. Thanks a lot for testing it out. Great information.

Have a good day,