Welcome to the MacNN Forums.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

You are here: MacNN Forums > Enthusiast Zone > Classic Macs and Mac OS > applescript for printing

applescript for printing
Thread Tools
Reply With Quote
Apr 18, 2001, 01:09 PM
I was told that you could write an applescript that would let you drag and drop a file onto it and it would print it. The person also said that it would bypass the print dialog box, printing the default ONE copy.

Can anyone tell me what that script would be? I don't know how to have the script detect the creator of the file, for one thing.
Forum Regular
Join Date: Jan 2001
Location: Bruges, Belgium
Status: Offline
Reply With Quote
Apr 19, 2001, 07:26 AM
for creator detection use "standard extra's"
(can't say more about it cause I'm working on pc now)

Dedicated MacNNer
Join Date: Nov 1999
Location: Georgetown, Demerara, Guyana
Status: Offline
Reply With Quote
Apr 19, 2001, 11:12 AM
Hi jgates,

I guess you want to emulate the way a PostScript desktop-printer normally works (i.e., by directly printing a file without any user intervention), for non-PostScript documents? You could try searching for a suitable script at the various AppleScript websites (e.g., MacScripter.net's ScriptBuilders library), or read on for some possible approaches.

As caifara suggested above, you could readily use the 'info for' command (which was rolled into the 'Standard Additions' scripting addition or OSAX under Mac OS 8.5+) to obtain the creator-code of a drag-&-dropped file or, better yet, directly retrieve the full pathname of the owner application. Alternatively, you could use a couple of commands in the scriptable Finder to accomplish the same thing.

On the other hand, bypassing the Print dialogue to automatically print just "one copy" is a much trickier task. Unfortunately, unlike prompted printing, there is no standard scripting command for automatic one-copy printing. E.g., Tex-Edit Plus uses 'print one copy theseFiles', whereas AppleWorks uses 'print theseFiles with one copy'. To accommodate several applications, you would probably end up to having to write different code for each of them. Moreover, these commands might not always work reliably (e.g., Tex-Edit Plus sometimes ignores the 'print one copy' command if there isn't at least one window already open).

Instead, you could try using one of the freeware OSAXen that can automate typing 'Cmd-P' followed by 'Return', which simulates invoking the Print dialogue via the standard menu-shortcut, and then auto-accepting it via the default button (usually 'Print' or 'OK'). Examples of such OSAXen include Sändi's Additions, Sigma's Additions, etc., which can all be found in the usual Keyboard "folder" of MacScripter.net's Scripting Additions library.

A sample script using the Sändi's Additions OSAX is shown below; you could save it as an applet/droplet from within Script Editor. The script assumes that a printer has already been selected in the Chooser, and it attempts to handle any number of files (possibly having different creator codes).
-- -------------------------------------------------------------------------------------------------------- on run set thisFile to (choose file with prompt "Select file(s) to print." of type {}) -- Allow all file-types set theseFiles to {thisFile} return (RunOrOpen(theseFiles) of me) end run -- -------------------------------------------------------------------------------------------------------- on open theseFiles return (RunOrOpen(theseFiles) of me) end open -- -------------------------------------------------------------------------------------------------------- on RunOrOpen(theseFiles) -- Loop through each file in turn repeat with thisFileRef in theseFiles set thisFile to (contents of thisFileRef) -- Obtain the full pathname of this file's owner application -- ................................................................................................................................. -- METHOD A.:- USING THE 'STANDARD ADDITIONS' OSAX set thisFileInfo to (info for thisFile) -- set theAppCreatorCode to (file creator of thisFileInfo) -- We don't actually need this set theAppPathname to ((default application of thisFileInfo) as string) -- ................................................................................................................................. (* -- METHOD B.:- USING THE FINDER tell application "Finder" set theAppCreatorCode to (creator type of (item thisFile)) set theAppRef to (application file id theAppCreatorCode) set theAppPathname to (theAppRef as string) end tell *) -- Print this file, via its owner application tell (application theAppPathname) -- Bring the application frontmost (required when simulating keystrokes) -- NOTE: This 'activate' command might not work with all apps, -- so you could try using 'tell app "Finder" to open (item theAppPathname)' instead activate -- Open this file into a window open {thisFile} -- Simulate invoking & accepting the Print dialogue for this window -- NOTE: Requires the Sändi's Additions OSAX TypeText ("p" & return) with Command -- Close this window (to avoid screen clutter) -- NOTE: This 'close window 1' command is optional; and, it might not work with all apps, -- so you could try using 'TypeText "w" with Command' instead close window 1 end tell end repeat return end RunOrOpen
Note that the above script isn't very robust, as it doesn't include error-handling. Also, as noted in the script's comments, the 'activate' and the 'close window 1' commands might be incompatible with many apps (e.g., some apps don't recognise the 'activate' command at all, and some versions of MS Internet Explorer use 'CloseWindow windowID' instead of 'close windowRef'); to work around these issues, you could try using alternative commands as described in the comments. Further, the script doesn't quit any owner application (after launching it to print a file), so if several different apps are being launched, it could lead to memory shortage.

Regards, and good luck,


[This message has been edited by Paul Crawford (edited 04-19-2001).]
Thread Tools
Forum Links
Forum Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Privacy Policy
All times are GMT -4. The time now is 12:18 AM.
All contents of these forums © 1995-2015 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.8 © 2000-2015, Jelsoft Enterprises Ltd., Content Relevant URLs by vBSEO 3.3.2