MacOS – How to use a variable inside a path in AppleScript

applescriptdeletingfindermacos

tell application "System Events"
name of current user
end tell
tell application "System Events"
set answer to name of current user
end tell
tell application "Finder"
delete ((POSIX file "/Users/answer/Desktop/untitled folder"))
end tell
tell application "Finder"
empty the trash
end tell

I want to make a universal code to delete this specific folder but I can't seem to make my variable (=answer) work in the quoted path.

answer

.

delete ((POSIX file "/Users/answer/Desktop/untitled folder"))

Best Answer

The following lines of AppleScript code replaces all of the code in your OP:

set thisFolder to POSIX path of (path to desktop folder as string) & "untitled folder"
try
    tell application "System Events" to delete folder thisFolder
end try

Note that this directly deletes thisFolder and its contents, bypassing the Trash, so be sure this is what you want before using it!

As coded, if thisFolder does not exist, the try command keeps it from fatally halting execution of the code. In other words, you'll not get an error message and it will continue processing additional code in the script, if any.