• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Choosing files
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Choosing files


  • Subject: Re: Choosing files
  • From: Shane Stanley <email@hidden>
  • Date: Fri, 14 Nov 2014 12:23:31 +1100

The equivalent of choose file/folder is similar, and offers two sometimes-requested facilities: the ability to let the user choose files and folders on the one dialog, and the ability to decide whether alias files are solved or passed directly.

Again, the warning:

Before you run the following code, a warning: ASObjC code that involves drawing stuff on screen, like this, has to be run in the foreground on what is known as the main thread. Scripts are nearly always run on the main thread -- the one exception being in script editors, where they are usually run on background threads. So when you are trying out this code, if you don't run it in the foreground, you will crash your editor.

In Script Editor, you run a script in the foreground by holding the control key when running it, or pressing control-command-R. You can see the Run command change names in the Script menu when you hold down the control key. In ASObjC Explorer you check the 'Run in foreground' checkbox at the bottom of the script window.

use AppleScript version "2.4"
use scripting additions
use framework "Foundation"

-- make panel
set openPanel to current application's NSOpenPanel's openPanel()
tell openPanel
-- set main values
its setMessage:"Your message here" -- AS's prompt
-- other values you *can* set
its setDirectoryURL:(current application's NSURL's fileURLWithPath:(POSIX path of (path to desktop))) -- AS's default directory
its setAllowsMultipleSelection:true -- AS's multiple selections allowed
its setAllowedFileTypes:{"txt"} -- AS's of type. Provide missing value for all types
its setShowsHiddenFiles:false -- AS's invisibles; default is false
its setTreatsFilePackagesAsDirectories:false -- AS's showing package contents; default is false
its setTitle:"Choose a File" -- Panel's title; default is "Open"
its setPrompt:"Open" -- Override name on button; default is "Open"
its setCanChooseFiles:true -- whether it's like choose file; default is true
its setCanChooseDirectories:true -- whether it's like choose folder; default is false
its setResolvesAliases:true -- whether aliases are automatically resolved; default is true
end tell
-- show panel
set returnCode to openPanel's runModal()
if returnCode is (current application's NSFileHandlingPanelCancelButton) then
error number -128
end if
-- get chosen paths and tags
set thePosixPaths to (openPanel's |URL|()'s valueForKey:"path") as list
set theTags to openPanel's tagNames()
if theTags = missing value then
set theTags to {}
else
set theTags to theTags as list
end if
return {thePosixPaths, theTags}


-- 
Shane Stanley <email@hidden>
<www.macosxautomation.com/applescript/apps/>

 _______________________________________________
Do not post admin requests to the list. They will be ignored.
AppleScript-Users mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:
Archives: http://lists.apple.com/archives/applescript-users

This email sent to email@hidden

  • Follow-Ups:
    • Re: Choosing files
      • From: Shane Stanley <email@hidden>
References: 
 >Choosing files (From: Shane Stanley <email@hidden>)

  • Prev by Date: Choosing files
  • Next by Date: Re: Choosing files
  • Previous by thread: Choosing files
  • Next by thread: Re: Choosing files
  • Index(es):
    • Date
    • Thread