Can I automate printing to PDF?
My goal is to find an easy way to automate printing a web page to PDF to a specific application. I can obviously do that manually by selecting File/Print or cmd-P, click on the PDF button and then scroll down to the selection I want, but I'm doing this multiple times a day and I'd like to automate it.
I did have it setup to be done via iKey by hitting cmd P, and then the dialogue box was keyboard navigable by hitting tab or arrow to move from field to field but that’s no longer the case for some reason, so that macro no longer works. The only way I can find to select the pdf button on the print dialogue is with the mouse. Is this something that could be done with AppleScript? I know nothing about scripting, but this seems like it should be simple. And if it makes a difference, I'm trying to print the PDF to the application Together, a very cool info management app I'm now using instead of DevonThink Pro for much of my archiving. If anyone has any suggestions, I'd be much obliged. |
I am in the middle of scripting a solution to use with DEVONthink Pro right now. Here is what I have so far:
[CODE](* Script to download a webpage in OmniWeb as a single page PDF with named with either the selected text or the name of the page. If you choose, it will also processes that title for correct capitalization.*) (* If uncommented, this will process the eventual title for correct capitalization by loading the Capitalize Script Library written by Christiaan Hofman and available at his page: http://www.physics.rutgers.edu/~hofman/applescript/ You will also need to uncomment a section near the end of the script.*) --set capitalizeLib to (load script file ¬ --((path to home folder as string) & "Library:ScriptingAdditions:Capitalize.scpt")) tell application "OmniWeb" try activate if not (exists browser 1) then error "No browser is open." set this_window to browser 1 set this_url to address of this_window set PDF_name to the name of this_window (*Setting window size to get a good width for the PDF (It doesn't matter for all pages, but it does seem to affect some pages) *) set bounds of this_window to {700, 22, 1250, 750} (*Try to get selected text for the filename, else use page title. While the command set this_name to do script "unescape(getSelection())" window this_window is more elegant, it doesn't work on all pages. *) tell application "System Events" set old_clipboard to (the clipboard) set the clipboard to "" keystroke "c" using {command down} if (the clipboard) is not "" then set PDF_name to the clipboard set the clipboard to (old_clipboard) -- Uncomment this section also if you want to implement title processing --tell capitalizeLib -- set PDF_name to capitalize(PDF_name) --end tell -- Executing key command to save page as the displayed title to the desktop keystroke "S" using {shift down, option down, command down} -- Manipulating the save dialog box tell application process "OmniWeb" keystroke "d" using {command down} -- Setting save location to the desktop set value of text field 1 of sheet 1 of front window to PDF_name -- Setting the filename click button 1 of sheet 1 of front window -- Clicking the save button end tell end tell end try end tell[/CODE] Over the next couple of days, I am going to implement a dialog box that lets you double check the name and input or select tags for the file to be written to the Spotlight comments section by the finder. For use with Together, you might might to use a folder action script to subsequently import the file. This could perhaps be configured to only process files with a certain character/string appended to the title or a spotlight comment. I'd really appreciate any suggestions people might have for improving or extending this script. |
this looks awesome, I'm looking forward to seeing where it goes.
But I am definitely not the guy to provide technical comment on the script. I’ll leave that to others. Thanks for sharing! |
This may be off topic a bit, but whatever happened to the feature of holding down option and the File > Save As… menu item changed to Save As PDF… ? I am really missing this feature in 5.7.
|
Danoz,
the save as PDF shortcut does work, though shift-option-command-s isn't particularly easy to hit. Of course, you can always use iKey or some other utility to handle with with a single key press. However, I am trying to find a more powerful archiving solution to fit into my workflow. rmathes, While I haven't given up on DEVONthink yet, I have been looking at Together recently. Unfortunately, it seems that it is not applescriptable, which does limit options for automation. However, based on a post I found on Applescript.net, [URL="http://bbs.applescript.net/viewtopic.php?id=19238"]http://bbs.applescript.net/viewtopic.php?id=19238[/URL], I was able to get most of the way into the script for you. You will need to tailor it to your set-up, but [CODE]tell application "OmniWeb" activate if not (exists browser 1) then error "No browser is open." end tell tell application "System Events" try keystroke "p" using {command down} tell application process "OmniWeb" click menu button 1 of sheet 1 of front window delay 0.1 repeat (* "X times", with x as an integer depending on position of desired application in menu*) keystroke (ASCII character 31) -- down arrow key end repeat keystroke return end tell end try end tell [/CODE] I also made some significant additions to the script I posted earlier. Because it is probably overkill for most people, I will post it here instead of editing my earlier post. -If you have selected some text in the front browser, it sets that text as the title of the page, -Optionally converts the title to title case -Presents a dialog box for tagging the file, -Formats those tags (currently for Punakea, though it would be easy to change it for another program) -Sets the spotlight comment to the page URL and those tags -Provided that you enable it in the script, displays a Growl notification that everything went OK (though I should improve the error handling) [CODE](* Script to download a webpage in OmniWeb as a single page PDF, named with either the selected text or the name of the page. Will optionally processes that title for correct capitalization. *) -- Properties for file tagging format; Currently set for Punakea property tag_prepend : " ###begin_tags###" property tag_append : "###end_tags### " property tag_prefix : "@" property tag_separator : ";" property tag_spacer : " " property tag_string : "" -- Basic settings for handling file tagging after the download property downloadLocation : path to desktop from user domain as string property fileExtension : ".pdf" property titlecase : false -- Change to enable conversion of title to title case tell application "OmniWeb" try activate if not (exists browser 1) then error "No browser is open." set theBrowser to front browser set theURL to the address of theBrowser set theName to the name of theBrowser (*Setting window size to get a good width for the PDF (It doesn't matter for all pages, but it does seem to affect some pages) *) set oldBounds to bounds of theBrowser set bounds of theBrowser to {700, 22, 1250, 750} (*Try to get selected text for the filename, else use page title. While the command set this_name to do script "unescape(getSelection())" window this_window is more elegant, it doesn't work on all pages. *) tell application "System Events" set old_clipboard to (the clipboard) set the clipboard to "" keystroke "c" using {command down} if (the clipboard) is not "" then set PDF_name to the clipboard set the clipboard to (old_clipboard) end tell set theName to my formatTitle(theName) --Setting the file name to titlecase, removing illegal characters -- Displaying the dialog with proposed record name, with the option of adding tags to the file tell application "OmniWeb" activate set theResult to display dialog ¬ "The file will be saved as: " & return & return & (theName) & return default answer ¬ "Enter tags (separated by commas)" buttons {"Change Title", "Cancel", "OK"} ¬ default button 3 with title "Set Title and Tags" set the_tags to text returned of theResult -- Changing the title if (button returned of theResult) is "Change Title" then set the_result to display dialog ¬ "New Title:" default answer ¬ theName with icon 1 ¬ buttons {"Cancel", "OK"} ¬ default button "OK" set theName to text returned of the_result end if --set file_tags to text returned of the result -- Getting the tags set tag_string to theURL & my formatTags(the_tags) -- Processing the file tags to encode in the correct format end tell tell application "System Events" try keystroke "S" using {shift down, option down, command down} keystroke "d" using {command down} -- Setting save location to the desktop set value of text field 1 of sheet 1 of front window of application process "OmniWeb" to theName -- Setting the filename click button 1 of sheet 1 of front window of application process "OmniWeb" -- Clicking the save button on error errMsg display dialog errMsg buttons {"Cancel"} default button "Cancel" end try set bounds of theBrowser to oldBounds -- Restoring original window boundaries end tell on error error_message number error_number if the error_number is not -128 then display alert "OmniWeb" message error_message as warning end try end tell delay 1.0 set theFile to downloadLocation & theName & fileExtension as alias my tagFile(theFile, tag_string) set growlDescrip to theName & " Downloaded Successfully" my growlNotification("OmniWeb", "PDF Capture", growlDescrip) on formatTitle(this_text) if titlecase is true then set this_text to do shell script "python -c \"import sys; print unicode(sys.argv[1], 'utf8').title().encode('utf8')\" " & quoted form of this_text set searchString to ":" set replacementString to "- " set currentDelimiter to AppleScript's text item delimiters set AppleScript's text item delimiters to the searchString set the item_list to every text item of this_text set AppleScript's text item delimiters to the replacementString set this_text to the item_list as string set AppleScript's text item delimiters to "" set AppleScript's text item delimiters to currentDelimiter return this_text end formatTitle on growlNotification(growlIcon, growlTitle, growlDescrip) tell application "System Events" set Growl_active to (name of processes) contains "GrowlHelperApp" end tell if Growl_active then set appName to "MattsNotif" set notifs to {growlTitle} tell application "GrowlHelperApp" register as application ¬ appName all notifications notifs ¬ default notifications notifs ¬ icon of application growlIcon notify with name growlTitle title growlTitle description growlDescrip application name appName end tell end if return "" end growlNotification on formatTags(file_tags) if file_tags is not "" then set current_delimiter to AppleScript's text item delimiters set AppleScript's text item delimiters to ", " set tag_string to tag_spacer & tag_prepend -- Adding the string prefix for Punakea repeat with i from 1 to the count of text items of the file_tags set this_tag to text item i of the file_tags set tag_string to tag_string & (tag_prefix & this_tag & tag_separator) end repeat set tag_string to tag_string & tag_append -- Adding the string suffix for Punakea set AppleScript's text item delimiters to "" set AppleScript's text item delimiters to current_delimiter end if return tag_string end formatTags -- Subroutine to tag a file on tagFile(theFile, tag_string) tell application "Finder" try if (exists theFile) then set current_comment to comment of theFile set the comment of theFile to current_comment & " " & tag_string else if not (exists file the_file) then error "The file was not created." end if on error errMsg display dialog errMsg buttons {"Cancel"} default button "Cancel" end try end tell end tagFile [/CODE] Edited 2/24/08 to clean up code 2/25/08 to complete code for print to . . . |
VN23....thank you!
So, dumb question but what syntax do I use to arrow down 10 times to get to the Together menu option (it’s the 10th item in the list)? |
No, not a dumb question--there was a reason I didn't put it in there in the first place. Figured it out, though: "keystroke (ASCII character 31)". I amended my earlier post with the new code and updated the longer one too.
|
[QUOTE=VB-23;33374]Danoz,
the save as PDF shortcut does work, though shift-option-command-s isn't particularly easy to hit. [/QUOTE] Thanks very much. This has escaped my attention. |
VB-23....awesome, we’re almost there! I edited the code per your instructions and it works great. Only problem is, it moves the cursor to highlight the correct selection but doesn’t press ‘return’ to complete the command. And I don’t know enough about the syntax to know how to put that in or where it should go. If I could impose on you for this one last thing....
Then I'll setup iKey to invoke the script with a hotkey and I'm in business. You are the man, thanks! |
rmathes,
most of the keystroke commands are far more intuitive than those for the arrow keys: keystroke return |
All times are GMT -8. The time now is 07:29 AM. |
Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2024, vBulletin Solutions, Inc.