-
-
Save rarylson/5d20fc96335851365a02 to your computer and use it in GitHub Desktop.
| -- 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 |
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
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.
I don't think lines 30-43 and with properties {name:new_file} on line 49 are necessary (at least as of macOS Sequoia) to prevent overwriting existing files. If another file is made the filename automatically becomes "untitled 2", "untitled 3", "untitled 4", and so on.
If you want maxim-uvarov's script to have the opposite behavior for desktop vs non-desktop files (a file opened in desktop is immediately opened without being shown in the folder, while a file opened in a non-desktop folder is shown in the folder selected for renaming), then replace
-- 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
with
-- 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 then
delay 0.1
open the_file
else
reveal the_file
delay 0.3
set selection to the_file
end if
end tell
(removing with properties {name:new_file} if you delete lines 30-43.)
There is no "Service" in Catalina, but there is a "Quick Action"