Apple Event: May 7th at 7 am PT

Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

Writing a script to expand a snippet within TextEdit.

I posted this problem before and unfortunately the folks that responded didn't completely solve my problem...


1..I have a "snippet" in the keyboard expansion application "TextExpander" named 'Excused absence'... what this snippet does is expand into a boilerplate letter to provide an excuse in High School when my daughter is sick or otherwise misses a class or classes or the whole day.


My goal: To write an 'AppleScript' routine that will first open a new document in 'Textedit' and then cause the snippet "Excused absence" to expand in the newly created 'Textedit' document...


Thus far I have code provided by Jacques that DOES open a new document and DOES expand the snippet 'Excused absence'... The problem is that it expands the snippet within "Script Editor" rather than in the 'new document created in Textedit' Following is the code that Jacques sent to me (my thanks to him for his effort)


telUser uploaded file


I I am at my wits end with what I had hoped would be a simple task using AppleScript...


any and all replies/help would be appreciated greatly

MacBook Pro with Retina display, OS X Yosemite (10.10.3), null

Posted on Apr 30, 2015 4:56 PM

Reply
4 replies

Apr 30, 2015 8:18 PM in response to dennisfromlebanon

This will write your excuse snippet to a variable, which is then assigned to the text of the new TextEdit document. This document is created quietly without launching TextEdit.


set fp to ((path to desktop) as text) & "Excuse.txt"


tell application "TextExpander"

set excused_text to expand snippet (get snippet "Excused absence" of group "My Snippets")

end tell


tell application "TextEdit"

set newDoc to (make new document at the front with properties {name:fp, text:excused_text})

tell text of newDoc

set font to "Helvetica Neue"

set size to 14

end tell

save newDoc in file fp

end tell

May 1, 2015 8:45 AM in response to VikingOSX

Dear VikingOSX,


First, thank you for your reply, Not knowing if the script you wrote is able to be cut and pasted into Script Editor 'as is', I tried that first and got the error window listed below. Then, within seconds, Textedit crashed... I repeated this 3 times to make sure it was no fluke. If I need to change anything in the script you sent to me, please let me know... I appreciate your effort and really want to get this to work.


User uploaded file


I have other scripts that try to work, they even open up the Textedit window but no snippet expansion in Textedit... that said, the snippet does expand within Script Editor following the last "End Tell" statement ??? why would that be, all I can think is that for some reason Textedit must either go to the background or, it opens after the snippet expands in Script Editor.

May 1, 2015 10:42 AM in response to dennisfromlebanon

Ok. This script works using an external file with two paragraphs of text as a substitute for TextExpander, which I don't have installed. No more Apple Event errors. Quickly writes out the two paragraphs of Lorem Ipsum from the choose file into the Excuse.txt file. Tested on Yosemite 10.10.3.


set fp to ((path to desktop) as text) & "Excuse.txt"


(*

tell application "TextExpander"

set excused_text to expand snippet (get snippet "Excused absence" of group "My Snippets")

end tell

*)

-- remove these two lines after uncommenting the above tell block

set thisFile to choose file

set excused_text to (readthisFileastext)


tell application "TextEdit"

set newDoc to (makenewdocumentwith properties {name:fp, text:excused_text})

tell text of newDoc

set font to "Helvetica Neue"

set size to 14

end tell

savenewDocinfilefpasalias

end tell


-- quit lingering applications that may remain open

tell application "TextEdit" to if it is running then quit

-- tell application "TextExpander" to if it is running then

Writing a script to expand a snippet within TextEdit.

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple ID.