I have a Service that creates a new .txt or .rtf file. The Service saves this file to disk. The filename of this file is supplied by the user via an entry dialog. But, I have found that if the filename contains a forward slash (/
), then the file will not be created.
Let's suppose that I would like to create a file entitled: This is my / file.txt.
I can create this file manually in Finder.app without error, which indicates that the OS allows forward slashes in filenames (unlike the colon, for example).
Here is some code (i.e., a snippet from my Service) to reproduce the error:
set theCustomFilename to text returned of (display dialog "Enter filename:
" default answer "This is my /file")
set thePath to "/Users/Me/Desktop/"
set theFileExtension to ".txt"
set theCustomFilePathname to thePath & theCustomFilename & theFileExtension
try
set fileHandle to open for access theCustomFilePathname with write permission
write "This is the document text." to fileHandle
close access fileHandle
on error eStr number eNum
display dialog eStr & " number " & eNum buttons {"OK"} default button 1 with title "File I/O Error..." with icon caution
try
close access fileHandle
end try
end try
If you run the above code, you will be given the error:
File /Users/Me/Desktop/This is my / file.txt wasn’t found. number -43
/
intheCustomFilePathname
are directory separators and which are part of a folder or file name?theCustomFilePathname
once it's assigned, there you still have the same problem :-)theCustomFilename
, and then escape each instance that exists in this variable, prior to theset theCustomFilePathname to thePath & theCustomFilename & theFileExtension
line.Me/Desktop
part isDesktop
within a folderMe
and not something calledMe/Desktop
?