Hey Guys. Trying to convert a workflow I have in automator to all applescript. My Workflow currently does the following:
- Open new plain text doc in textedit
- Prompts me to type some text
- Saves the text document in my movies folder as “address.txt”
I know I could re-write my current code as
tell application "TextEdit" to save the front document in "Macintosh HD:Users:comcastcable:Movies:address.txt"
tell application "TextEdit" to quit without saving
But I am using this on multiple machines, so I just want my script to set be able to find the “movies” folder on any machine so I have it re-written as the following:
set theText to "@cable.comcast.com"
tell application "TextEdit"
activate
if (front document exists) = true then
if (text of the front document) ≠"" then
make new document
end if
else
make new document
end if
set the text of the front document to theText
end tell
tell application "Finder"
display dialog "Who do you want these links sent to?
Enter the people in the text document, starting with the person's email, (hit tab key), then type the video name."
end tell
set finalResult to theText
set addressFile to "address.txt"
set logName to ((path to movies folder as text) & addressFile)
set fileRef to open for access file logName with write permission
write finalResult & return to fileRef as «class utf8» starting at eof
close access fileRef
tell application "TextEdit" to quit without saving
I am getting the following error:
File file Macintosh HD:Users:comcastcable:Movies:address.txt is already open.
Any ideas?
Hi,
probably you got an error sometime while testing and the script didn’t close the file
PS:
you can do the whole thing without TextEdit, however you need another separator (tab doesn’t work)
set theText to "@cable.comcast.com"
tell application "Finder"
display dialog "Who do you want these links sent to?
Enter the people in the text document, starting with the person's email, (hit tab key), then type the video name." default answer theText & return & return & return
end tell
set finalResult to text returned of result
set addressFile to "address.txt"
set logName to ((path to movies folder as text) & addressFile)
try
set fileRef to open for access file logName with write permission
write finalResult & return to fileRef starting at eof
close access fileRef
on error
try
close file logName
end try
end try
Note: the try blocks make sure that the file will be closed reliably
Your right. I closed my script, reopened, and ran again. It runs fine. But I needed to have my script save text that I added after my script opened the new text doc. Here is my final script for those who want it:
set theText to "@comcast.net"
tell application "TextEdit"
activate
if (front document exists) = true then
if (text of the front document) ≠"" then
make new document
end if
else
make new document
end if
set the text of the front document to theText
end tell
tell application "Finder"
display dialog "Enter you additional needed text here"
end tell
set addressFile to "address.txt"
tell application "TextEdit" to save the front document in ((path to movies folder as text) & addressFile)
tell application "TextEdit" to quit without saving
Thanks Stefan. As usual. Your way is better.