Printing 2 copies of a text edit file


I am attempting to automatically print two copies of a text file as soon as the file is saved in a specific folder. Text Edit automatically launches, removes the first 90 characters, then brings up the print dialog. I have attempted to have the script to type the number 2 where the number of copies shows up in the print dialog box, but it just seems to skip that and print one copy instead. All of this is being run on Mac OS X Server 10.4.5.

Ultimately, I would like to have any text file named 1.txt to print 1 copy and 2.txt to print 2 copies, but I cannot even get the 2 copies to work so I’ll deal with the next part after I get this figured out.

Here is what I have so far. I am a novice, so please excuse any odd formatting.

on adding folder items to this_folder after receiving added_items
repeat with each_item in added_items
tell application “TextEdit” to open each_item
tell application “TextEdit”
end tell
tell application “System Events”
key code {125}
repeat with i from 1 to 90
key code {51}
end repeat
key down command
keystroke “p”
key up command
tell sheet 1 of window 1
repeat until exists
delay 0.1
end repeat
key code {84}
key code {36}
end tell
end tell
delay 20
tell application “Finder” to delete each_item
tell application “System Events” to do shell script "kill " & (unix id of first process whose name is “TextEdit”)
end repeat
end adding folder items to




Welcome to MacScripter.

You can use the shell for printing as well. If you successfully can get your script to edit the .txt file, simply re-save the edited version, and use something like this:

do shell script "lpr -#2 " & (quoted form of (POSIX path of each_item))

I just threw in the variable each_item like you have in your current script; although if you save it under a different name, you can use that instead.

If you want a single copy, just omit the -#2 part, and only a single copy will be printed. Be advised that this will access the default printer, so if you need to use a different printer, check out this thread.

In the future, please try to paste your code within the Applescript brackets (available just above the Message window) to make it easier for others to work with your script.

Good luck,

This works for removing the first 90 characters but I have no idea why it goes so slow on my mac.

tell application "TextEdit"
	delete characters 1 thru 90 of paragraph 1 of document 1
end tell