Home > Enterprise >  How to create new file in Finder and open it?
How to create new file in Finder and open it?

Time:10-20

For some reason Finder on Mac doesn't come with create new file functionality. The fastest way I've found to create a new file is to drag the finder path into Terminal and create a file there... which is super annoying

I wrote an apple script to automatically create a new file and bound it to a shortcut. It works perfectly, except that I can't figure out how to open the new file from within the applescript. I'm pretty sure the error stems from POSIX / UNIX paths but couldn't find a way to get this to work even when I put POSIX next to file, currentDir etc.

Here's my script:

set file_name to display dialog "File Name" default answer "untitled" with icon note buttons {"Cancel", "Continue"} default button "Continue"
set file_name to (text returned of file_name)

tell application "Finder"
    set theWin to window 1
    set currentDir to POSIX path of (target of theWin as alias)
    
    make new file at (the target of the front window) as alias with properties {name:file_name}
    set currentPath to (currentDir & file_name) as string
    open file currentPath
end tell

The script creates the file, then errors out saying it can't find the file to open it.

CodePudding user response:

If you plan on doing file activities with the Finder it is best to avoid using posix paths. You have a couple of redundancies in your code so I streamlined it a bit.

set file_name to display dialog "File Name" default answer "untitled" with icon note buttons {"Cancel", "Continue"} default button "Continue"
set file_name to (text returned of file_name)

tell application "Finder"
    set theWin to window 1
    set currentDir to (target of theWin as text)
    --> "MacHD:Users:username:Hours of Operation:"

    make new file at theWin with properties {name:file_name}
    set currentPath to currentDir & file_name
    --> "MacHD:Users:username:Hours of Operation:untitled.txt"
    open file currentPath
end tell

Try putting this after the above (and commenting out the above open file… line).

tell application "System Events"
    activate application "TextEdit"
    open file currentPath of application "TextEdit" 
end tell
currentPath

Also, after trying the above, put the variable name currentPath as the last line of the script (as I added above) and run from Script Editor. Its value should appear in the 'Result'. You can replace the drive/username components but I want to see the exact layout of the string.

A couple of things to consider:

Some files can't be opened this way, for example, a jpeg. Preview will balk at opening an empty image, presumably because that's not how images are made. Complex file formats (e.g. pages) will also fail. But with a plain text file, there shouldn't be any issues. As such, I'd consider adding '.txt' to your default filename.

CodePudding user response:

I cleaned up the code a bit and got rid of unnecessary lines.

As long as I added the file extension ".txt" , making the default answer "untitled.txt", this following AppleScript code opens the new created file "untitled.txt" in TextEdit.app... Without throwing any errors.

However, if I created file "untitled" with no file extension, running the script opened the new created file "untitled" in a new Script Editor window.

set fileName to text returned of (display dialog "File Name" default answer ¬
    "untitled.txt" with icon note buttons {"Cancel", "Continue"} default button "Continue")

tell application "Finder"
    set newFile to (make new file at Finder window 1 ¬
        with properties {name:fileName}) as alias
    delay 1
    open newFile
end tell

Note: Since using both variable naming styles underscore_naming and camelCaseNaming, within the same code is considered bad practice... I edited the code to use only the camelCaseNaming variable naming style.

  • Related