Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

hajime

macrumors G3
Original poster
Jul 23, 2007
8,132
1,393
Hello. I often create README or MEMO file in different folders. Under Windows, it is possible to create a text file by just right-clicking on a window. Can I do the same thing under Mac OS? So far, I have to open a text editor from the Dock or Application folder, type in whatever I want, then specify a directory where I want to save the file. It is a bit inconvenience. Thanks
 
You can create an Automator workflow to create a document with a default name (e.g., "README.TXT"), then immediately open it in Textedit or something like that.

To run your script, open the folder in Finder, select Services and choose your workflow. Shouldn't be too difficult. I don't have access to a Mac at the moment, so if you need specifics, I can reply later.
 
I found automator services don't work very well with this kind of thing. I use an applescript run from a keyboard shortcut instead. You could also add an applescript app to Finder's toolbar to accomplish this.
 
You could use the touch command in Terminal to create the file, but you'd still have to know the correct path.

Is it possible to use the mouse/keyboard to select an open window, get the path, and then paste it to xterm/Terminal? I can then cd to that path and use the vi command to create the document.
 
Code:
Is it possible to use the mouse/keyboard to select an open window, get the path, and then paste it to xterm/Terminal? I can then cd to that path and use the vi command to create the document.

You can do the whole thing in applescript. Try this one I wrote. It gets the current folder in Finder and saves a blank .txt file to it with the name of your choice.

Code:
set file_extension to "txt"
	
	tell application "Finder"
		set save_folder to insertion location as alias
		set save_folder_name to name of save_folder
		set new_file to choose file name with prompt "Enter file name:" default location save_folder
		set whole_file_name to ((new_file as text) & "." & file_extension)
		set new_file_POSIX to POSIX path of new_file
		get exists of whole_file_name
		
		if result is true then
			beep
			set new_file_alias to whole_file_name as alias
			set file_name to name of new_file_alias
			
			if save_folder_name = "Desktop" then
				display alert "\"" & file_name & "\" already exists. Do you want to replace it?" message "A file with the same name already exists on the Desktop. Replacing it will overwrite its current contents." buttons {"Replace", "Cancel"} as warning
			else
				display alert "\"" & file_name & "\" already exists. Do you want to replace it?" message "A file with the same name already exists in the folder " & save_folder_name & ". Replacing it will overwrite its current contents." buttons {"Replace", "Cancel"} as warning
			end if
			
			if button returned of result = "Replace" then
				do shell script "rm -rf '" & new_file_POSIX & "." & file_extension & "'"
				delay 0.2
				do shell script "touch '" & new_file_POSIX & "." & file_extension & "'"
				delay 0.2
				set extension hidden of new_file_alias to true
				delay 0.1
				select file whole_file_name
				open file whole_file_name
			end if
		else
			do shell script "touch '" & new_file_POSIX & "." & file_extension & "'"
			
			set new_file_alias to whole_file_name as alias
			set file_name to name of new_file_alias
			
			delay 0.2
			set extension hidden of new_file_alias to true
			delay 0.1
			select file whole_file_name
			open file whole_file_name
		end if
	end tell
 
Code:

You can do the whole thing in applescript. Try this one I wrote. It gets the current folder in Finder and saves a blank .txt file to it with the name of your choice.

Code:
set file_extension to "txt"
	
	tell application "Finder"
		set save_folder to insertion location as alias
		set save_folder_name to name of save_folder
		set new_file to choose file name with prompt "Enter file name:" default location save_folder
		set whole_file_name to ((new_file as text) & "." & file_extension)
		set new_file_POSIX to POSIX path of new_file
		get exists of whole_file_name
		
		if result is true then
			beep
			set new_file_alias to whole_file_name as alias
			set file_name to name of new_file_alias
			
			if save_folder_name = "Desktop" then
				display alert "\"" & file_name & "\" already exists. Do you want to replace it?" message "A file with the same name already exists on the Desktop. Replacing it will overwrite its current contents." buttons {"Replace", "Cancel"} as warning
			else
				display alert "\"" & file_name & "\" already exists. Do you want to replace it?" message "A file with the same name already exists in the folder " & save_folder_name & ". Replacing it will overwrite its current contents." buttons {"Replace", "Cancel"} as warning
			end if
			
			if button returned of result = "Replace" then
				do shell script "rm -rf '" & new_file_POSIX & "." & file_extension & "'"
				delay 0.2
				do shell script "touch '" & new_file_POSIX & "." & file_extension & "'"
				delay 0.2
				set extension hidden of new_file_alias to true
				delay 0.1
				select file whole_file_name
				open file whole_file_name
			end if
		else
			do shell script "touch '" & new_file_POSIX & "." & file_extension & "'"
			
			set new_file_alias to whole_file_name as alias
			set file_name to name of new_file_alias
			
			delay 0.2
			set extension hidden of new_file_alias to true
			delay 0.1
			select file whole_file_name
			open file whole_file_name
		end if
	end tell


Thanks. How does it get the current folder? Do I move the mouse pointer to any window to get the path? I have never played with applescript before. It looks like I have to type in the code you provided under the AppleScript Editor. Can I have it running in the background all the time and quit whenever I like?
 
Thanks. How does it get the current folder? Do I move the mouse pointer to any window to get the path? I have never played with applescript before. It looks like I have to type in the code you provided under the AppleScript Editor. Can I have it running in the background all the time and quit whenever I like?

This script doesn't run in the background, you just run it whenever you need a blank txt file. The first part of the script gets the path to the current folder.

Paste it into AppleScript Editor.app and press run to try it out. You can then save it as an application and place it in Finder's toolbar for example for easy access.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.