NSOpen Panels

Hey everyone,

I just went to the Mavericks beta and now I am having issues with my open panel…

(old Code)


    on showModalOpen_()
        set myFile to {}
        set thePanel to current application's NSOpenPanel's openPanel()
        set myDirectoryPath to current application's |NSURL|'s fileURLWithPath_("/Volumes/images")
        tell thePanel
            setMessage_("Open File:")
            --            setAllowedFileTypes_({"tif"})
            setTitle_("My Open Panel")
            setShowsHiddenFiles_(false)
            setDirectoryURL_(myDirectoryPath)
            setCanChooseFiles_(true)
            setCanChooseDirectories_(true)
            setAllowsMultipleSelection_(true)
            set returnCode to runModal()
        end tell
        set returnCode to returnCode as integer
        if returnCode = (current application's NSFileHandlingPanelOKButton) as integer then
            set theURLs to thePanel's URLs() as list
            set progressCount to count of theURLs
            repeat with i from 1 to count of theURLs
                tell item i of theURLs to set thePosixPath to |path|()
                log thePosixPath
                set hfsPath to (((thePosixPath as text) as POSIX file) as text)
                log hfsPath
                set end of myFile to hfsPath
                log myFile
            end repeat
            else
            log "Cancel pressed"
            error -128
        end if
    end showModalOpen_

With this code I am now getting “Can’t get path of file “volume:2015_03 Fall:Documents:nameOfFile.xlsx”. (error -1728)”

I have looked on here and found the coding that Shane Stanley had put up in a post, but still have issues.

(new Code)


    on showModalOpen_()
        if not (current application's NSThread's isMainThread()) as boolean then
            display alert "This script must be run from the main thread." buttons {"Cancel"} as critical
            error number -128
        end if
        
        -- 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 class "NSURL"'s fileURLWithPath:(POSIX path of (path to desktop))) -- AS's default directory
            its setAllowsMultipleSelection:true -- AS's multiple selections allowed
            its setAllowedFileTypes:missing value -- 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 theCount to (count thePosixPaths)
        log theCount
    end showModalOpen_

The problem I have here is that it only opens 1 file, even though I selected multiple files. I added “theCount” to see how many files it listed but there is only one. I also tried combining the two scripts, but failed at that.

I also have a feeling that my NSSave Panel is going to have issues also. I have found that it no longer likes the following code for my progress bar.


my performSelectorOnMainThread_withObject_waitUntilDone_("incrementMyProgress:", "Processing image " & theCounter & " of " & progressCount, true)

Can anyone help, Please…

Do you mean the El Capitan beta?

So in El Capitan, AppleScript aliases and files are bridged with URLs. This mean that when you coerce an array of NSURLs to a list, the URLs also get converted to files («class furl»). This explains your error:

If you want your script also to work with pre-10.11 systems, the solution is keep it as an array. So then you deal with it like this:

set progressCount to theURLs's |count|()
repeat with i from 1 to progressCount
set oneURL to theURLs's objectAtIndex:(i-1)
set thePosixPath to oneURL 's |path|()

If it’s 10.11 only, you continue using a list, but knowing it’s a list of files, so you can get HFS paths using “as text”.

That should be:


        set thePosixPaths to (openPanel's URLs()'s valueForKey:"path") as list

Thanks Shane. Yes I meant El Capitan… it was a long day yesterday…

So I have incorporated the code you suggested and I get the following error:
"file “…blah”, file “…blah” doesn’t understand the “count” message. (error -1708)

Hopefully I incorporated it correctly!

Here is the code that i am using now…


    property myFile: missing value
    property theButton: missing value
    property finalLocation : missing value
    property theCounter: 0
    property statusMessage: "Click a button to begin..."
    property progressCount: missing value
    property theWindow: missing value
    property myProgressBar : missing value -- connected to the progress bar
	property myTextField : missing value -- connected to the text field
    property myTextFieldTwo : "" -- connected to the bottom text field
    property totalLayers: missing value
    property shouldStop: false
    property button_pressed: missing value
    property text_typed: missing value
    property thePosixPath: missing value
    property theURLs: missing value
    
    
    on showModalOpen_()
        set myFile to {}
        set thePanel to current application's NSOpenPanel's openPanel()
        set myDirectoryPath to current application's |NSURL|'s fileURLWithPath_("/Volumes/images")
        tell thePanel
            setMessage_("Open File:")
            -- setAllowedFileTypes_({"tif"})
            setTitle_("My Open Panel")
            setShowsHiddenFiles_(false)
            setDirectoryURL_(myDirectoryPath)
            setCanChooseFiles_(true)
            setCanChooseDirectories_(true)
            setAllowsMultipleSelection_(true)
            set returnCode to runModal()
        end tell
        set returnCode to returnCode as integer
        if returnCode = (current application's NSFileHandlingPanelOKButton) as integer then
            set theURLs to thePanel's URLs() as list
            set progressCount to theURLs's |count|()
            repeat with i from 1 to progressCount
                set oneURL to theURLs's objectAtIndex:(i-1)
                set thePosixPath to oneURL 's |path|()
                log "my File name is: " & thePosixPath
                set hfsPath to (((thePosixPath as text) as POSIX file) as text)
                log hfsPath
                set end of myFile to hfsPath
                log myFile
            end repeat
            else
            log "Cancel pressed"
            error -128
        end if
    end showModalOpen_

No – you mustn’t coerce it to a list. That’s what’s converting the URLs to files. Delete the as list from this line.

Thanks Shane, that fixed it… I guess i need to purchase your updated book, I have AppleScriptOBjc Expored 1-3, but I will have get #5.

One last question if you don’t mind…

I have this so that you can select files or folders, how can I have the script determine if it’s a folder to get the urls for the files inside the folder to put in my list. Right now I have to select all the files in the folder to process.

Thanks again for your help.

Something like:

set {theResult, isDirectory} to theNSURL's getResourceValue:(reference) forKey:(current application's NSURLIsDirectoryKey) |error|:(missing value)
set {theResult, isPackage} to theNSURL's getResourceValue:(reference) forKey:(current application's NSURLIsPackageKey) |error|:(missing value)
if isDirectory as boolean and (not isPackage as boolean) then