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?
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")
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…
‘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
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).
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
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.
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
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.