0

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

7
  • How is AppleScript supposed to know which of the / in theCustomFilePathname are directory separators and which are part of a folder or file name?
    – nohillside
    May 9, 2017 at 5:37
  • You are correct. I had oversimplified my post. In my real code, the filename is contained in its own variable. Please see my edit. May 9, 2017 at 6:11
  • This doesn't change the value of theCustomFilePathname once it's assigned, there you still have the same problem :-)
    – nohillside
    May 9, 2017 at 8:12
  • I don't understand your comment. The script could find all instances of a forward slash in the theCustomFilename, and then escape each instance that exists in this variable, prior to the set theCustomFilePathname to thePath & theCustomFilename & theFileExtension line. May 9, 2017 at 8:21
  • These are all just strings, how should AppleScript know that it is a path or file name? And how should AppleScript know whether the Me/Desktop part is Desktop within a folder Me and not something called Me/Desktop?
    – nohillside
    May 9, 2017 at 8:44

3 Answers 3

4

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

enter image description here

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.

3
  • If "You should never [use] the forward slash (/) in your filenames," then why does Apple allow the use of the forward slash, when naming or renaming a file in Finder? Apple could have easily prohibited this character in filenames if they wanted to, in the way that they have done so with the colon. May 9, 2017 at 6:46
  • 3
    I can't answer why Apple does/doesn't do something - only Apple can do that. All I can tell you is that doing so causes the OS to exhibit strange behavior when trying to work with the files. The / is to delineate paths and from what I have observed, putting them in the name confuses things.
    – Allan
    May 9, 2017 at 6:52
  • 2
    @rubik'ssphere You are not prohibited from using a forward slash because the Finder maps it to : automatically for you (see the Terminal output at the beginning of the answer)
    – nohillside
    May 9, 2017 at 8:15
3

The confusion you are having is because the Finder allows you to use / in a file name by mapping the / to : under the hood. In the file system itself you are not allowed to have a file name contains a / character but you are allowed a : character.

This has come about because the original MacOS used the : as the path separator while OS X, like all Unix based operating systems, used / so the Finder was written to map one to the other.

In your AppleScript code you are using Unix paths so the code fails. Instead use a : character in your code and you will see a / in the filename in the Finder.

1
  • "Instead use a : character in your code and you will see a / in the filename in the Finder." I just tried to do this with my AppleScript code, but I get the same error. May 9, 2017 at 6:49
0

There exists a solution.

I've figured out a method that allows a forward-slash character in a filename. My solution is inspired by the code provided in this answer, written by user3439894.

Using the code that I provided in my original post, change this line:

  • set thePath to "/Users/Me/Desktop/"

to:

  • set thePath to POSIX file "/Users/Me/Desktop/"

And, change this line:

  • set theCustomFilePathname to thePath & theCustomFilename & theFileExtension

to:

  • set theCustomFilePathname to (thePath & theCustomFilename & theFileExtension) as string

You are now able to successfully include a forward-slash character in the filename, without error, just as you are free to do in Finder.app (or from the Save As menu in TextEdit.app).


Less controversial solution:

If you are worried about the admonitions offered in the other two answers to this question, regarding the evils of the forward slash, I have devised a fully "aboveboard" workaround. (FWIW, I have never seen a forward-slash character in one of my filenames cause an issue before.)

One can simply replace the standard forward-slash symbol in the submitted string with an alternative forward-slash symbol. The alternative symbol looks close enough to the real symbol for the desired punctuational effect of a forward slash to be maintained.

This solution is "less controversial" because OS X does not bar this obscure alternative symbol as a filename character.

Here is the AppleScript code:

if theCustomFilename contains "/" then
    set theCustomFilename to replace_chars(theCustomFilename, "/", "∕")
end if

The above code uses a replace_chars sub-routine. This sub-routine replaces every instance of a defined sub-string in a string, with a new string:

on replace_chars(this_text, search_string, replacement_string)
    set AppleScript's text item delimiters to the search_string
    set the item_list to every text item of this_text
    set AppleScript's text item delimiters to the replacement_string
    set this_text to the item_list as string
    set AppleScript's text item delimiters to ""
    return this_text
end replace_chars
1
  • 2
    I strongly disagree with the advice to replace "/" with the Unicode "DIVISION SLASH" symbol (or with other similar characters. They look the same to human eyes but are completely different characters. Yes, that is why you suggested it. But that is extremely misleading, and will create a lot of confusion for anyone other than you (and probably even for you in the future).
    – mivk
    Sep 22, 2019 at 15:01

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .