path to desktop not updating inside AppleScript application

So when I ran the below on a new user account as part of my AppleScript application it gave me error: sh: line 0: cd: /Users/username/Desktop/Filename/Filename2: No such file or directory. sh: file.txt: Permission denied

When I replaced:

property source : POSIX path of (path to desktop as text) & "Folder1" & "/Folder2"

With the below, everything worked fine. Any ideas why the path to desktop wasn’t updating?

property source : "~/Desktop/Folder1/Folder2"
property resultFile : quoted form of ("file.txt")
property source : POSIX path of (path to desktop as text) & "Folder1" & "/Folder2"

do shell script "cd " & source & "; rm -f " & resultFile & ¬
	"; cat *.txt > " & resultFile

Hi,

the initial value of a property is set at compile time, so if you want to use scripts in other user accounts or computers never use relative paths in properties.


property source : missing value

set source to POSIX path of (path to desktop) & "Folder1" & "/Folder2"

In addition to Stefan’s post. Tilde paths aren’t supported in AppleScript so my advise would be never use tilde paths at all.

As far as the relative paths go, how else would you reference a folder on the desktop with every mac that you might use this on?

I use (path to desktop as text) multiple times in other places of the Application and all those work fine except for this instance below which I have to replace with: “~/Desktop/Folder1/Folder2”

Because of the replacement everything works. I was just wandering why for only this Path to desktop it wasn’t working.

You are correct DJ Bazzie Wazzie that AppleScript doesn’t support the unix Tilde reference. Unix does, however. And in this instance, it is the solution that works.

property resultFile : quoted form of ("file.txt")
property source : POSIX path of (path to desktop as text) & "Folder1" & "/Folder2"

do shell script "cd " & source & "; rm -f " & resultFile & ¬
   "; cat *.txt > " & resultFile

The important part is to not use a relative path (like Path To Desktop) in a property. A property is compiled and saved into the app. It will not reset to the new user’s path when you move the script to another Mac.

Either use a local variable, or if you need it to be a property make sure the code is such that the value gets cleared out and re-set when the app runs.

Model: iMac
Operating System: Mac OS X (10.6)