• 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:52:26 +1100

The other reason you might want to use an open/save panel is the ability to add extra stuff to it. This is called an accessory view -- an area at the bottom where you can add extra controls, as you see in lots of applications' open and save dialogs. This is an example of how to create an open panel with an accessory view containing a single checkbox. You can of course make more complex accessory views, but this shows the principle.

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.

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

-- create a view
set theView to current application's NSView's alloc()'s initWithFrame:(current application's NSMakeRect(0, 0, 400, 40))
-- create a button
set theButton to current application's NSButton's alloc()'s initWithFrame:(current application's NSMakeRect(100, 10, 150, 20))
-- make button a checkbox and give it a title
theButton's setButtonType:(current application's NSSwitchButton)
theButton's setTitle:"I am a checkbox"
-- add the checkbox to the view
theView's addSubview:theButton

-- make open 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 setAllowsMultipleSelection:true -- AS's multiple selections allowed
its setAllowedFileTypes:{"txt"} -- AS's of type. Provide missing value for all types


-- add the view to the panel
its setAccessoryView:theView
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
set theState to theButton's state() as boolean
return {thePosixPaths, theTags, theState}

You can get more complex -- for example, a control in the accessory view can call a handler in your script while the dialog is still showing. Add these two lines after setting theButton's title:

theButton's setTarget:me
theButton's setAction:"checkboxChecked:"

That tells the checkbox that when it's clicked it should call a handler named "on checkboxChecked:" that belongs to me (the script). Now add a handler of that name at the end:

on checkboxChecked:sender
set theState to sender's state() as boolean
if theState then
display dialog "The checkbox is now checked"
else
display dialog "The checkbox is now unchecked"
end if
end checkboxChecked:

Clicking the checkbox should now display a dialog.

Just remember to run it in the foreground.

-- 
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>)
 >Re: Choosing files (From: Shane Stanley <email@hidden>)

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