7

I am comfortable with creating new documents from command line on OS X (in my case El Capitan), but I wanted to explore the possibility to have Windows-like option to create a new document on right click. I have found the below script and I know that it needs running from Automator > Workflow > Run AppleScript:

set doc_list to {"Add new..."}
tell application "Finder"
    if not (exists folder "NewHere" of folder (path to application support from user domain)) then
        display dialog "This is the first time you've run NewHere." & return & return & "Files added to the list are located at" & return & "~/Library/Application Support/NewHere" & return & "Removing a file from this folder removes it from the list."
        make new folder at folder (path to application support from user domain) with properties {name:"NewHere"}
    else
        try
            set doc_list to doc_list & name of every file of folder "NewHere" of folder (path to application support from user domain) as list
        end try
    end if
    set my_file to choose from list doc_list with prompt "Choose a document to place here"
    if result is not false then
        set my_file to item 1 of my_file as string
        if my_file is "Add new..." then
            set new_file to choose file
            duplicate file new_file to folder "NewHere" of folder (path to application support from user domain) with replacing
            set my_name to text returned of (display dialog "Enter a name for the new file." default answer (name of new_file as text))
            my do_it((name of new_file as text), my_name)
        else
            set my_name to text returned of (display dialog "Enter a name for the new file." default answer my_file)
            my do_it(my_file, my_name)
        end if
    end if
end tell

on do_it(my_file, my_name)
    tell application "Finder" to set my_dest to (folder of the front window) as text
    set my_dest to my_dest & my_name as text
    set my_origin to (path to application support from user domain) & "NewHere:" & my_file as text
    do shell script "cp " & quoted form of (POSIX path of my_origin) & " " & quoted form of (POSIX path of my_dest)
    tell application "Finder" to open my_dest as alias
end do_it

It prompts me with a windows saying, "Choose a document to place here". What am I supposed to do to have the script working?

6

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.

  • Note: drag and drop in finder works by holding down the Command key while dragging the .app file. At least in MacOS 10.12 – Jorj Oct 18 '17 at 10:46
1

I have a script I run in the Finder with a keyboard shortcut from FastScripts (N) that pops-up a pick-list of file templates (stored in a template folder). To add new templates just place them in that folder.

Posted on StackExchange a few years ago: How to create a text file in a folder

enter image description here

-ccs

0

I've been using New File Menu from the Mac App Store to create new files via right-click, & it works just fine. It comes with 30 or so templates, but you can also create your own. Seems like it’s a lot easier solution.

You can find info about it at https://langui.net/new-file-menu/ & at https://itunes.apple.com/us/app/new-file-menu/id1064959555?mt=12.

You must log in to answer this question.

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