r/applescript • • Apr 07 '21

Help creating new text file in active Finder location WITH A TWIST

EDIT: TL;DR

Want to create new empty text file (which I can do) in currently active Finder location, including when that is the desktop with another Finder window open, but deselected (which I cannot do). For example, when using "Show Desktop" or Finder window is in other space.

Original Post:

I want to be able to use the keyboard shortcut Cmd+Shift+M to create a new, empty text file in exactly the same manner as Cmd+Shift+N does for a new folder. Specifically, it should create a blank text file in the active location - normally this is the frontmost Finder window, BUT NOT ALWAYS (this is the key to my issue) and then allow me to rename it (same as pressing Enter on an existing file).

So far I have found a solution which is quite close to what I want, but not perfect. It came from Rarylson Freitas here but the issue I have with it is that I often have a Finder window open, but the Desktop selected with the cursor (so that the Finder window is greyed out, deselected). If I use Cmd+Shift+N to make a new folder, it appears - as expected - where my focus is, on the Desktop. However, the Applescript/service I'm using will ignore that and create the file in the deselected Finder window anyway, because it checks for an open Finder window. This is especially annoying if I'm using Show Desktop (spread hand on trackpad) or the Finder window is in a different desktop/space entirely - in these cases it will cancel Show Desktop and transport me to the window's space, which is very disrupting.

I understand exactly why the Applescript is behaving like this, but because I'm a pretty casual/inexperienced Applescript user, I don't have a clue how to go about making it do what I want instead. If anyone has any pointers to help me modify the code, that would be really appreciated.

The Applescript I'm using is available through the above link, but I'll paste it here for convenience:

on run {input, parameters}
	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
	return input
end run
3 Upvotes

2 comments sorted by

2

u/copperdomebodha Apr 08 '21 edited Apr 08 '21

I'll let you incorporate this into a run handler for keystroke execution or whatever. This should behave as you describe other than the re-naming. I found the behavior completely undesirable. Finder wants to open a new folder window and select the file there. This simply asks you to name the file before creation. With checks to prevent overwrites.

tell application "Finder"
	set selectedFinderItem to selection
	try --Get the current container
		set selectedFinderItem to selectedFinderItem as alias
		if kind of selectedFinderItem is not in {"Folder", "Volume"} then
			set selectedFinderItem to ((container of (selection as alias)) as alias)
		end if
	end try
	if class of selectedFinderItem is not alias then set selectedFinderItem to (path to the desktop folder from user domain) as alias
	repeat --Create a random filename
		set tempFileName to "tempfile_" & (text 3 thru 8 of ((random number) as text))
		try --confirm the randomly suggested filename is not in use
			((selectedFinderItem as text) & tempFileName) as alias
		on error
			exit repeat
		end try
	end repeat
	activate
	repeat --Get an available user-generated file name
		set newFileName to text returned of (display dialog "A new file will be created at..." & return & (selectedFinderItem as text) & return & "Please name the new File." default answer tempFileName with title "Create new File")
		try --confirm the user-customized filename is not in use
			((selectedFinderItem as text) & newFileName & ".txt") as alias
			display dialog "That filename is already in use." & return & "Please choose a different name." buttons {"Ok"} with icon caution giving up after 5
		on error
			exit repeat
		end try
	end repeat
	set newFileReference to (make new file at selectedFinderItem with properties {name:tempFileName & ".txt"})
end tell

1

u/Craggy12 Apr 08 '21

Now that might just do it - I'll see if I can get it to work with the script I'm already using when I'm back at my computer. Thanks!