6

I'm trying to work with the script below and the repeat until exists window "Print"loop never returns true (I never hear the beep 3). Is this the correct way to wait for a window to appear?

I am using the Accessibility Inspector and this is the correct name of the print dialog window.

# Saves current document open in EverNote as PDF
#
activate application "Evernote"
tell application "System Events"
    tell process "EverNote"
        # Open the print dialog
        beep 1
        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)
        beep 3

        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
7

I seem to have fixed the main problem of the "waiting for window" issue. Apparently nested "tells" are not so good - so I solved this by specifying the process which owns a particular UI element:

repeat until window "Print" of process "Evernote" exists

Haven't solved putting the current date into the filename field of the "Save" sheet yet but tomorrow is another day! Here's the full script:

activate application "Evernote"
tell application "System Events"

    # Open the print dialog
    keystroke "p" using command down

    # Wait until the Print dialog opens before proceeding
    repeat until window "Print" of process "Evernote" exists
    end repeat

    click menu button "PDF" of window "Print" of process "Evernote"

    # 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" of process "Evernote"
    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" of process "Evernote"

    # Wait until the Save dialog opens before proceeding
    repeat until exists window "Save" of process "Evernote"
    end repeat

    set theDate to current date

    #tell (current date) to get (it's month as integer) & "-" & day & "-" & (it's year as integer)
    #set the clipboard to result as text

    #set myDate to result as text
    #set the clipboard to "dog" as text
    #if (get (the clipboard) is not "") then
    #set value of text field 1 of sheet "Save" of process "Evernote" to get (the clipboard) & ".pdf"
    #end if

    set value of text field of sheet "Save" of process "Evernote" to "dog" & ".pdf"
    # 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

    # click button "Save" of window "Save" of process "Evernote"


end tell
  • I have also used tell application "System Events" to tell process "MyProcess" ... blah with some success. – Olie Oct 13 '14 at 21:28
  • Does repeat until window "Print" of process "Evernote" exists really work for you? When I try exact that line I get a compilation error pointing at " before Evernote "Expected end of line but found “"”." – d-b Feb 26 '17 at 1:42
  • @d-b – was a couple of years ago so I don't really recall – but I think, yeah, it did work. – No Grabbing Feb 26 '17 at 1:48

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .