Path-to-me bug, or is it my error?

I have these lines of (probably incompetent) code in an application:

set vmPath to (path to me) & "Contents:Resources:Files:MacOS.sheepvm" as string
set vmFile to vmPath as alias
set vmPosix to (POSIX path of vmFile) as string
set extfsPath to (vmPosix & "extfspath")

They cause no problem when I run my application, but if I try to run it SD, I get this error:

  Stopped:(Error: File Macintosh HD:Users:edward:Desktop:Mac OS wasn't found.)

Notice the two colons between “app” and “Contents”. I’m assuming that’s the problem.

EDIT: I now see that I can avoid this by replacing all that code with this one line:

set extfsPath to (POSIX path of (path to me)) & "Contents/Resources/Files/MacOS.sheepvm/extfspath"

But perhaps this really is a bug in SD?

What this line of code does is gets path to me, which returns an alias, and therefore the & operator concatenates it with the following string, producing {alias "Macintosh HD:Users:edward:Desktop:Mac OS", "Contents:Resources:Files:MacOS.sheepvm"}, which you then coerce to a string with as string, resulting in the alias being coerced to a HFS path, and then joined using whatever your current value for text item delimiters is.

What you probably meant was:

set vmPath to ((path to me) as text) & "Contents:Resources:Files:MacOS.sheepvm"

This no longer relies on the value of text item delimiters, and doesn’t create an otherwise pointless list.

But you should really be using path to resource — it’s designed for getting stuff in the Resources folder. So something like:

set vmPath to path to resource "MacOS.sheepvm" in directory "Files"

Thank you, Shane. I should have known these things, and that part of my code dates back to the time when I had no idea at all of what I was doing. “path to resource” is something I also should have known about. Thank you again!

Just to nit-pick that leaving out the inner parentheses is slightly more efficient:

set vmPath to (path to me as text) & "Contents:Resources:Files:MacOS.sheepvm"

In this form, the as text is a parameter of path to which causes it to return the text directly instead of returning an alias which is then coerced to text.

1 Like

Well picked! :slight_smile: