Bug in Lion if file contains /

Hi,

I discovered what seems a bug:

Please try to run this in Lion:

set filePath to “Macintosh HD:Users:john:Desktop:London/UK.txt” – change the path according to your HD name and user
set fileRef to open for access filePath with write permission
write “abc” to fileRef
close access fileRef

You receive bad file name
From 10.0 to 10.6.x is absolutely possible write a file whose name contains a slash char.

Seems that this is changed under Lion.
Any workaround?

Hagimeno

ist works for me on 10.7.2

Hagimeno,

I get the same result. My first reaction is that you have left out the word 'file", as in:

set fileRef to open for access file filePath with write permission

But that fails too. (Leaving out the “file” would fail under old versions of the OS, I’m pretty sure.)

It seems the problem is actually creating the file. If the file already exists, your script works. So I guess the workaround is to make the file some other way, like:

tell application "Finder" to make file at folder "Macintosh HD:Users:shane:Desktop" with properties {name:"London/UK.txt"}

Or probably faster:

do shell script ("echo '' > " & quoted form of POSIX path of "Macintosh HD:Users:shane:Desktop:London/UK.txt")

although this will add a linefeed to the file.

Not if you use echo in your /bin folder instead of the built-in echo.

do shell script ("/bin/echo -n '' > " & quoted form of POSIX path of "Macintosh HD:Users:shane:Desktop:London/UK.txt")

Thanks for all suggestions:

Or this form more “simple” that use the touch command:

set filePath to “Macintosh HD:Users:john:Desktop:London/UK.txt” – change the path according to your HD name and user
do shell script "/usr/bin/touch " & quoted form of (POSIX path of filePath)
set fileRef to open for access file filePath with write permission
write “Abc” to fileRef
close access fileRef

Once the file is created AppeScript can write on it.

For Shane: I usually put the term “file” in open for access file filePath. I forgot yesterday…

Thanks

Hagimeno

Good to know – thanks, DJ.

Just out of curiosity:

‘open for access’ also accepts POSIX paths nowadays. Does the following variation work in Lion, or is it the same problem?

set filePath to (path to desktop as text) & "London/UK.txt"
set fileRef to open for access (POSIX path of filePath) with write permission
try
	write "abc" to fileRef
end try
close access fileRef

As of 10.6, you don’t need it if you use a POSIX path. (Not that that solves the / problem.)

Wow! It only took Shane one second to answer my query! :lol:

:lol:

Hi Shane,

the following code using posix, return bad file name again.

set filePath to “Lion:Users:adminosx:Desktop:London/UK.txt” – change the path according to your HD name and user
set filePathPosix to POSIX path of filePath

set fileRef to open for access filePathPosix with write permission
write “abc” to fileRef
close access fileRef

Another solution should be to write the file to a tmp file and after rename it via shell of Finder (probably shell is faster).

Hagimeno

Hi all,

another workaround:

set filePathHFS to “Macintosh HD:Users:john:Desktop:tmp.txt” – change the path according to your HD name and user
set filePathPosix to POSIX path of filePathHFS
set parentFolderPosix to do shell script "/usr/bin/dirname " & filePathPosix
set destinationPathHFS to (POSIX file parentFolderPosix & “:London/UK.txt”) as string
set destinationPathPosix to POSIX path of destinationPathHFS
set fileRef to open for access filePathHFS with write permission
write “abc” to fileRef
close access fileRef
do shell script "/bin/mv " & quoted form of filePathPosix & " " & quoted form of destinationPathPosix

Hagimeno

As far as I know, slash isn’t a valid character in a fileName.
Maybe your problems are linked to tithe one embedded in :
London/UK.txt"

If the slash is used here as a delimiter between a subfolder name “London” and a fileName “UK.txt”

I’m not sure that using the colon and the slash in the same full pathname isn’t a good idea.

Yvan KOENIG (VALLAURIS, France) vendredi 25 janvier 2011 16:57:28

Doesn’t work either but for another reason:
As usual POSIX path turns slashes into colons

In a HFS path environment a slash is indeed a valid character in a file name.

The oddity is that it works in all pre-Lion system versions

Yes. But that’s just a convention used in POSIX paths to differentiate between a forward slash in a name and the forward-slash delimiters in the path. In Snow Leopard, the script I posted creates a file on the desktop called “London/UK.txt” ” as I would expect.

For me it’s a big bug.
In the last 15 years I used Applescript to save file whose filename contains / char.
Yes there are workaround but remain a bug.
Unfortunately for us Apple in last years works only for iOS.
How is possible that nobody at debug/quality AS team discover this?
If is a documented “feature” I can accept.

Hagimeno

We were supposed to remove the slash from filenames since the delivery of OS X because it’s an UNIX system which use the slash as delimiters.

From 10.0 to 10.6, Apple was fair enough to take care of possible embedded slash.
At last, they did in Lion what would be done for years.
Embedded slashes are causing an error.

Yvan KOENIG (VALLAURIS, France) vendredi 25 janvier 2011 18:50:20

Because most of us started to remove the slash when they stepped over from Mac OS to Mac OS X.

REMOVED: because of carbon issues and not rosetta directly, so not really an issue since carbon is not removed from Lion but the Finder has changes.

That would be OK if (a) it was announced, (b) it solved a problem (which it doesn’t really because the problem had been solved other ways), and (c) it was applied consistently. In this case, I reckon it smells more like a simple bug.

OTOH, I think they are best avoided by scripters for the pragmatic reason that they cause complications when switching between HFS and POSIX paths in terms of file names.