Apple - Right-click for Creating a New Document - How?

Here is an AppleScript I use as an AppleScript application so I can assign it the TextEdit icon and place it in the Toolbar of Finder. Then it's available to create a new text document at whatever location Finder is set to, that one has write permissions to. In other words, if one tries to create a new text document at a location that is only write accessible to e.g. root or any location one does not have specific write permissions to, then one is notified upon trying to create the new text document.

The code checks to see if a file of the same name already exists and if so notifies and brings back the Save As: dialog box.

Open (Apple)Script Editor and copy and paste the code below into the empty window and save it as Create New Text File Here.app. Make sure you select Application for File Format: in the Save As: dialog box.

Before adding it to the Toolbar of Finder, you'll want to change it's icon to that of which is used by TextEdit. You can open the Get Info sheet for each app and then copy and paste the one from TextEdit.app to the one in Create New Text File Here.app. Note that this is the icon displayed in the top left corner of the Get Info sheet.

Now that it has a nicer icon, drag and drop the new app to the location on the Toolbar of Finder you'd like for it to be. Note that depending on which version of OS X / macOS you may need to press the Command ⌘ key while dragging the app to the Toolbar of Finder.

To use, just click the Create New Text File Here.app icon in the Toolbar of Finder and you'll be prompted for a name and the a new text document file will be created and opened from the current location of the Finder window.


on run
    my createNewTextFile()
end run


on createNewTextFile()

    tell application "Finder"
        activate
        set the currentFolder to (folder of the front window as alias)
    end tell

    tell me
        activate
        set fileName to ""
        repeat while fileName = ""
            display dialog "Save As:" with title "Create New Text File Here" default answer fileName buttons {"Cancel", "OK"} default button 2
            set fileName to text returned of the result
        end repeat
        if fileName ends with ".txt" then
            set newTextFile to POSIX path of currentFolder & fileName
        else
            set newTextFile to POSIX path of currentFolder & fileName & ".txt"
        end if

    end tell

    tell application "Finder"
        set itExists to (exists newTextFile as POSIX file)
    end tell

    tell me
        activate
        if itExists is false then
            try
                do shell script "touch \"" & newTextFile & "\"; open \"" & newTextFile & "\""
            on error
                display dialog "Cannot create the \"" & newTextFile & "\" file at this location!..." with title "Cannot Create File" buttons {"OK"} default button 1 with icon stop
            end try
        else
            display dialog "The \"" & newTextFile & "\" file already exists!..." with title "File Already Exists" buttons {"OK"} default button 1 giving up after 5
            my createNewTextFile()
        end if
    end tell

end createNewTextFile

Note that this code could also be used in Automator for a Service workflow, however I've found that I'm already in Finder when I want to create a new text document and it's just easier to click the Create New Text File Here.app icon in the Toolbar of Finder then to right-click and choose from the already over populated Services context menu that I have.

Additionally, if you modify the code once the script is saved as an application and placed in the Toolbar of Finder, then you'll have to remove the icon from the Toolbar and then drag and drop the newly saved Create New Text File Here.app in order for it to work properly.