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

How to write applescript to print TextEdit document

Hi!


I'm trying to set up automator workflow to print a plain text document created in TextEdit as quickly as possible. (this is to be used in a live performance where I need to deliver printed scripts to performers just before they speak, so every second I can shave off the time taken to send the data to the printer is crucial)


The work flow:

get finder item > print finder item

is very quick but I get strange results: the text goes right from one side of the paper to the other and words are broken across lines (no hyphens just broken). (If I print the same document from Textedit it comes out fine with margins and no broken words.)


Please does anybody know how I might write an applescript to print the document instead (that I could insert into my automator workflow which also contains other actions after the printing) so that I could control the printing settings and the words would not break? The margins don't matter so much but broken words are very hard to read.


I have to use plain text for other reasons - that part is non-negotiable.


Thanks in advance for any ideas!

MacBook Pro (Retina, 15-inch, Late 2013)

Posted on Nov 11, 2016 4:17 AM

Reply
5 replies

Nov 11, 2016 7:32 AM in response to VikingOSX

Thanks for this. It prints quickly but not how I wanted it to (this time the text was a narrow column although words weren't broken - I guess because I have the file open as a very narrow window on my computer, but I need it to be like that for other reasons).

I have used this workflow (see below) successfully in terms of the print coming out looking how I want it to, but as it simulates keystrokes in the print dialog window it is a little clumsy and delays the print job by a couple of vital seconds. Do you know how I could adapt it so I'm not simulating keystrokes please? Also I don't want textedit to quit afterwards because i'm using it all the time to modify the document live before it gets printed again. It's a complicated setup!


Thanks again


User uploaded file

Nov 11, 2016 5:31 AM in response to banana_123

The following allows you to select a text file, and pass it to TextEdit, which will then print it to the default printer without any print dialog. Then TextEdit checks to see if it is running and quits. Everything happens “silently” without any application, or dialog windows after the initial document selection. Tested on OS X 10.11.6. Click on the following to enlarge.


User uploaded file

Nov 11, 2016 9:07 AM in response to banana_123

Your Run AppleScript content as it is currently written will ignore your specified Finder item file, launch TextEdit, and present the File Chooser. It won't print until you have selected a file.


If you intent is to just pass the Playground.txt document into TextEdit, have it print the file, and then remain open, then you will need this AppleScript in place of your current effort:


on run {input, parameters}

set myDoc to input as text

tell application "TextEdit"

activate

openmyDoc

delay 0.2

tell application "System Events" to keystroke "p" using {command down}

tell application "System Events" to key code 36

-- closemyDocsavingno

end tell

end run

The following replacement AppleScript functions exactly as the preceding GUI scripting AppleScript, and does not change any print panel settings.

on run {input, parameters}

tell application "TextEdit"

activate

openinput

printinputwith properties {target printer:"default"} without print dialog

-- close input saving no

end tell

end run

And used in the context of your original workflow:

User uploaded file

Whether I ran your the solution with GUI scripting, or with the above workflow, the input text file printed exactly as it appeared in TextEdit.

Nov 11, 2016 1:51 PM in response to VikingOSX

I see now that I don't need the "get specified finder item" action as I run my workflow as a service directly from the open file (playground.txt) which I am constantly editing and then wanting to print.


The script you sent me prints very quickly but it is still only printing the text in a column (both the GUI and the applescript version) - not surprisingly as I have my window set to being very narrow (for other reasons and I can't change this). But when I hit cmd p either manually, or via the service I built, the text shuffles itself into a regular block with standard margins. Also if I save my file as a pdf it does that.


Do you know how I can alter the "get file and print" script that you've sent me to forget about the width of the window and therefore the text? (But not to go to the other extreme of forgetting word breaks as the "print finder item" does).

Nov 11, 2016 3:37 PM in response to banana_123

The issue is that TextEdit remembers what was set in the Print panel from the last time they were set. I get what I see in TextEdit with plain text documents — not the column issue that you report. Of course, if you actually have plain text layed out in columns, that might be another matter.


The two screen shots below are not settings that I need enabled to receive a normal printout. They may be necessary for your situation though, so I recommend trying them. Set them before you run the automator solution. You likely do not need the collate pages setting enabled.

User uploaded file

User uploaded file

How to write applescript to print TextEdit document

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