• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
RE: GUI Scripting - Print/Save PDF As Postscript
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

RE: GUI Scripting - Print/Save PDF As Postscript


  • Subject: RE: GUI Scripting - Print/Save PDF As Postscript
  • From: "Scott Babcock" <email@hidden>
  • Date: Tue, 16 Aug 2005 15:15:37 -0700
  • Thread-topic: GUI Scripting - Print/Save PDF As Postscript

I'd forgotten that you have to be talking to the process itself when you
use the 'at' aurgument of the 'click' event:

tell application "TextEdit" to activate
tell application "System Events"
    set theProcess to process "TextEdit"
    set theMenuBar to menu bar 1 of theProcess
    set theFileMenu to menu "File" of menu bar item "File" of theMenuBar

    -- open the Print sheet
    click menu item ("Print" & (ASCII character 201)) of theFileMenu
    -- let the sheet draw
    delay 1

    -- get a reference to the PDF button
    set theWindow to window 1 of theProcess
    set theSheet to sheet 1 of theWindow
    set theGroup to UI element 4 of theSheet
    set theButton to UI element 6 of theGroup

    -- compute the center coordinate of the button
    set {btnLeft, btnTop} to position of theButton
    set {btnWidth, btnHeight} to size of theButton
    set clickX to btnLeft + (btnWidth div 2)
    set clickY to btnTop + (btnHeight div 2)

    -- click the button to open the menu
    tell theProcess to click at {clickX, clickY}

    -- let the menu draw
    delay 1

    -- select Save PDF as PostScript
    key code 125
    key code 125
    keystroke space
end tell

This still relies on the order of the PDF menu, but it doesn't rely on
the tab order of the Print sheet. You have to choose your poison,
though, because it does encode the structure of the sheet's UI element
hierarchy.

> -----Original Message-----
> Date: Tue, 16 Aug 2005 02:16:31 -0400
> From: Jeff Porten <email@hidden>
> Subject: Re: GUI Scripting - Print/Save PDF As Postscript
> To: AppleScript-Users AppleScript <email@hidden>,
> 	"email@hidden> <email@hidden"
> 	<email@hidden>
> Message-ID: <email@hidden>
> Content-Type: text/plain; charset=US-ASCII; delsp=yes; format=flowed
>
> Your wish is my command.  Got this working over the weekend for PDF
> printing; add an extra key code 125 to do Postscript.
>
> The evil PDF button click was solved by calculating the xy
> coordinates from the size of the panel in relation to the window.
> This works with Safari, where the window is expected to be wider than
> the panel.  If you use it with TextEdit and the panel causes the
> whole window to shift, YMMV, and I'd be interested to hear about the
> fascinating ways in which this breaks.
>
> Other clever bits:
>
> 1) the script will copy the selection to the clipboard and paste it
> in as the name of the saved file.  If you don't want that, kill the
> Command-C and Command-V lines and add your own keystrokes.
>
> 2) after the script proper is a second bit of code that can be added
> for resiliency.  I noted that sometimes the xy click doesn't take, so
> in the web parsing script where I incorporated this code, I added a
> looping test to make sure the UI keeps clicking until it finds the
> PDF submenu.
>
> This script is slated to go up on my site as part of the donationware
> offerings.  AS-U users not obligated, thanks to all of the help I get
> here, but if anyone feels so moved you won't catch me complaining.
> On the other hand, just being able to say I helped Bill Cheeseman
> with an AppleScript is a giant step on the road to dying happy.
>
> Best,
> Jeff
>
>
> tell application "Safari"
>      set winbounds to bounds of window 1
>      set winwidth to (item 3 of winbounds) - (item 1 of winbounds)
>      set xcoord to (item 1 of winbounds) + winwidth / 2 - 291 + 49 +
> 41 as integer -- 291, 1/2 of panel width; 49, x from left edge; 41,
> 1/2 of PDF button
>      set ycoord to (item 2 of winbounds) + 262
>
>      activate
>      tell application "System Events"
>          tell process "Safari"
>              keystroke "c" using command down
>              delay 0.2
>              keystroke "p" using command down
>              delay 1
>              click at {xcoord, ycoord}
>              delay 0.2
>              key code 125
>              delay 0.2
>              keystroke return
>              delay 0.2
>              keystroke "v" using command down
>          end tell
>      end tell
> end tell
>
>
> (* loop to fix problems where click to open PDF menu is missed *)
>                  set needClick to true
>                  set loopcounter to 0
>                  repeat until exists window "Save"
>                      if needClick then
>                          click at {xcoord, ycoord}
>                          delay 0.2
>                          key code 125
>                          delay 0.2
>                          keystroke return
>
>                          set needClick to false
>                      end if
>                      delay 0.2
>                      set loopcounter to loopcounter + 1
>                      if loopcounter = 5 then
>                          set needClick to true
>                          set loopcounter to 0
>                      end if
>                  end repeat
 _______________________________________________
Do not post admin requests to the list. They will be ignored.
Applescript-users mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:

This email sent to email@hidden

  • Follow-Ups:
    • Re: GUI Scripting - Print/Save PDF As Postscript
      • From: Bill Cheeseman <email@hidden>
  • Prev by Date: RE: GUI Scripting - Print/Save PDF As Postscript
  • Next by Date: script for redirecting messages in Mail
  • Previous by thread: RE: GUI Scripting - Print/Save PDF As Postscript
  • Next by thread: Re: GUI Scripting - Print/Save PDF As Postscript
  • Index(es):
    • Date
    • Thread