Applescript to delete specific folder

Hi guys,

i am quiet new to this forum and to applescripting.
I just made my first real apple script today but found some things which i dont really understand.
Below i have my apple script which works great.


set folderToDelete to "Mac OS X:Users:constantin:Downloads:incomplete"
set folderGetSize to "Users/constantin/Downloads/incomplete"

tell application "Finder"
	
	set folderToSyncSize to (size of (info for folderGetSize))
	if folderToSyncSize < 20000000 then
		if folder "Mac OS X:Users:constantin:Downloads:incomplete" exists then
			delete entire contents of folder folderToDelete
		end if
	end if
end tell

But as you can see there are two defenitions for the folder where i want to delete the contents in.
Why do i have to define the folder with “:” if i want to delete the contents.
And if i want to have information about the folder i have to use “/”.
I really dont understand.

I hope somebody can explain this to me.

Hi,

a colon separated string path is an HFS path, the primary path type of AppleScript.
A slash separated string path is a POSIX path, it’s needed when talking to the shell.

The reason why info for seems to work only with a POSIX path is that info for actually expects an alias or file specifier,
for example


size of (info for alias folderToDelete)

In your script you’re retrieving first the size of the folder and later you’re checking for existence which is useless.
I recommend to use System Events to get the folder size “ info for is deprecated anyway “ and the Finder to delete the items.
The code within the try block will be skipped if the folder does not exist.
entire contents is not needed because when a folder is going to be deleted all contents of this particular folder will be deleted implicitly.

path to downloads folder is a relative path to the downloads folder of the current user


set folderToDelete to (path to downloads folder as text) & "incomplete:" -- HFS path
try
	tell application "System Events" to set folderToSyncSize to size of folder folderToDelete
	if folderToSyncSize < 20000000 then
		tell application "Finder" to delete every item of folder folderToDelete
	end if
end try

Hey Cor180,

If there’s a bunch of stuff in that folder it will be a lot faster to delete the folder and not mess with the contents. If the original folder is needed then it can be recreated.


set parentPath to (path to downloads folder as text)
set folderToDeleteName to "Incomplete"
set folderToDeletePath to parentPath & folderToDeleteName & ":"
set folderToDeleteAlias to alias folderToDeletePath

tell application "System Events"
	set fldrSize to size of folderToDeleteAlias
end tell

if fldrSize > 0 then
	tell application "Finder"
		delete folderToDeleteAlias
		make new folder at folder parentPath with properties {name:folderToDeleteName}
		tell alias parentPath to update
	end tell
end if

Do you have the Applescript Language Guide? Download the pdf version for searchability.

Are you familiar with paths and file/folder objects?

If not then you should spend a little time getting to know them.

A path is nothing more than a string.

Some applications and some AppleScript commands can understand the bare path string, but in general it’s better to turn it into an object before working with it.

The most portable file/folder object is an alias.

You can play with the following script to see a small demonstration.

Comment-out and uncomment lines to see what’s what.

Provide an HFS path appropriate to your system for the script to work with.


set myAlias to alias "Ryoko:Users:chris:Downloads:Test_Folder:" # An alias

tell application "Finder"
	set fldr to "Ryoko:Users:chris:Downloads:Test_Folder:" # An HFS Path String
	# Uncomment the following line to see it fail.
	# set fldrProp1 to properties of fldr # The Finder can't resolve the string.
	set fldrProp1 to properties of folder fldr # We've turned the string into a folder-object in the Finder.
	set fldrProp2 to properties of myAlias
	set finderFldrObject to folder fldr
end tell

tell application "System Events"
	# Uncomment the following line to see it fail.
	# size of finderFldrObject # Fails: System Events doesn't like the Finder-object.
	set systemEventsFolderObject to folder fldr
	size of systemEventsFolderObject
	size of myAlias
end tell