zeldor.biz

Linux, programming and more

Copyright © 2025
Log in

MAC OS X Finder New File

December 26, 2017 by Igor Drobot 6 Comments

For some of my typical work flows I’m missing the base feature in MAC OS X to create a simple “New File” just like in other operating systems, like right click and “Create new Text File”.
For a quick note or just to start to create a new script/app.

Here comes the solution :-)

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
set file_name to "untitled"
set file_ext to ".txt"
set is_desktop to false
 
-- get folder path and if we are 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

set file_name to "untitled" set file_ext to ".txt" set is_desktop to false -- get folder path and if we are 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

Filed Under: Linux Tagged With: MAC OS X

Categories

Archives

Tags

apache2 Apple arduino ARM Automation backup bash Cisco Cluster Corosync Database Debian Debian squeeze DIY DNS Fedora FTP Fun Icinga Ipv6 KVM Linux LVM MAC OS X Monitoring MySQL Nagios Nginx openSUSE OpenVPN PHP Proxy Python python3 qemu RAID rsync Samba security ssh Ubuntu virtualization Windows Windows 7 Wordpress

Comments

  1. Dave E. says

    April 28, 2021 at 16:06

    The workflow runs fine, but fails to appear in the “services” menu, although it’s in ~/Library/Services/

    Worse: the assigned shortcut returns “The action “Run AppleScript” encountered an error: “Not authorised to send Apple events to System Events.””—this is on Mojave?

  2. joe says

    October 14, 2018 at 06:14

    Thanks! I think this will do the job.
    on run {input, parameters}

    — get folder path and if we are 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
    tell application “Finder”
    set selection to make new file at this_folder
    end tell

    return input
    end run

  3. RC says

    May 21, 2018 at 16:28

    Thank you very much. It worked for me. I use it through the menu (Finder>Services).

  4. fs says

    May 7, 2018 at 19:00

    hi

    when I run the shortcut in a folder in Finder, it merely open another Finder tab.

    not working. I did not get my next text file created.

    what is so simple in Windows is so damn difficult in Mac and yet I keep hearing my friends say how great Mac is.

    I really wonder.

  5. Yang says

    March 14, 2018 at 09:02

    Thanks! I add a “delay 0.4” before the return keystroke, otherwise it often clicks the wrong file rather the new created file. And it seems more convenient if it is exported as an app and dragged to the Finder toolbar :-)

Trackbacks

  1. Create new text file from Finder toolbar - herrkaefer says:
    September 25, 2020 at 21:45

    […] The script is borrowed from this post. I did a little modification by adding a delay line to avoid wrong […]

Leave a Reply

Your email address will not be published. Required fields are marked *

Yeaaah Cookie! We use cookies to ensure that we give you the best experience on our website. If you continue to use this site we will assume that you are happy with it.Ok