How to create a README file in any folder in one step?

Discussion in 'Mac Basics and Help' started by hajime, Sep 1, 2011.

  1. hajime macrumors 68030

    Joined:
    Jul 23, 2007
    #1
    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
     
  2. vmachiel macrumors 68000

    Joined:
    Feb 15, 2011
    Location:
    Holland
  3. aarond12 macrumors 65816

    aarond12

    Joined:
    May 20, 2002
    Location:
    Dallas, TX USA
    #3
    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.
     
  4. Jolly Jimmy macrumors 65816

    Jolly Jimmy

    Joined:
    Dec 13, 2007
    #4
    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.
     
  5. miles01110 macrumors Core

    miles01110

    Joined:
    Jul 24, 2006
    Location:
    The Ivory Tower (I'm not coming down)
    #5
    You could use the touch command in Terminal to create the file, but you'd still have to know the correct path.
     
  6. hajime thread starter macrumors 68030

    Joined:
    Jul 23, 2007
    #6
    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.
     
  7. Jolly Jimmy macrumors 65816

    Jolly Jimmy

    Joined:
    Dec 13, 2007
    #7
    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
     
  8. hajime thread starter macrumors 68030

    Joined:
    Jul 23, 2007
    #8

    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?
     
  9. Jolly Jimmy macrumors 65816

    Jolly Jimmy

    Joined:
    Dec 13, 2007
    #9
    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.
     

Share This Page