2
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"))
1

You need to use string concatenation to get your variable in the string. Specifically in your case something like "/Users/" & answer & "/Desktop/untitled folder" should work, though you may need to wrap that in parenthesis as described here

1

AFAIR you have to use string concatenation (as moneyt mentioned in his answer) and convert it to a colon path as string:

-- Get and set name of current user
tell application "System Events"
    set UserName to name of current user
end tell

-- Create POSIX path string
set PosixPath to "/Users/" & UserName & "/Desktop/" & "untitled folder"

-- Convert POSIX path into colon path
set ColonPath to (POSIX file PosixPath) as string

-- Delete folder and empty trash
tell application "Finder"
    delete ColonPath
end tell

tell application "Finder"
    empty the trash
end tell

A simpler version without user-defined variables but reverting to a Finder property in the AppleScript suite "Finder Basics" (home (folder, r/o) : the home directory) is:

tell application "Finder"
    delete folder "untitled folder" of folder "Desktop" of home
end tell

tell application "Finder"
    empty the trash
end tell

Please don't use simple variable names like "answer" because they may conflict with AppleScript's reserved variables/strings!

1

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.

  • 1
    I like your "KISS" approach (as you mentioned the other day in a different post) with your answer here +1 ;P – wch1zpink Jan 11 '18 at 4:01
0

If you run the script while being logged in as the user that you want to delete the directory for, you can just use this as your file path ~/Desktop/ The tilde(~) symbolizes /Users/[Current username]

  • AppleScript doesn't take relative pathnames. ~/Desktop would throw an error. – CJK Jan 11 '18 at 3:05
  • But bashscripts in AppleScript does – Harcker Jan 11 '18 at 9:39

You must log in to answer this question.

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