Save new TextEdit document to current folder

Greetings – I’m trying to add a script that I can activate with a hotkey/Alfred/Launchbar etc. to add a new blank text file in the current folder.

I’ve found a couple of versions on the web, and this is what I have:


on run
	tell application "Finder" to set myFolder to (folder of the front window) as text
	tell application "TextEdit"
		activate
		make new document
		set theDate to current date
		save document 1 in myFolder
	end tell
end run

Problem is, I’m getting an error “TextEdit got an error: AppleEvent handler failed.” number -10000

When I run it from the AppleScript Editor I get the message “The document “Untitled 2” could not be saved as “Test Folder”. You don’t have permission.”

I’ve done some fairly serious investigation, tried using the POSIX path, etc. But I’m thinking this is a Lion issue with the ‘save’ command I’ve seen elsewhere: http://macscripter.net/viewtopic.php?id=36661&p=1

Any ideas on getting this to work? It seems so simple!

thanks!

john.

Model: iMac
Browser: Safari 534.52.7
Operating System: Mac OS X (10.7)


on run
	tell application "Finder" to set myFolder to (folder of the front window) as text
	tell application "TextEdit"
		activate
		make new document
		set theDate to current date
		tell application "System Events" to tell process "TextEdit" to tell menu bar 1 to tell menu bar item 5 to tell menu 1 to tell menu item -1 to set flag to enabled
		name of document 1
		if flag then
			save document 1 in file (myFolder & result & ".rtf")
		else
			save document 1 in file (myFolder & result & ".txt")
		end if
	end tell
end run

Replaced the first one by a version taking care of the default format.

Yvan KOENIG (VALLAURIS, France) samedi 14 janvier 2012 20:03:10

Hi. Welcome to MacScripter.

To be expicit about what Yvan’s done in his script, the paradigm is that you save a document in a file, not a folder.

Thank you, Yvan – this works perfectly.

At first I got an error – “System Events got an error: Access for assistive devices is disabled. -1719”

So I went into System Preferences → Universal Access and checked “Enable access for assistive devices” and that did the trick… thank you!

And thank you, Nigel for the clarification – that makes perfect sense.

Btw Yvan, I’m a big fan – I still use a script of yours I found on the Apple Support website to separate a field containing first and last name into separate fields in Numbers… a lifesaver! Glad I have the opportunity to thank you for that. :slight_smile:

Here is a slightly different version.



on run
	my activateGUIscripting()
	tell application "Finder" to set myFolder to (folder of the front window) as text
	tell application "TextEdit"
		activate
		make new document
		tell application "System Events" to tell process "TextEdit" to tell menu bar 1 to tell menu bar item 5 to tell menu 1 to tell menu item -1
			if enabled then
				".rtf"
			else
				".txt"
			end if
		end tell
		save document 1 in file (myFolder & my dateTimeStamp() & result)
	end tell
end run

--=====

on dateTimeStamp()
	return (do shell script "date +_%Y-%m-%dT%H.%M.%S")
end dateTimeStamp

--=====

on activateGUIscripting()
	(* to be sure than GUI scripting will be active *)
	tell application "System Events"
		if not (UI elements enabled) then set (UI elements enabled) to true
	end tell
end activateGUIscripting

--=====

As Nigel pointed, as English is not my main language I don’t comment too much what I post :wink:

I just saw the comment about GUIScripting so I added a handler.

Yvan KOENIG (VALLAURIS, France) samedi 14 janvier 2012 20:59:48

Yes, very nice – better actually, I realized that the first version will overwrite a file if one with the same name is already there – “untitled.txt” for instance… the date/timestamp is a good solution.

Thank you again - hope others find one of these two scripts useful!