it seems like this ought to be so easy–but i’ve been fighting it for over an hour. from what i’ve gathered from poking around the forums, TextEdit has some goofy peculiarities with regard to making new files.
what i want to do is write a quick script that i can launch with some hotkey that will assemble web clippings with the site name and url.
i want to select some text in safari, press some hotkey combo, and have the name of the document containing the selection, the selected text, and the url copied into a textedit document on the desktop. ideally i could repeat this ad nauseam, appending new clippings to the file, and then at some point rename the file on the desktop and start all over again.
I’m fighting the file creation part-i get all kinds of obnoxious errors from textedit. any help would be greatly appreciated.
If all you want to do is create a new text file, I’d suggest skipping TextEdit altogether and just writing the stuff directly to a file using the write to file command. Here’s the basic setup:
set textFile to (((path to desktop) as string) & "thefile.txt") as file specification
set theContents to "This is the text that will be in the file." & return
try
open for access textFile with write permission --Allow the script to edit the file's contents
--If you ever want to overwrite the entire file, put "set eof of textFile to 0" here.
write theContents to textFile starting at eof --eof is the end of the file.
close access textFile --I believe this can cause a memory leak if you don't put it in.
on error
try
close access textFile --So there's no memory leak if there's an error and the file isn't closed.
end try
end try
--For an easy way to see if what you wanted was written to the file, and to let Applescript use what's in there, use:
read textFile
--Returns "This is the text that will be in the file." and adds a line every time.
there is no selection command in Safari so you must grab the text with UI scripting.
Try this:
set textFile to ((path to desktop) as string) & "WebClips.txt"
tell application "Safari" to activate
tell application "System Events" to tell process "Safari" to keystroke "c" using command down
tell application "Safari" to set {theDoc, theURL} to {name, URL} of document 1
write_to_file(theDoc & return & theURL & return & (the clipboard) & return & return, textFile, true)
on write_to_file(|data|, target, append)
try
set open_target_file to ¬
open for access file target with write permission
if append is false then ¬
set eof of open_target_file to 0
write |data| to open_target_file starting at eof
close access open_target_file
return true
on error
try
close access file target
end try
return false
end try
end write_to_file
To run the script with a hotkey, you need a tool like FastScripts or QuicKeys
There’s a JavaScript command that gets the selected text in Safari. I’m not sure if the way I’ve used it below is the correct or the best way “ I’m not a JavaScript expert “ but it works.
Safari’s document names, URLs, and ‘do JavaScript’ results are Unicode text, which is what gets saved to the file with your script. It looks OK in TextEdit, but moving through the document with the left and right arrow keys takes two presses for each character. Just in case that makes any difference to mcaselli, the version of your script below coerces the Unicode to string before sending it to the handler.
set textFile to ((path to desktop) as string) & "WebClips.txt"
tell application "Safari"
set {theDoc, theURL} to {name, URL} of document 1
set selectedText to (do JavaScript "\"\"+window.getSelection();" in front document)
end tell
set stringData to theDoc & return & theURL & return & selectedText & return & return as string
write_to_file(stringData, textFile, true)
on write_to_file(|data|, target, append)
try
set open_target_file to ¬
open for access file target with write permission
if append is false then ¬
set eof of open_target_file to 0
write |data| to open_target_file starting at eof
close access open_target_file
return true
on error
try
close access file target
end try
return false
end try
end write_to_file