Bizarre file path failure

I have a lengthy path to a file that I’m constructing, and need to check to see if it’s a folder or a file. It’s failing, claiming the file couldn’t be found. I assumed there was a problem with the construction of the path, but after banging my head on the wall for a while, I ran a test that absolutely baffles me!

The following code works perfectly:

tell application “System Events” to set extensionKind to kind of (info for POSIX file “/Users/thomas/Library/Application Support/Mozilla/Extensions/{ec8030f7-c20a-464f-9b0e-13a3a9e97384}/searchme@mybrowserbar.com.xpi”)

This returns, as I would expect, a result of “Unix Executable File”.

However, if I use the following code, I get a -43 error back from System Events:

set wholePath to “/Users/thomas/Library/Application Support/Mozilla/Extensions/{ec8030f7-c20a-464f-9b0e-13a3a9e97384}/searchme@mybrowserbar.com.xpi”
tell application “System Events” to set extensionKind to kind of (info for POSIX file wholePath)

The paths there are exactly the same… the only difference is that in the latter case, it’s stored in a string variable rather than being hard-coded, and for some reason this causes it to fail.

What’s going on here???

Hi,

dealing with file URLs there are some cases where AppleScript behaves different specifying a variable or a literal string.
I recommend not to use POSIX file at all, info for isn’t needed either when talking to System Events.

The predefined relative path


path to application support folder from user domain

refers always to the Application Support folder of the current user.
It’s easier to work with HFS paths (colon separated)


set filePath to (path to application support folder from user domain as text) & "Mozilla:Extensions:{ec8030f7-c20a-464f-9b0e-13a3a9e97384}:searchme@mybrowserbar.com.xpi"
tell application "System Events" to set extensionKind to kind of file filePath

Both codes works on my machine but there is one other thing that is wrong with that code. I’m not sure if the file not exists error has anything to do with it but using the deprecated info for command which is part of the standard scripting addition shouldn’t be used in another application’s context.

So you should use:

set extensionKind to kind of (info for (POSIX file "/Users/thomas/Library/Application Support/Mozilla/Extensions/{ec8030f7-c20a-464f-9b0e-13a3a9e97384}/searchme@mybrowserbar.com.xpi"))

What are those cases, and why does it behave differently?

I’ve actually solved my problem, using the following code:

tell application "System Events" to set extensionKind to class of item (inFolder & oneExtension) as string

(The path is constructed by combining the parent folder path, generated elsewhere, and the name of an item in that folder. The “wholePath” variable used in my previous example was just a simplification of that used in my tests.)

This works perfectly, so my issue really isn’t one of needing to figure out how to tell if something is a folder or a file, but a desire to understand why the behavior was different with a string literal than with a variable containing exactly the same string. I don’t want to spend another hour debugging a script over this sort of thing again in the future! :o

Hello.

As DJ Bazzie Wazzie stated in his post;the info for command is deprecated. It has been deprectated since OS X 10.6 at least so, the first step is to avoid using it, and using its recommended replacement in the standard additions dictionary. LIke you have done in your last attempt.

I am not trying to be overbearing here. It is just best to follow the recommendations of the dictionary, with regards to deprecated commands. IMHO we should all do that, even if it still works, because deprecated commands usually will break at least in some ways, in the future.

I fully agree with McUsrII about the use of deprecated features like info for.

This said, the curious guy made a test with this short code :

set wholePath to "/Library/Script Libraries/changeCase Lib.scptd/"
set extensionKind to kind of (info for POSIX file wholePath)

It behaved flawlessly.
Are you sure that speaking to System Events was really needed ?

From my point of view, it’s not and worse, it’s the wrongdoer.
If I run :

set wholePath to "/Library/Script Libraries/changeCase Lib.scptd/"

tell application "System Events" to set extensionKind to kind of (info for POSIX file wholePath)

I get :
error “Erreur dans System Events : Le fichier POSIX file /Library/Script Libraries/changeCase Lib.scptd/ est introuvable.” number -43 from file “SSD 500:Library:Script Libraries:changeCase Lib.scptd:”

In fact, to be honest, I already encountered this kind of behavior.
An alternate way would be :

set wholePath to "/Library/Script Libraries/changeCase Lib.scptd/"

tell application "System Events" to tell me to set extensionKind to kind of (info for POSIX file wholePath)

My understanding is that we are facing the supposed to be well known problem of OSAX function triggered in a tell application block.

Yvan KOENIG (VALLAURIS, France) jeudi 21 août 2014 22:46:18