Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
AppleScript to create a new file in Finder (to be used in Automator)
-- AppleScript to create a new file in Finder
--
-- Use it in Automator, with the following configuration:
-- - Service receives: no input
-- - In: Finder.app
--
-- References:
-- - http://apple.stackexchange.com/a/129702
-- - http://stackoverflow.com/a/6125252/2530295
-- - http://www.russellbeattie.com/blog/fun-with-the-os-x-finder-and-applescript
--
-- Know bugs
-- - When create/delete in desktop, after some time, the file is showed again and after deleted (it works, but it's strange)
set file_name to "untitled"
set file_ext to ".txt"
set is_desktop to false
-- get folder path and if we're in desktop (no folder opened)
try
tell application "Finder"
set this_folder to (folder of the front Finder window) as alias
end tell
on error
-- no open folder windows
set this_folder to path to desktop folder as alias
set is_desktop to true
end try
-- get the new file name (do not override an already existing file)
tell application "System Events"
set file_list to get the name of every disk item of this_folder
end tell
set new_file to file_name & file_ext
set x to 1
repeat
if new_file is in file_list then
set new_file to file_name & " " & x & file_ext
set x to x + 1
else
exit repeat
end if
end repeat
-- create and select the new file
tell application "Finder"
activate
set the_file to make new file at folder this_folder with properties {name:new_file}
if is_desktop is false then
reveal the_file
else
select window of desktop
set selection to the_file
delay 0.1
end if
end tell
-- press enter (rename)
tell application "System Events"
tell process "Finder"
keystroke return
end tell
end tell
@rarylson
Copy link
Author

rarylson commented Apr 30, 2015

Install

The idea behind this AppleScript is to create an Automator workflow and assigning a shortcut to it using the following steps:

  • Open Automator and create a Service;
  • Set the input to no input, and the application to Finder.app;
  • Drag and Drop the Run AppleScript workflow element onto the grey space;
  • Put the contents of this AppleScript in the textbox;
  • Save the workflow with a reasonable name (like New File);
  • Go to Settings -> Keyboard -> Shortcuts -> Services and assign a shortcut to it.

@davidvb
Copy link

davidvb commented Mar 5, 2018

For Alfred There’s this nice workflow to do the same:
http://ianisted.co.uk/new-finder-file-alfred-2

It’s using ruby though, but its code is tiny and super fast:


_osa1 = 'tell application "Finder" to set insertionLocation to insertion location as alias'
_osa2 = 'return POSIX path of insertionLocation'
activeFinderPath = (osascript -e "#{_osa1}" -e "#{_osa2}").strip

filename = 'untitled'
extension = 'txt'

if '{query}' == 'help' || '{query}' == '?'
system("open", "http://ianisted.co.uk/new-finder-file-alfred-2")
exit
else
if '{query}' && '{query}'.include?(?.)
filename = '{query}'.split('.')[0]
extension = '{query}'.split('.')[1]
end

file = [filename, extension].join('.')
path = [activeFinderPath, file].join

template = [extension, extension].join('.')
if (File.exists?("templates/#{template}"))
  system(%[cp "templates/#{template}" "#{path}"])
else
  system(%[touch "#{path}"])
end

end

@JDTm
Copy link

JDTm commented Jun 11, 2020

When you need an input field instead of renaming afterwards (this way, you don't need the Accessibility security right):
replaces line 14:

set file_name to text returned of (display dialog "" default answer "untitled")
if file_name is "" then
	set file_name to "untitled"
end if

and delete rows 59++

@maxim-uvarov
Copy link

maxim-uvarov commented Nov 2, 2020

Open Automator and create a Service;

There is no "Service" in Catalina, but there is a "Quick Action"

@maxim-uvarov
Copy link

maxim-uvarov commented Nov 2, 2020

The version to open file in default text editor.

-- AppleScript to create a new file in Finder
--
-- Use it in Automator, with the following configuration:
-- - Service receives: no input
-- - In: Finder.app
--
-- References:
--  - http://apple.stackexchange.com/a/129702
--  - http://stackoverflow.com/a/6125252/2530295
--  - http://www.russellbeattie.com/blog/fun-with-the-os-x-finder-and-applescript
--
-- Know bugs
-- - When create/delete in desktop, after some time, the file is showed again and after deleted (it works, but it's strange)

set file_name to "untitled"
set file_ext to ".txt"
set is_desktop to false

-- get folder path and if we're in desktop (no folder opened)
try
    tell application "Finder"
        set this_folder to (folder of the front Finder window) as alias
    end tell
on error
    -- no open folder windows
    set this_folder to path to desktop folder as alias
    set is_desktop to true
end try

-- get the new file name (do not override an already existing file)
tell application "System Events"
    set file_list to get the name of every disk item of this_folder
end tell
set new_file to file_name & file_ext
set x to 1
repeat
    if new_file is in file_list then
        set new_file to file_name & " " & x & file_ext
        set x to x + 1
    else
        exit repeat
    end if
end repeat

-- create and select the new file
tell application "Finder"
    
    activate
    set the_file to make new file at folder this_folder with properties {name:new_file}
    if is_desktop is false then
        reveal the_file
		delay 0.1
        open the_file
    else
        select window of desktop
        set selection to the_file
        delay 0.1
    end if
end tell

@RblSb
Copy link

RblSb commented Jul 7, 2021

Add delay 0.3 line after reveal the_file to fix rename focus for newest macOS. You also need to allow Finder in security setings for Accessibility tab.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment