user3012084
user3012084

Reputation: 11

How can I create a display dialog that presents specific file path options for a given PDF file?

I am very new to using Automator and Applescript.

I would like to use Automator and AppleScript to detect PDF files that are downloaded to the "Downloads" folder and opens a display dialog that allows me to select the file path and move the file. So far, what I have (which isn't right) is something like:

set question to display dialog "Save fileName in..." buttons {"Figuring Relation", "Iconoclasm", "Elsewhere"} default button 3

set answer to button returned of question

    if answer is equal to "Figuring Relation" then
        tell application "Finder" to move fileName to POSIX file "/Users/mac/Documents/College/Junior/Fall/Art 347 - Figuring Relation"

I want the "Figuring Relation" and "Iconoclasm" buttons to change the file path to a designated file path (I don't want to browse for it), and the "Elsewhere" button to open a Finder window where I can select/browse the path.

If possible, I'm also looking to add the date to the beginning of the file name as "mm-dd_filename".

I am not sure of how to translate the Automator Input to Applescript, or how to include the filename in the display dialog text. Thank you so much for any help.

Upvotes: 1

Views: 375

Answers (1)

ThrowBackDewd
ThrowBackDewd

Reputation: 1767

Here is an example using just applescript. In my example, it assumes you're selecting the file you're wanting to move, but you could easily add something for the script to "Find" all files ending with ".pdf" if you wanted to and then loop through the results.

on run
    try
        set thisFile to choose file
        tell application "Finder" to set currentName to thisFile's name

        -- Setting variables for the destinations to be used later
        set FiguringRelationPath to (path to documents folder) & "College:Junior:Fall:Art 347 - Figuring Relation:" as string
        set IconoclasmPath to (path to documents folder) & "Iconoclasm:" as string

        -- Ask the user
        set answer to button returned of (display dialog "Save \"" & currentName & "\" in..." buttons {"Figuring Relation", "Iconoclasm", "Elsewhere"} default button 3)

        -- Set the destination variable based on the users response to the dialog
        if answer is equal to "Figuring Relation" then
            set destination to FiguringRelationPath
        else if answer is equal to "Iconoclasm" then
            set destination to IconoclasmPath
        else
            set destination to choose folder with prompt "Please select the destination folder" as string
        end if

        -- Test that the destination directory exists, if not post the error
        try
            set destination to destination as alias
        on error
            error ("Destination path " & destination as string) & " doesn't appear to exist"
        end try

        -- Rename the file with the date prefix
        set tDatePrefix to (do shell script "date '+%m-%d'") & "_" as string
        tell application "Finder" to set x's name to tDatePrefix & x's name as string

        -- Move the file
        tell application "Finder" to move thisFile to destination

    on error err
        activate
        display dialog "Error: " & err buttons {"OK"} default button 1
    end try
end run

Upvotes: 0

Related Questions