apple scripting for Preview in Mojave

iMac (Retina 5K, 27-inch, Late 2015); Processor 4GHz Intel Core I7; Memory 16GB 1867 Mhz DDR3; Startup Disk iTB Fusion; MacOS Mojave (10.14)

I had previously had run an Apple Script on the Preview Application running OS 10.12. That script doesn't run on 10.14. In fact, when I open the Script Editor in the Utilities Folder and look for applications that it will work with Preview isn't listed. Are there plans to add Preview scripting features? I have a desperate need to use Preview to convert PDF files to JPEG files after opening the PDF file from the Print Directory.

I used the following Apple Script in OS 10.12.


-- Click the “File” menu.

delay 0.15

set timeoutSeconds to 2.000000

set uiScript to "click menu bar item \"File\" of menu bar 1 of application process \"Preview\""

my doWithTimeout( uiScript, timeoutSeconds )



-- Export…

delay 0.15

set timeoutSeconds to 2.000000

set uiScript to "click menu item 13 of menu 1 of menu bar item \"File\" of menu bar 1 of application process \"Preview\""

my doWithTimeout( uiScript, timeoutSeconds )



-- Press ⌘V

delay 0.15

set timeoutSeconds to 2.000000

set uiScript to "keystroke \"v\" using command down"

my doWithTimeout( uiScript, timeoutSeconds )



-- Make a selection from the popupbutton.

delay 0.15

set timeoutSeconds to 2.000000

set uiScript to "click pop up button 1 of group 2 of sheet 1 of window 1 of application process \"Preview\""

my doWithTimeout( uiScript, timeoutSeconds )



-- PDSC Picture Folder

delay 0.15

set timeoutSeconds to 2.000000

set uiScript to "click menu item \"PDSC Picture Folder\" of menu 1 of pop up button 1 of group 2 of sheet 1 of window 1 of application process \"Preview\""

my doWithTimeout( uiScript, timeoutSeconds )



-- Make a selection from the popupbutton.

delay 0.15

set timeoutSeconds to 2.000000

set uiScript to "click pop up button 1 of group 1 of sheet 1 of window 1 of application process \"Preview\""

my doWithTimeout( uiScript, timeoutSeconds )



-- JPEG

delay 0.15

set timeoutSeconds to 2.000000

set uiScript to "click menu item \"JPEG\" of menu 1 of pop up button 1 of group 1 of sheet 1 of window 1 of application process \"Preview\""

my doWithTimeout( uiScript, timeoutSeconds )



-- Click the “Save” button.

delay 0.15

set timeoutSeconds to 2.000000

set uiScript to "click UI Element \"Save\" of sheet 1 of window 1 of application process \"Preview\""

my doWithTimeout( uiScript, timeoutSeconds )



-- Press ⌘W

delay 0.5

set timeoutSeconds to 2.000000

set uiScript to "keystroke \"w\" using command down"

my doWithTimeout( uiScript, timeoutSeconds )



on doWithTimeout(uiScript, timeoutSeconds)

set endDate to (current date) + timeoutSeconds

repeat

try

run script "tell application \"System Events\"

" & uiScript & "

end tell"

exit repeat

on error errorMessage

if ((current date) > endDate) then

error "Can not " & uiScript

end if

end try

end repeat

end doWithTimeout

iMac, OS X Yosemite (10.10.2), I also have a MBP & Mac Mini

Posted on Oct 7, 2018 7:41 PM

Reply
9 replies

Oct 9, 2018 9:17 AM in response to raharris40

GUI scripting of any application can break between operating system releases when Apple changes the arrangement, or removes certain Cocoa interface elements within the application. You could be faced with a complete rewrite of your GUI scripting solution depending on how extensive the application changes have been.


The real AppleScript support for Preview is quite limited, as is shown in Script Editor : File menu : Open Dictionary… Preview.

Oct 9, 2018 9:56 AM in response to raharris40

You should avoid using item numbers in UI scripting. While using the name of the menu item can also break, it is less likely.


However, you are still going to have to manually run through each section and see if the items you are trying to control are still in the same place. UI Scripting is almost impossible without a tool that can tell you what each item is called. I remember using one long ago when UI Scripting was introduced, but I can't remember the name.

Oct 9, 2018 9:16 AM in response to Barney-15E

Thank you for the reply. I should have mentioned that the above script is running in Filemaker Pro 16 Advanced and that I have given FM permission to control my computer. Before the script runs I have assigned the task to "Preview". This script ran fine under Sierra, but doesn't run after I upgraded to Mojave. At least one thing changed in Preview. The file menu item # of the "Export" changed from 13 to 22. When I corrected this matter it just moved into the second window of Preview and nothing further happened. Again, when I check to see if Preview is controllable looking from the Script Editor in the Utilities Folder and look for applications that it will work with Preview isn't listed; however, I'm not sure that this is relevant!

Oct 7, 2018 7:57 PM in response to raharris40

There is nothing in that script that depends on Preview being scriptable. It is all User Interface (UI) Scripting. You are just telling the system to find the listed items in Preview and activate them.

You need to determine if all of the Menus, Menu items, buttons, etc. still match what the script is directed to activate.

Things may have changed in the Preview UI. For instance, popup button 1 may not be on group 2 of sheet in "-- Make a selection from the popupbutton."

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

apple scripting for Preview in Mojave

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.