MacNN Forums (
-   Classic Macs and Mac OS (
-   -   applescript for printing (

jgates Apr 18, 2001 02:09 PM
applescript for printing
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.
caifara Apr 19, 2001 08:26 AM
for creator detection use "standard extra's"
(can't say more about it cause I'm working on pc now)

Paul Crawford Apr 19, 2001 12:12 PM
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.,'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'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
                -- .................................................................................................................................
                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
                        -- 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
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).]
All times are GMT -4. The time now is 02:44 PM.

Copyright © 2005-2007 MacNN. All rights reserved.
Powered by vBulletin® Version 3.8.8
Copyright ©2000 - 2017, vBulletin Solutions, Inc.

Content Relevant URLs by vBSEO 3.3.2