Can't get folder - please help

Hi guys,

I would very much appreciate your help in understanding what is going wrong here.
I have a folder named /Users/morb/Downloads/Test - which contains files.

When I run this script I get the error: Finder got an error: Can’t get folder “/Users/morb/Downloads/Test/”.


try
tell application “Finder”

delete (every item of folder ("/Users/morb/Downloads/Test/"))

end tell

on error
display dialog (“Error. Couldn’t delete files”) buttons {“OK”}
end try

Morten

Hi Morten. Welcome to MacScripter.

The Finder doesn’t understand POSIX paths (with slashes), only its own references, specifiers based on HFS paths (with colons), or aliases. You have quite a few options!

try
	tell application "Finder"
		
		delete (every item of folder ("Users:morb:Downloads:Test:") of startup disk)
		-- Or: 
		-- delete (every item of folder ("Downloads:Test:") of home)
	end tell
	
on error
	display dialog ("Error. Couldn't delete files") buttons {"OK"}
end try

Or:

set testFolderPath to (POSIX file "/Users/morb/Downloads/Test") as text
-- Or:
-- set testFolderPath to (path to downloads folder as text) & "Test:"

try
	tell application "Finder"
		
		delete (every item of folder testFolderPath)
		
	end tell
	
on error
	display dialog ("Error. Couldn't delete files") buttons {"OK"}
end try

Or:

set testFolder to (POSIX file "/Users/morb/Downloads/Test") as alias
-- Or:
-- set testFolder to ((path to downloads folder as text) & "Test:") as alias

try
	tell application "Finder"
		
		delete (every item of testFolder)
		
	end tell
	
on error
	display dialog ("Error. Couldn't delete files") buttons {"OK"}
end try

Or you could use System Preferences instead, which does understand specifiers based on POSIX paths but has its own peculiarites:

try
	tell application "System Events"
		
		move (get every disk item of folder "/Users/morb/Downloads/Test/") to trash
		
	end tell
	
on error
	display dialog ("Error. Couldn't delete files") buttons {"OK"}
end try

Dear Nigel,

Thank you so much. I truly appreciate your post with some good explanations.

Best regards,
Morten