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: 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 | # ]