[Apple] How do you put a variable in the middle of a pathway? (AppleScript)

Solution 1:

You need to put parentheses around:

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


(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")


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.

Solution 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…