I’m writing a script that installs an application in a user’s Applications folder (not the Applications folder in the full system, but the one in the user’s home directory). To determine if the user has an application folder already, I use this code:
tell application "System Events"
set appUserPath to path of application folder of user domain
end tell
If this produces an error, I create an Applications folder in the user domain.
A problem arises if the user has recently moved the ~/Applications folder to the trash, or moved it to the desktop temporarily, or done something else with it. No matter where that folder is, System Events still thinks that it remains the application folder of the user domain.
Is there a way to tell System Events to set a folder that I create to become the application folder of the user domain, no matter what other folder might already be designated as the application folder of the user domain?
I’ve searched for the answer to this, and studied the dictionaries, but I’m a beginner with AppleScript, and may have missed something obvous. Thanks for any advice.
I recommend to take the home folder as relative path and hard-code the Applications folder like
set appUserPath to ((path to home folder as text) & "Applications:")
try
appUserPath as alias -- throws an error if the folder doesn't exist
on error
tell application "Finder" to make new folder at home with properties {name:"Applications"}
end try
One question: is the Applications folder named “Applications” in non-English-language systems? I thought (perhaps wrongly) that the reason to ask System Events for the “application folder” was that it would give the name of the folder even if the folder had a non-English name. Or am I simply wrong about this?
The internal name of the folder is always “Applications” regardless of the display in the Finder.
If you want to keep the localized names, you have to add a little invisible zero byte file named .localized
in the Applications folder. In the following script the folder will be created by a shell command and
the .localized file will be added. Consider that the variable appUserPath now contains a slash separated POSIX path
set appUserPath to POSIX path of (path to home folder) & "Applications/"
try
POSIX file appUserPath as alias -- throws an error if the folder doesn't exist
on error
do shell script "/bin/mkdir -p " & quoted form of appUserPath & " ;/usr/bin/touch " & quoted form of (appUserPath & ".localized")
end try