Applescript: Batch save files in Preview without save dialog
NickName:David Petty Ask DateTime:2016-02-22T20:26:37

Applescript: Batch save files in Preview without save dialog

thanks in advance for your help.

New to Applescript. Trying to open in Preview all files in folder and save them. The problem is that saving pops up a dialog half the time, which requires me to sit there hitting enter.

Code:

tell application "Finder"
    set fl to files of folder POSIX file "/Users/myname/Desktop/myfolder/" as alias list
end tell

repeat with f in fl
    tell application "Preview"
        activate  
        open f

        # Trying to save before the window has appeared will fail.
        # Note: - This assumes that NO window was initially open.
        #  - The code should be made more robust to eventually time out.

        repeat until (count of windows) > 0
            delay 0.3
        end repeat
        save front document
        close front document
    end tell
end repeat

Thank you for the help

Copyright Notice:Content Author:「David Petty」,Reproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/35553657/applescript-batch-save-files-in-preview-without-save-dialog

Answers
pbell 2016-02-23T20:46:14

I made several PDF files and download from different sites and I get sample of version 1.3, 1.4, 1.5, 1.7, ..but no 1.6 ! For all of them no issue.\n\nAnyway, because I have not been able to reproduce what you have, I took different approach. \n\n1) I have added at top of the script, a list of encoding/version list which may required special treatment (like hit return key). You can of course amend these 2 lists to manage other cases you may have.\n\n2) I changed your script to allow the script to get encoding and version values of the pdf. This is done by using spotlight data base via shell command 'mdls'. I use it 2 times to get version and encoding characteristics. The shell command returns characters before and after the value we want to get, so I use text x thru Y to extract the encoding and the version itself.\n\n3) if PDF version/encoding are in the list predefined which requires special treatment, then I set OKReturn to true.\n\n4) Just after the save instruction, the script now test if OKReturn is true. then I ask the script to hit return key for you. you may have to adjust this part, for instance, it could be not only 1 return, but 2 or something else. this is something I have not been able to test, because all my pdf are working. please keep in mind that because I simulate return key, you should not use the keyboard during the script run.\n\nbased on my test, I don't think the encoding is the blocking criteria. I think the version 1.6 is.Here is the script. It includes comment to make you able to adjust it:\n\nset CodingReturn to {\"Mac OS X 10.7.3 Quartz PDFContext\"}\n set VersionReturn to {\"1.6\"}\n\nset myFolder to choose folder\ntell application \"Finder\"\nset fl to files of myFolder as alias list\nend tell\n\nrepeat with f in fl\nset FVersion to do shell script \"mdls -name kMDItemVersion \" & quoted form of POSIX path of f\nset FEncoding to do shell script \"mdls -name kMDItemEncodingApplications \" & quoted form of POSIX path of f\nif (length of FVersion) > 21 then set FVersion to text 19 thru -2 of FVersion -- extract only version number\nif (length of FEncoding) > 42 then set FEncoding to text 38 thru -4 of FEncoding -- extract only the coding\n\nset OKReturn to ((FVersion is in VersionReturn) and (FEncoding is in CodingReturn)) -- special treatment true/false\n\ntell application \"Preview\"\n activate\n open f\n\n repeat until (count of windows) > 0\n delay 0.3\n end repeat\n save front document\n\n if OKReturn then -- we need special key to be pressed\n tell application \"System Events\" to keystroke return\n end if\n\n close front document\nend tell\nend repeat\n\n\nI would be very interested to get your feedback about this version.",


More about “Applescript: Batch save files in Preview without save dialog” related questions

Applescript: Batch save files in Preview without save dialog

thanks in advance for your help. New to Applescript. Trying to open in Preview all files in folder and save them. The problem is that saving pops up a dialog half the time, which requires me to sit

Show Detail

AppleScript to Save Preview Document

I'm using a third party application to copy an image to the clipboard. I would then like to perform an AppleScript that opens Preview, creates a New Document which will then use the clipboard as the

Show Detail

Applescript Change To Folder Save Dialog

I'm trying to automate opening and saving a file in applescript. I can't seem to get consistent results with the save dialog though. Is it possible to change a save dialog to a specific folder in

Show Detail

AppleScript "Save As" dialog?

What is the AppleScript code for a "Save As" dialog with a custom file type extension? I know the code for Open dialog: set theFiles to (choose file of type {"set"} with prompt "Save As File" wit...

Show Detail

Applescript to Control Photoshop Save Dialog Box

How are you? I've created an applescript automation to save files in .JPG while the Save Dialog Box is open. (So I can control the name of the saved files) Is there a way to control the Save Dial...

Show Detail

How can I batch save a bunch of AppleScript scpt files to applications?

I have a bunch of AppleScript scripts in a directory that I want to be able to build as apps without having to go into each one and manually save as application. I've already created a "build scri...

Show Detail

Trying to open, modify, and save files in Quicktime with AppleScript

I am unsuccessfully trying to use AppleScript to automate the process of making small edits to a bunch of files. More specifically, I want a script that will: Open a specific file in QuickTime Spl...

Show Detail

Applescript: Pasting Clipboard Text into Open/Save Dialog Box

I have many untitled TextEdit files. I'd like to use applescript to save each using, as a name, the text of the top line of each document. The following will select and copy the first line of a

Show Detail

How to use AppleScript to enter a filename in Firefox Save As dialog?

I am relatively new to AppleScript and I'm trying to find a way to programmatically enter a filename into a Save as PDF... dialog in Firefox. I've gotten this far from searching the web. I've tur...

Show Detail

AppleScript to save message into eml file in Outlook 15 for Mac

The following AppleScript line is giving an error in Microsoft Outlook 15 for Mac (available from Office 365 or Office 2016 Preview). It works fine in Outlook 2011 for Mac. Do you know the AppleS...

Show Detail