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

Calling a TextExpander routine from TextEdit

Hi, I am trying to write an AppleScript routine that opens a new document in TextEdit and then executes a "snippet" (small piece TextExpander) of code) that will insert some boilerplate text on the TextEdit document.


What I started with:


tell application "TextEdit"

activate

make new document at the front

execute "/excuse" (the name of the snippet in TextExpander)

end tell


I've tried several iterations of this and am still no where close, it does make the new document but that is all the further I've gotten.


I have also looked at the library of TextEdit but cannot seem to find out how to execute an external script.


Any and all help would greatly be appreciated.


Denny

<Email Edited by Host>

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

Posted on Apr 28, 2015 12:42 PM

Reply
14 replies

Apr 28, 2015 4:04 PM in response to dennisfromlebanon

For one, TextEdit has no idea what the command 'execute' is supposed to do, so it's no wonder your script does nothing.


You need to target TextExpander to actually execute the snippet. In addition, TextExpander's documentation implies that 'expand snippet' is the command you want, not 'execute' (although I haven't looked deeply at TextExpander, so 'execute' might be valid).

Ultimately, I'd expect your command to look more like:


tell application "TextEdit"

activate

make new document at the front

end tell


tell application "TextExpander"

expand snippet "/excuse"

end

Apr 28, 2015 5:45 PM in response to Camelot

Camelot,


Thank you so much for your reply and what you pose does seem to make perfect sense to me, I cut and pasted your suggested code and I received the following error:


error "TextExpander got an error: Can’t make \"/excuse\" into type snippet." number -1700 from "/excuse" to snippet


I'm uncertain what this means... It does open a new document, but I am not certain how to step thru the applescript code to see exactly where it stumbled. The snippet I created in TextExpander is not 'plain text', do you suppose that could be part of the problem, It is named "/excuse" and is boilerplate for the following:



To: LSD Attendance

Date: Tue Apr 28, 2015 05:42PM


Subj: Excused absence



Please excuse daughters name absence on:


Date:

Yesterday all day

Yesterday period/s 1st 2nd 3rd 4th 5th 6th 7th

Today period/s 1st 2nd 3rd 4th 5th 6th 7th


For the following reason:


She was sick.

She had a doctor appointment.

She had a dentist appointment.

She was out of town with family.

She had an unexpected emergency.

She got sick in class.

She___________________________



Thank you,


(picture of my signature)


Dennis X. Xxxxxxx

gambuchi@gmail.com

Apr 28, 2015 6:27 PM in response to dennisfromlebanon

Camelot,


FYI: I have played around with the name of the snippet, thinking that perhaps the forward / could be influencing the code. All the same snippet though.


I further changed the snippet from Rich text to plain text, to applescript and finally to shell script all with the same results. If I remove the quotes from around the snippet name, I get the error that it has not been defined as a varialble.


With the quotes left in place, I get this:


User uploaded file


with the quotes removed, I get the following:


User uploaded file


I am at my wit's end, this seemed like it should be so easy. Do you have any more suggestions that might overcome these problems?

Apr 28, 2015 8:02 PM in response to dennisfromlebanon

Hi,


You must specify the group (the foldername in TextExpander), like this :


tell application "TextExpander"
    expand snippet (get snippet "/excuse" of group "My Snippets") -- folder's name
    -- or -- expand snippet (get snippet "/excuse" of group 1) -- the folder's index
end tell



To check the name of the snippets, use this script :

tell application "TextExpander"
    return name of snippets of groups
end tell

Apr 29, 2015 9:27 AM in response to Jacques Rioux

Good day Jacques Rioux,


I attempted to do as you suggested and even then managed to get errors, the following is an example, I am confused...



Using the following script,

User uploaded file


I got this error which makes little sense to me as I am the account administrator...

User uploaded file


I am embarrased to keep asking how to do this, it should be simple but I am missing something. I do GREATLY appreciate your time and will follow your lead and hopefully get this "simple" script to work.

Apr 29, 2015 11:01 AM in response to dennisfromlebanon

Hi Dennis,



dennisfromlebanon wrote:


Good day Jacques Rioux,


I attempted to do as you suggested and even then managed to get errors, the following is an example, I am confused...



Using the following script,

User uploaded file



The syntax of your script is not correct.

You don't need to change anything in my script, except the name of the snippet and the name of the group, if these names are not correct only.


tell application "TextEdit"
     activate
     make new document at the front
end tell
tell application "TextExpander" -- "/excuse" is the snippet's name
     expand snippet (get snippet "/excuse" of group "My Snippets") -- "My Snippets" is the folder's name  in the window of TextExpander
end tell


--

To get the exact name, run this script in the editor and check the result.


set l to {}
tell application "TextExpander"
     repeat with i in groups
          set t to (get name of snippets of i) & {"}" & return}
          set item 1 of t to "Group's name = " & name of i & " : { Snippet's name = " & item 1 of t
          set end of l to t
     end repeat
end tell
set text item delimiters to ", "
set theresult to l as text
set text item delimiters to ""
return theresult

Apr 29, 2015 12:17 PM in response to Jacques Rioux

Jacques, thank you again for the prompt response... I ran the script you sent me to get the exact name and by the looks of the result, I may have been confusing the label I use to execute the snippet with it's actual name... do you think that is the case based on the enclosure I've sent you??User uploaded file


The picture below is what I got when I ran the script that is supposed to expand the snippet:


User uploaded file

Apr 29, 2015 12:43 PM in response to dennisfromlebanon

Jacques,


Below is a picture of the TextExpander window showing how the snippet I want to use is formulated and shows part of the content of the snippet.


User uploaded file


This next picture is of the results when I had changed the snippet name to "Excused absence"... what was produced was actually the contents of

the clipboard from where I copied your code and pasted it into Script Editor... It appears that somehow, the code told TextExpander to insert the contents of the clipboard rather than the contents of the snippet... Can't figure how that happened though.


User uploaded file

Apr 29, 2015 8:52 PM in response to dennisfromlebanon

Hi,



The label in the window of TextExpander is the name of the snippet to use into an AppleScript --> "Excused absence"


You can use an abbreviation, like this :


tell application "TextExpander" -- "/excuse" is the abbreviation  
     expand abbreviation "/excuse"
end tell




I don't know how the contents of the clipboard is pasted into your TextEdit document, perhaps a temporary bug, try to restart the computer if the problem persists.

May 2, 2015 1:17 PM in response to dennisfromlebanon

Hi,



The activate command in this script work when the TextEdit application is already open, but not when TextEdit is closed

tell application "TextEdit"  
     activate  -- stay in the background when TextEdit is closed
     make new document at the front  
end tell 



To work correctly when TextEdit is closed (also work when TextEdit is open):

move the activate command after the line "make new document at the front", like this :


tell application "TextEdit"
     make new document
     activate -- go to the foreground
end tell
tell application "TextExpander" to expand abbreviation "/excuse"

May 2, 2015 5:20 PM in response to dennisfromlebanon

Hi,


Looks like TextEdit is much slower than the script, the same script work without problem here.


Try to use a delay before the expand command

------

tell application "TextEdit"

makenewdocument

activate-- go to the foreground

end tell

do shell script "sleep 2" -- delay of 2 seconds (increase time, if it expand in the Script window.)

tell application "TextExpander" to expand abbreviation "/excuse"

Calling a TextExpander routine from TextEdit

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