Search code examples
macosfileapplescriptphotoshopfinder

Applescript to Save File as Two Separate File Formats with Fixed Extensions


I often need to save a screenshot of an art file as two different files. The first would be a PDF with a "_prv" at the end of the file name. The second would be a JPG with an "_thm" file name.

So, examples of file names would be:

1733419_prv.pdf
1733419_thm.jpg

The "1733419" part would be job-specific, and it would be nice if the script would prompt the user the enter that information. It would also be nice if the script could prompt the user for the location that the new files should be saved to.

Is this all possible as an applescript or possibly a Photoshop script?

Thank you so much for any help! Bryan


Solution

  • Updated Answer

    I have improved the script a little. It will now do nothing and exit gracefully if you select Cancel when entering the Job Reference number. It will also now handle spaces in the Job Reference number. It also creates an output directory on your Desktop, so if you enter a Job Reference number of 1234 you will see a directory (folder) called 1234 appear on your Desktop which contains the 2 output files.

    #!/bin/bash
    
    # Ask user for job reference, store in $ref
    ref=$(osascript -e 'Tell application "System Events" to display dialog "Enter Job Reference:" default answer ""' -e 'text returned of result' 2>/dev/null)
    
    # Quit without doing anything if user didn't enter anything
    [ -z "$ref" ] && exit
    
    # Make a place for the output files
    DIRECTORY="$HOME/Desktop/$ref"
    mkdir "$DIRECTORY" 2> /dev/null   # Don't worry if it already exists
    cd "$DIRECTORY"
    
    # Get user to select area to grab, then save as JPEG
    screencapture -i -t jpg "${ref}_thm.jpg"
    
    # Convert JPEG to PDF
    sips -s format pdf "${ref}_thm.jpg" --out "${ref}_prv.pdf" > /dev/null 2>&1
    

    Original Answer

    I hope this is close to what you mean. Save it in your HOME directory as grab, then start Terminal and type:

    chmod +x grab
    

    which will make the file executable.

    #!/bin/bash
    
    # Ask user for job reference, store in $ref
    ref=$(osascript -e 'Tell application "System Events" to display dialog "Enter Job Reference:" default answer ""' -e 'text returned of result' 2>/dev/null)
    
    # Get user to click on a window to grab, then save as JPG
    screencapture -w -i -t jpg ${ref}_thm.jpg
    
    # Convert JPG to PDF
    sips -s format pdf ${ref}_thm.jpg --out ${ref}_prv.pdf > /dev/null 2>&1
    

    You can now either type:

    ./grab
    

    or double-click on the file called grab in the Finder.

    It will ask you for a job reference and then start the screen grab. You should click on a window to tell it which one you mean. It will then create the two files you asked for.

    The advantage of this method is that no extra software is required to be installed - such as ImageMagick or anything since it uses built-in OSX tools only.