 |
|
Mar 9th, 2008, 05:24 AM
|
#11
|
|
Full Citizen
Join Date: Jul 2003
Location: New Zealand
Posts: 523
|
This should be simple with UI scripting, but turns out it isn't. The very long applescript in this post may work for you:
MacScripter BBS | Applescript Forums / Save as PDF
However, a different approach might be to use a command-line tool to do the job, like this one (it takes a URL and converts to pdf):
wkpdf - Trac
Automator could take the URL from the page you are looking at in Safari and send it to a shell script to do the conversion.
Good luck!
__________________
How could you not be happy with balloons full of hamsters hanging off of your belt?
|
|
|
Mar 9th, 2008, 07:09 AM
|
#12
|
|
New Neighbour
Join Date: Mar 2008
Posts: 4
|
Found Solution
Thanks for the previous reply. I ended up figuring out a solution. Actually, spent a good deal longer trying to see if I could detect the presence of a modifier key so I can either choose to automatically save or allow me to choose a filename and/or location. But I sorta gave up on the later.
Here is the code to select the "Save as PDF..." option from the Print dialog:
Code:
# Saves current document open in Firefox as PDF
activate application "Firefox"
tell application "System Events"
tell process "Firefox"
# Open the print dialog
keystroke "p" using command down
# Wait until the Print dialog opens before proceeding
repeat until exists window "Print"
end repeat
# Expand the "PDF" menu button (must be expanded before the menu is referencable)
click menu button "PDF" of window "Print"
# Wait until the Menu button menu is created before proceeding
repeat until exists menu item "Save as PDF…" of menu 1 of menu button "PDF" of window "Print"
end repeat
# Select the "Save as PDF" menu item
click menu item "Save as PDF…" of menu 1 of menu button "PDF" of window "Print"
# Wait until the Save dialog opens before proceeding
repeat until exists window "Save"
end repeat
# Paste the contents of the clipboard in and Save
# This is sorta hack; Probably best to leave the 'Save As" dialog open and let the user finish it off but I have a special purpose
if (get (the clipboard) is not "") then
set value of text field 1 of window "Save" to get (the clipboard) & ".pdf"
end if
click button "Save" of window "Save"
end tell
end tell
|
|
|
Mar 9th, 2008, 07:58 PM
|
#13
|
|
New Neighbour
Join Date: Mar 2008
Posts: 4
|
My last post only works for applications that use the print dialog window instead of the sheet and it was hard-coded to Firefox. I generalized to work with [I think] any app it and figured I'd post that as well:
Code:
# Saves current document open in foreground app as PDF using the Print dialog "Save as PDF" option
# value to limit how long we wait for a given dialog or menu popup
set maxloops to 100
# Open a log file - only used for debugging
# Log file is written to the root of the hard disk
# set fid to open for access "mylog.txt" with write permission
### Set process name based on the frontmost app ###
# This allows the script to run with whatever app is in foreground
# Note: Some apps use a property sheet and some use a window for the print
# Dialog so depending on which we need to take different actions
set process_name to displayed name of (info for (path to frontmost application))
# If needed, you can hard-code the app name by uncommenting the following:
# set process_name "Firefox"
# If we hard-code the name, we should active the app
# activate application process_name
tell application "System Events"
tell process process_name
# Open the print dialog
keystroke "p" using command down
# Wait until the Print dialog opens before proceeding
set prdlg to 0 # Initialize loop variable
set lcnt to 0 # Counter to prevent infinit loops
repeat until prdlg is not 0
# Determine if the app is using a dialog or a sheet for print options and then create reference to dialog or sheet for use later
if exists window "Print" then
# Current App uses the Print dialog (not sheet)
set prdlg to 1
set prdlgref to a reference to window "Print"
set wtype to "dlg"
else if exists sheet 1 of window 1 then
# Current App uses the Print sheet (not dialog)
set prdlg to 1
set prdlgref to a reference to sheet 1 of window 1
set wtype to "sht"
end if
set lcnt to lcnt + 1
if (lcnt) > maxloops then
display dialog "Reached Max Loops waiting for print dialog to open."
return
end if
end repeat
# Expand the "PDF" menu button (must be expanded before the menu is referencable)
click menu button "PDF" of prdlgref
# Wait until the Menu button menu is created before proceeding
set lcnt to 0
repeat until exists menu item "Save as PDF…" of menu 1 of menu button "PDF" of prdlgref
set lcnt to lcnt + 1
if (lcnt) > maxloops then
display dialog "Reached Max Loops waiting for Save as PDF dropdown to open."
return
end if
end repeat
# Select the "Save as PDF" menu item
click menu item "Save as PDF…" of menu 1 of menu button "PDF" of prdlgref
# Wait until the Save dialog opens before proceeding
set lcnt to 0
repeat until exists window "Save"
set lcnt to lcnt + 1
if (lcnt) > maxloops then
display dialog "Reached Max Loops waiting for save dialog to open."
return
end if
end repeat
end tell
end tell
|
|
|
Mar 19th, 2008, 09:57 AM
|
#14
|
|
New Neighbour
Join Date: Mar 2008
Posts: 2
|
Hi Steve,
I tried this code of yours but got the error "System Events got an error: NSReceiverEvaluationScriptError: 4" at instruction "click menu button "PDF" of prdlgref". That's about the same error I got before finding your script. What I ended up doing was to do a lot of "keystroke"s commands as clicking that elusive button wasn't possible.
I'm using too firefox and MacOSX 10.4.11 and enabled the check "Enable access for assistive devices". Sounds like this is a tricky button.
|
|
|
Mar 19th, 2008, 03:33 PM
|
#15
|
|
New Neighbour
Join Date: Mar 2008
Posts: 4
|
Alternative Solution
Make sure that the text of the button you pasted in has ellipses and not just three dots. Apparently you need to match the character exactly.
I learned of an alternative solution recently. Here is what I was told:
|
Quote: |
|
|
|
|
The latest issue of Macworld shows how to assign a keyboard shortcut to items in the PDF popup if you are running Leopard. Basically, you add one for all applications in the keyboard preference, and make sure to list the item name as it appears in the popup. |
|
|
|
|
I ended up creating a keyboard shortcut to "Save as PDF..." to Cmd-P in the Apple Keyboard Prefs panel. So then I just need to type Cmd-P twice and I get the Save as PDF dialog
|
|
|
Mar 26th, 2008, 12:58 PM
|
#16
|
|
New Neighbour
Join Date: Mar 2008
Posts: 2
|
Yep, I made sure using ellipses and all. I also tried three dots and opt-semicolon, uppercase, lowercase, several letters, restarted my mac, restarted Firefox only, and not one worked from Applescript. Even if I do it myself, I have to position onto the PDF button (tab-ing all the way) and then hit the space bar and then cmd-p works. Also, I tried using UIElementInspector but couldn't decipher the correct clicking sequence.
Maybe is my Applescript version (1.10.7) or the printing version that doesn't understands Applescript very well.
|
|
|
Mar 26th, 2008, 10:29 PM
|
#17
|
|
Honourable Citizen
Join Date: Nov 2006
Posts: 10,180
|
|
Quote: |
|
|
Originally Posted by Calgary Guru  |
|
|
|
|
For a friend, I'm looking for the for or five lines of Applescript needed to tell a program like SAFARI to take the current window and "Print to PDF" using the standard PRINT procedure. Should be just a few lines of Applescript hopefully. |
|
|
|
|
Have you tried macosxhints.com - OS X tips and tricks!
|
Quote: |
|
|
Originally Posted by Calgary Guru  |
|
|
|
|
Oh, and why is there a "RECORD" button in the Script Editor for Applescript but it doesn't record anything! How useless is that! |
|
|
|
|
Hey it used to (and still does) work in OS 9. As far as I can recall it has never worked in OS X. Like the Macros in AppleWorks it is one of those things the Apple wizards didn't want to be bothered with.
__________________
Do NOT Touch this computer!!! Touching this computer WILL cause irreversible brain damage.
It's Un-Canadian to oppose a warming trend.
|
|
|
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -4. The time now is 12:10 AM.
|
|