macosapplescriptfilepathhfs

Avoiding username in an HFS file path


I recently put together an AppleScript file which invloves reading a file at one point. The command I am using to do this is:

set paragraph_ores to read file "Macintosh HD:Users:MYUSERNAME:Desktop:ORES.txt" using delimiter linefeed

Now, whilst this works perfectly and as expected, the problem comes that I was hoping to integrate this into a separate application which would run this script when necessary but had the realisation that everybody who would need to use the code would have to go into this script and specifically put in their username which may be tricky or tedious for some people.

In an attempt to circumvent this problem I looked to Google where I found out some more information about HFS file paths to which I had no prior knowledge to find out that the general formula is:

<Volume Name>:<Directory Name>:...:<Directory Name>:<Filename>

And an example they give to use this type of file path is:

Macintosh HD:Applications:Safari.app

This proved confusing as it did not reference the username at all. Therefore I tried changing my original command line to:

set paragraph_ores to read file "Macintosh HD:Desktop:ORES.txt" using delimiter linefeed

However, this proved unsuccessful and I was presented with an error. My next thought was to set a variable to the current username and integrate this into the path as shown below:

tell application "System Events"
    set username to name of current user
end tell
set paragraph_ores to read file "Macintosh HD:Users:" & username & ":Desktop:ORES.txt" using delimiter linefeed

Unfortunately, this too, for some reason, seemed not to work. I would really appreciate it if someone could point me in the right direction of how to solve this problem of avoiding the username in the HFS file path.

Thank you in advance for your help,

Tom


Solution

  • Use the path to (folder) command, e.g.:

    set paragraph_ores to read file ((path to desktop as string) & "ORES.txt")
    

    Have a look at path to (folder) in the AppleScript Language Guide, which returns the location of the specified special folder.