AppleScript: How to create a file that contains a forward slash in its filename? 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 &amp; theCustomFilename &amp; 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 &amp; " number " &amp; 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
You should never use the forward slash (/) in your filenames.

As an experiment, I opened TextEdit and saved a file with a forward slash in the name



Listing out the directory comments we get:

$ ls
total 8
drwxr-xr-x   3 areed  staff   102B May  9 02:02 .
drwx------+ 29 areed  staff   986B May  9 02:05 ..
-rw-r--r--@  1 areed  staff   325B May  9 02:00 Test:this.rtf


It changes the forward slash out to a colon.

An interesting thing happens when you use bash's autocomplete function; hit tab more than once and instead of "stopping" when it has found the full match, it keeps going:

$ cat Test\:this.rtf Test\:this.rtf Test\:this.rtf Test\:this.rtf Test\:this.rtf Test\:this.rtf


In the above example, I typed cat Tes and then pressed Tab 5 times.  Instead of beeping and stopping, it kept repeating as if stuck in a loop; this is not correct behavior.

Doing a search on this behavior, I stumbled across this article from C|Net:  Do not use "forward slash" in Mac OS X filenames, Distiller 6.0 example.  The article is quoted as saying:


  The forward slash should also never be used in the name of a drive or
  partition, or virtually in any other name field in Mac OS X. The
  reason behind this limitation stems from Mac OS X's UNIX underpinnings
  
  In prior versions of Mac OS the location of a file was noted using a
  path, with the colon ( :) separating objects. Mac OS X uses the
  forward slash ( /) in place of the colon.


It seems that this behavior/limitation is still present in macOS.  Bottom line, don't use the forward slash in your filename.