Sunday, November 19, 2017

#1 2017-08-29 05:47:12 am

morb
Member
Registered: 2013-05-15
Posts: 2

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


Filed under: PATH

Offline

 

#2 2017-08-29 06:24:46 am

Nigel Garvey
Moderator
From:: Warwickshire, England
Registered: 2002-11-20
Posts: 4422

Re: Can't get folder - please help

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!

Applescript:

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:

Applescript:

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:

Applescript:

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:

Applescript:

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


NG

Offline

 

#3 2017-08-29 06:38:31 am

morb
Member
Registered: 2013-05-15
Posts: 2

Re: Can't get folder - please help

Dear Nigel,

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


Best regards,
Morten

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)