Friday, January 28, 2022

#1 2012-08-23 03:18:31 pm

jasonwood
Member
Registered: 2008-10-31
Posts: 4

Print a PDF file on the desktop.

Applescript:

set filepath to (((path to desktop) as string) & "test.pdf")

tell application "Preview"
   activate
   open file filepath
   print file filepath
   close document filepath
end tell

If preview is closed when the script is executed, it prints fine, but it doesn't close the file, and eventually results in the error "AppleEvent timed out".

If preview is open when the script is executed (but no windows are open), the file will open but it will not even print. Same time out error eventually.

What am I doing wrong here?

Thanks!

Offline

 

#2 2012-08-23 03:39:52 pm

McUsr
Member
From:: Southern Norway
Registered: 2010-04-07
Posts: 1776

Re: Print a PDF file on the desktop.

The first thing you do wrong is using an application that isn't scriptable.

I haven't tried the lines below, but see if it works anyway, when you have edited it in. smile

Applescript:


tell application "Finder"
print file filepath
end tell


Mercurial vcs is a joy to use for scripting.


Filed under: print

Offline

 

#3 2012-08-23 04:25:39 pm

jasonwood
Member
Registered: 2008-10-31
Posts: 4

Re: Print a PDF file on the desktop.

Nothing happened with that script - including no errors.

So I've moved on to GUI scripting and came up with this. It works. Is this my best option? Any way to make it better? The delays are a bit annoying...

Applescript:

set filepath to (((path to desktop) as string) & "test.pdf")

tell application "Preview"
   activate
   open file filepath
end tell

tell application "System Events"
   tell process "Preview"
       delay 1
       keystroke "p" using command down
       delay 1
       keystroke return
       delay 1
       keystroke "w" using command down
   end tell
end tell

Offline

 

#4 2012-08-23 04:35:39 pm

McUsr
Member
From:: Southern Norway
Registered: 2010-04-07
Posts: 1776

Re: Print a PDF file on the desktop.

How about downloading Skim it is a decent pdf viewer, and fairly scriptable!

There is however a different way to do printing.

Edit

you can actually do it by a do shell script and the lpr command, see man lpr

Last edited by McUsr (2012-08-24 01:56:34 pm)


Mercurial vcs is a joy to use for scripting.


Filed under: print

Offline

 

#5 2012-08-24 10:53:02 am

jasonwood
Member
Registered: 2008-10-31
Posts: 4

Re: Print a PDF file on the desktop.

That's amazing!!! I replaced my entire script with:

Applescript:

do shell script ("cd ~/Desktop && lpr test.pdf")

Thank you!!!

Offline

 

#6 2012-08-24 01:54:40 pm

McUsr
Member
From:: Southern Norway
Registered: 2010-04-07
Posts: 1776

Re: Print a PDF file on the desktop.

smile
You can even make a droplet out of it.

Save this to the desktop as an application bundle.

Applescript:


on open _files
   
   repeat with f in _files
       set pf to quoted form of POSIX path of (f as text)
       do shell script "lpr " & pf
   end repeat
end open

then run this script :

Applescript:


tell application "Finder"
   open "Macintosh HD:System:Library:CoreServices:CoreTypes.bundle:Contents:Resources"
end tell

Open the file "PrintMonitorFolderIcon.icns", in Preview, select item 3 from the sidebar, and copy it to clipboard.
select the printer.app in Finder, show info for it, select the icon in the upper left corner and paste the icon on to it! wink

From now onwards, you can just drag and drop files onto it, and you can also put it onto Finders toolbar, (if you hold down cmd, while you click on the libelle in the upper left corner, you can change view options  of the toolbar icons, do try it. wink

Last edited by McUsr (2012-08-24 01:59:03 pm)


Mercurial vcs is a joy to use for scripting.


Filed under: print

Offline

 

#7 2012-08-24 05:36:17 pm

mouramartins
Member
From:: Algarve
Registered: 2010-11-21
Posts: 156

Re: Print a PDF file on the desktop.

jasonwood wrote:

Applescript:

set filepath to (((path to desktop) as string) & "test.pdf")

tell application "Preview"
   print filepath
end tell


Should work as you want.

Offline

 

#8 2012-08-24 06:41:23 pm

McUsr
Member
From:: Southern Norway
Registered: 2010-04-07
Posts: 1776

Re: Print a PDF file on the desktop.

Hi! smile

Something in Preview that you actually can script! wink

I made a droplet using the Preview snippet supplied by mouramartins.
You can use that as a replacement for the print droplet in the post above.

Applescript:


on open _files
   
   repeat with f in _files
       tell application "Preview"
           print (f as text)
       end tell
   end repeat
end open

Edit I just want to add, that I believe the Preview version to work for me, since I have been inside the package of Preview.app (the bundle), there I have been in the folder Contents/Resources, and edited the info.plist file, to state that it is scriptable. I  have read that for this to work on Os X Mountain Lion, you have to make a copy of Preview.app, and do the hack on the copy, due to code signing. Or Preview won't work.

But I don't know, maybe the print command always works in Preview. smile

Last edited by McUsr (2012-08-24 07:02:17 pm)


Mercurial vcs is a joy to use for scripting.


Filed under: print

Offline

 

#9 2012-08-25 04:26:32 am

mouramartins
Member
From:: Algarve
Registered: 2010-11-21
Posts: 156

Re: Print a PDF file on the desktop.

McUsr wrote:

But I don't know, maybe the print command always works in Preview. smile


I remember in primary school we were all singing:

"print, quit, save, open
are really hard core"


Happy days...

lol

Offline

 

#10 2012-08-25 06:07:03 am

McUsr
Member
From:: Southern Norway
Registered: 2010-04-07
Posts: 1776

Re: Print a PDF file on the desktop.

mouramartins wrote:

I remember in primary school we were all singing:

"print, quit, save, open
are really hard core"


Yes, it is so, isn't it. wink


Happy days to you as well smile

Last edited by McUsr (2012-08-25 06:07:51 am)


Mercurial vcs is a joy to use for scripting.


Filed under: hardcore

Offline

 

#11 2012-08-26 06:39:54 pm

relishgargler
Member
From:: Newport, MN
Registered: 2010-07-21
Posts: 87
Website

Re: Print a PDF file on the desktop.

This is part of a folder actions script I've been working on. The print part works great.

Applescript:

on adding folder items to thisFolder after receiving addedItems
   tell application "Finder"
       set finishedPrint to folder "a file path"
   end tell

   repeat with anItem in addedItems
...other stuff...
       tell application "Finder"
           print anItem
           delay 10
           move anItem to finishedPrint with replacing
       end tell
   end repeat
end adding folder items to

I don't think I really need the delay 10 in there, but it doesn't slow anything critical down for me. It gives Preview time to open the doc, send it to the printer and close it down before trying to process the next file. I should note though that I had to change the default program for PDFs back to Preview. Illustrator would pop up import dialogs that interrupted the script. Never bothered with trying Acrobat, since Preview takes up less RAM sitting open all day. (I use Illustrator for 95% of my work, hence changing the default.)

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)