AppleScript choose application

Posted in AppleScript on October 20, 2016 · 1 min read
How can I tell AppleScript to prompt for files and folders with choose application?

When processing or modifying files with AppleScript it is handy to prompt the file for the location or the file to modify.  Not long ago I demoed the prompt feature in the post "AppleScript current script's path" but after writing the post I realized nothing was written explaining the prompt feature. In AppleScript this is known as choose application and as mentioned by Apple it allows the user to choose an application. The syntax choose application is required but the with prompt parameter is optional. Here is a table with the sytnax options for choose application:

SyntaxTypeDefault ValueRequired
`choose application`yes
`with title`text"Choose Application"optional
`with prompt`textSelect an application:optional
`multiple selections allowed`booleanfalseoptional

Let's build a script example of the prompt:

tell application "Finder"
    set theSelection to ¬
        choose file with prompt "Choose any file"
end tell

Result:
alias "Macintosh HD:Users:username:pathLocation:fileChoosen"

Do note, when playing with the title, it appears to only work when choosing the Application:

tell application "Finder"
    choose application with title "Why no work for file?" with prompt "The app"
end tell

but when you script the selection for a file or folder:

tell application "Finder"
    choose file with title "Why no work for file?" with prompt "The app"
end tell

you are returned with Expected given, with, without other parameter name etc but found "".

if you wanted the option to select multiple files:

tell application "Finder"
    choose file with prompt "Choose something" with multiple selections allowed
end tell

The cool thing about choosing what to pass in a choose application is you can use lists:

tell application "Finder"
    choose from list {"September", "October", "November", "December"} ¬
        with title ¬
        "Favorite Months" with prompt ¬
        "Which months do you prefer?" OK button name ¬
        "my Months" cancel button name ¬
        "No Month" default items {"September"} ¬
        with multiple selections allowed
end tell

You could even set the type in the choose application:

tell application "Finder"
    choose file with prompt "Choose something" of type {"public.image"} with multiple selections allowed
end tell

and when ran, that should grew out everything except for images and directories. If you wanted to isolate to a particular file type you could write it as:

tell application "Finder"
    choose file with prompt "Choose something" of type {"public.json"} with multiple selections allowed
end tell

and the above would grey out everything that wasn't a directory or a JSON file. If you were wanting to use choose application to select the location to drop a few images after your AppleScript was complete try:

tell application "Finder"
    set theDrop to choose file with prompt "Choose something" of type {"jpg", "png"} default location (desktop as alias) with multiple selections allowed
end tell

The above AppleScript sets the variable theDrop; only highlights directories, images with the .jpg extension images and images with the .png extension; sets the default location to the Desktop; and allows the user to make multiple selections.

Help the site go ad free:
Storm Trooper