Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!


Click here to return to the '10.6: Create a new file anywhere via an Automator Service' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
10.6: Create a new file anywhere via an Automator Service
Authored by: xan666 on Aug 06, '10 08:24:25AM

I question the script you've put there.. it seems to function equally well without it.

tested on OS-X 10.6.4 this only allows new files to be created when right-clicking on a folder, not empty space as desired.

adding the prompt for filename/type is useless as well as it forces you to specify a full path, which takes longer then the whole rest of the process if the file is to be put within a deep folder - your better off just making untitled.txt and renaming it after.

I use just the Set Value of Variable, and New Text File actions
receives from "Files or Folders" in "Any Application"
the variable "Variable", and "Where" share "Path 1"

the result is a "Name of Workflow" option when you right click a folder, this method allows the new file to be placed INSIDE the folder, with the predetermined name. (you can specify this, but as I said above, your entering the ENTIRE path as punishment)..

I only mention this because I found this link looking to improve my current solution
thinking this would help me right-click anywhere

Also note: this script doesn't allow for multiple files to be created without intermediate name changes. says "the action 'whatever you called it' encountered a error" it would seem this service it meant to be written with 2 scripts, one to allow the service anywhere, and one to check if the name is taken, and if so, make the new name Untitled(i+1).txt (or other similar solution)

thanks anyways,

test system:
OS 10.6.4, Darwin 10.4.0
Automator v2.1(247)
MC737LL/a (core i7 MBP 15)



[ Reply to This | # ]
10.6: Create a new file anywhere via an Automator Service
Authored by: mihassan on Aug 10, '10 03:36:42AM
Hi, I am very new to AppleScript, the original script in this thread is one of the first few that I tried. I have extended the code to check if the file already exists, and if it exists the file name is changed. Also, if the user puts a different name for the file, the script remembers that and next time shows that title by default. I could not create the contextual menu for the blank space in finder window though. Here is the code:
property defaultFileName : "newTextFile"

on run {input, parameters}
	
	try
		tell application "Finder" to set the sourceFolder to (folder of the front window) as alias
	on error
		-- no open folder windows
		set the sourceFolder to path to desktop folder as alias
	end try
	
	set flagFileExists to true
	set indexFile to ""
	
	repeat while flagFileExists
		set indexFile to (indexFile + 1)
		tell application "Finder"
			set flagFileExists to (exists file (defaultFileName & indexFile & ".txt") in sourceFolder)
		end tell
	end repeat
	
	try
		tell me
			activate
			set newFileName to text returned of (display dialog "Enter new file name" default answer "" & defaultFileName & indexFile)
		end tell
		if newFileName is not equal to "" & defaultFileName & indexFile then
			set defaultFileName to newFileName
		end if
		set newFile to "" & sourceFolder & newFileName & ".txt"
		
		if not flagFileExists then
			set touchScript to "touch " & quoted form of (POSIX path of newFile)
			set openScript to "open " & quoted form of (POSIX path of newFile)			
			do shell script touchScript
			do shell script openScript
		else
			display dialog "File already exists"
		end if
	end try
	return input
end run


[ Reply to This | # ]