3

I am trying to write code that deletes files from a specified location and empties into the trash. If I use the direct string it works but when I try to add a variable it does not. Could someone help show me what I'm doing wrong?

When I tried to look up the error code number at the bottom, it didn't give me much insight.

Valid

tell application "Finder"
        delete (every item of folder "Macintosh HD:Users:sme0219:Pictures:Camera" whose name contains ".png")
end tell

Invalid

tell application "System Events"
    set userName to name of current user
    set compName to name of startup disk
end tell

tell application "Finder"
    delete (every item of folder compName & ":Users:" & userName & ":Pictures:Camera" whose name contains ".png")
end tell

Error Report

error "Can’t get {folder "Applications" of startup disk of application "Finder", folder "Library" of startup disk of application "Finder", folder "System" of startup disk of application "Finder", folder "Users" of startup disk of application "Finder", ":Users:", "sme0219", ":Pictures:Camera"} whose name contains ".png"." number -1728

2
  • This is a question about coding, and thus off-topic Commented May 21, 2021 at 1:47
  • 5
    @ScottEarle Since when are questions about AppleScript off topic? “We do encourage AppleScript, Automator, and UNIX shell scripting questions as well as how to use tools like Xcode for non-language specific tasks.” Taken from here —> apple.stackexchange.com/help/on-topic
    – wch1zpink
    Commented May 21, 2021 at 4:08

3 Answers 3

3

You need to put parentheses around:

compName & ":Users:" & userName & ":Pictures:Camera"

Example:

(compName & ":Users:" & userName & ":Pictures:Camera")

This concatenates everything within the parentheses as the full path, otherwise Finder just sees:

delete (every item of folder compName & ":Users:" & userName & ":Pictures:Camera" whose name contains ".png")

As:

get every item of folder "Macintosh HD"

Not the entire path.

So, this works:

tell application "System Events"
    set userName to name of current user
    set compName to name of startup disk
end tell

tell application "Finder"
    delete (files of folder (compName & ":Users:" & userName & ":Pictures:Camera") whose name contains ".png")
end tell


Instead of relying on System Events to get that information, you can use the path to (folder) convention to ascertain its path, e.g.:

set targetFolder to (path to pictures folder from user domain) & "Camera" as string

tell application "Finder"
    delete (files of folder targetFolder whose name contains ".png")
end tell

Please take a moment to read the linked document as I believe you'll find it quite informative.

Also note that since you stated code that deletes files in the OP and your code had the potential to delete a folder having a .png extension that every item has bee changes to files with the exception of the line after "otherwise Finder just sees:" in this answer as that line is part of explaining what the issue is that you are having.

0
2

This also works…

property folderPath : (path to users folder from system domain as text)

tell application "System Events"
    delete (files of alias (folderPath & name of current user & ¬
        ":Pictures:Camera:") whose name contains ".png")
end tell

NOTE This option deletes the files permanently and will not go to Trash.


FWIW… I decided to use System Events to delete the ”.png” files because after testing the code with Finder deleting 400 “.png” files then with System Events moving 400 “.png” files to the .Trash folder… It took Finder 26 seconds to delete them all vs. System Events took 3 seconds to move them all to the .Trash folder (Rather than deleting them immediately, which took 1.3 seconds).

enter image description here


You may want to consider trying to get in the habit of using System Events, rather than Finder to handle commands that they both have in common. Using Finder to process large amounts of items can often create huge bottlenecks when running your AppleScripts


In response to your question in the comment on this post, this following code would do the job.

tell application "System Events"
    tell current application to set userName to do shell script "whoami"
    set compName to name of (startup disk)
    set trashPath to (compName & ":Users:" & userName & ":Pictures:Camera")
    delete (every file of folder trashPath whose name contains ".png")
end tell

System Events doesn’t have an item class to act on so you would need to use delete every file… Not delete every item…

2
  • So theoretically I could replace Finder with System Events in this instance? tell application "Finder" set userName to do shell script "whoami" set compName to name of (startup disk) set trashPath to (compName & ":Users:" & userName & ":Pictures:Camera") delete (every item of folder trashPath whose name contains ".png") end tell
    – Michelle
    Commented May 23, 2021 at 21:31
  • The do shell script command is not a Finder command. It is a Standard Additions command. In this case, Finder worked it out and figured out which command to run. Therefor it would not be wrapped inside any tell block. However if you need to include a Standard Additions command within another application’s Tell block, the general accepted practice would be to do this… tell current application to do shell script . Which basically means you are telling the current script, NOT Finder, to run that command. See the edit in my post.
    – wch1zpink
    Commented May 24, 2021 at 17:08
-2

This is what I ended up doing with 100% success:

tell application "Finder"
    set userName to do shell script "whoami"
    set compName to name of (startup disk)
    set trashPath to (compName & ":Users:" & userName & ":Pictures:Camera")
    delete (every item of folder trashPath whose name contains ".png")
end tell

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .