Pallav Agarwal Pallav Agarwal - 1 month ago 8
AppleScript Question

Make apple script code work on any laptop (not need the user's name)

I have a simple code of deleting a file and making a folder. But how do I make it run on any laptop because everyone's user name is different. I know it has something to do with "~" but i'm not sure where.
The code below works properly but how do I make sure it can run on any mac:

try
tell application "Finder"
delete ((POSIX file "/Users/myname/Desktop/thefile.txt"))
make new folder at (POSIX file ("/Users/myname/Desktop")) with properties {name:"thefolder"}
end tell
on error
display dialog ("That didnt work") buttons {":("}
end try

Answer

The syntax independent of the user name is much simpler since the desktop folder of the current user is the root folder of the Finder

try
    tell application "Finder"
        delete file "thefile.txt"
        make new folder at desktop with properties {name:"thefolder"}
    end tell
on error
    display dialog ("That didnt work") buttons {":("}
end try