How to let AppleScript choose any type of file

applescript

I'm making a script that makes encrypting files easier and at the beginning portion I have this:

display dialog "Choose where your file is located."
set directory to POSIX path of (choose folder with prompt "File Location:" default location (path to desktop))
display dialog "Choose your file."
choose file with prompt "File Name:"
set filepath to result

When it asking for a file, is there any other way that I can make it be able to choose anything? (Folders, images…)

Best Answer

Can't be done

The following are different ways to prompt for files. There is no way, in pure Apple Script, to prompt for Files or Folders at the same time.

Select File:

set directory to POSIX path of (choose file with prompt "File Location:" default location (path to desktop))

Select Files:

set directory to POSIX path of (choose file with prompt "File Location:" default location (path to desktop) with multiple selections allowed)

Select Folder:

set directory to POSIX path of (choose folder with prompt "File Location:" default location (path to desktop))

Select Folders:

set directory to POSIX path of (choose folder with prompt "File Location:" default location (path to desktop) with multiple selections allowed)

Select Specific Types of Files:

Prompting for a Specific Type of File

If your script requires specific types of files for processing, you can use the choose file command’s optional of type parameter to provide a list of acceptable types. Types may be specified as extension strings without the leading period (such as "jpg" or "png") or as uniform type identifiers (such as "public.image" or "com.apple.iwork.pages.sffpages"). Listing 26-3 and Listing 26-4 show how to prompt for an image.

For Images:

set directory to POSIX path of (choose file of type {"public.image"} with prompt "File Location:" default location (path to desktop) with multiple selections allowed)

Sources: Mac Automation Scripting Guide


Using AppleScriptObjC you can ask for either Files or Folders. See this answer (if the linked answer helped, please upvote the linked Q&A):

No, you can't do it with "choose file" or "choose folder" verbs, but choosing a file or folder (or multiple files/folders) is supported by the underlying NSOpenPanel. So you can do it with AppleScriptObjC. Here's an example using [ASObjCRunner][1] (derived from [here][2]):

script chooseFilesOrFolders
  tell current application's NSOpenPanel's openPanel()
      setTitle_("Choose Files or Folders") -- window title, default is "Open"
      setPrompt_("Choose") -- button name, default is "Open"

      setCanChooseFiles_(true)
      setCanChooseDirectories_(true)
      setAllowsMultipleSelection_(true) -- remove if you only want a single file/folder

      get its runModal() as integer -- show the panel
      if result is current application's NSFileHandlingPanelCancelButton then error number -128 -- cancelled
      return URLs() as list
  end tell
end script

tell application "ASObjC Runner"
  activate
  run the script {chooseFilesOrFolders} with response
end tell

ASObjCRunner converts a NSArray of NSURL objects into an AppleScript list of files; the results can look something like:

{file "Macintosh HD:Users:nicholas:Desktop:fontconfig:", file "Macintosh HD:Users:nicholas:Desktop:form.pdf"}

[1]: http://www.macosxautomation.com/applescript/apps/runner.html
[2]: https://stackoverflow.com/questions/8125563/


You could use a dialog box prompting a user to select what they would like to encrypt.

display dialog "Select a type to Encrypt" buttons {"File(s)", "Folder(s)"}
set a to the button returned of the result
if a is "File(s)" then
    set directory to POSIX path of (choose file with prompt "File Location:" default location (path to desktop) with multiple selections allowed)
else
    set directory to POSIX path of (choose folder with prompt "File Location:" default location (path to desktop) with multiple selections allowed)
end if

Lastly, check out my answer here for a way to encrypt files with Apple Script and Automator.