AppleScript and NSOpenPanel in Lion

Discussion in 'Mac Programming' started by Touchstone64, Sep 22, 2011.

  1. Touchstone64 macrumors regular

    Joined:
    May 10, 2010
    Location:
    Manchester, UK
    #1
    Hi folks,

    I'm 'porting' some of my Snow Leopard scripts to Lion using the AppleScript Editor and I'm having significant problems getting NSOpenPanel's openPanel() working. In my original script I used code along the lines of:

    Code:
    tell application "Automator Runner" to tell «class opeP»
    		
    	get it's properties
    
    	set floating to true
    	set «class caCD» to true
    	set «class alMT» to true
    	set «class titl» to "Select the items to Matroskify"
    	
    	«event appScalM» "setShowsHiddenFiles:" given «class psof»:it, «class witP»:{false}
    
    	activate
    	if («event panSdisP» it) is not 0 then -- cancel button
    		--the path names property is a list of POSIX paths
    		set posixPaths to it's «class filO»
    	end if
    
    end tell
    In Lion I got errors regarding the use of «class caCD», so I dug around a bit and ended up with:

    Code:
    set myOpenPanel to current application's NSOpenPanel's openPanel()
    myOpenPanel's setCanChooseFiles_(true)
    myOpenPanel's setCanChooseDirectories_(true)
    myOpenPanel's setAllowsMultipleSelection_(false)
    myOpenPanel's runModal -- returns a list of file URL's
    set posixPaths to myOpenPanel's URLs()
    While this compiles, I get 'NSOpenPanel doesn’t understand the openPanel message' when I wrap it in an on run ... end run block and run it.

    Could anyone enlighten me as to which stupid mistake I've made this time?

    Thanks in advance...
     
  2. Red Menace macrumors 6502

    Red Menace

    Joined:
    May 29, 2011
    Location:
    Littleton, Colorado, USA
    #2
    You can't run Cocoa AppleScripts directly in the AppleScript Editor - there is a Cocoa-AppleScript applet template that you can create from the Editor that will run your code, although debugging is a bit of a pain. Another option is to use the AppleScriptObjC Explorer.
     
  3. Touchstone64 thread starter macrumors regular

    Joined:
    May 10, 2010
    Location:
    Manchester, UK
    #3
    Thanks Red Menace; for info I get the same behaviour at run-time (with the script embedded in a package).

    Presumably this was a deliberate change in Lion. Or was the Snow Leopard behaviour 'unorthodox', and Apple shut the door in Lion's AppleScript engine?
     
  4. Red Menace macrumors 6502

    Red Menace

    Joined:
    May 29, 2011
    Location:
    Littleton, Colorado, USA
    #4
    Your script as posted works for me, so the issue is most likely a later use of one of the items in the returned URLs (and just like ASOC in Xcode, an untrapped error will abort the rest of the handler). Remember that Cocoa methods return Cocoa objects, so the returned URLs need to be coerced to a list, and the individual items need be coerced to text in order to use them with AppleScript - for example:

    Code:
    set posixPaths to myOpenPanel's URLs() as list -- coerce the result
    repeat with X from 1 to (count posixPaths) -- coerce the file paths in place
    	set (item X of posixPaths) to (item X of posixPaths)'s |path|() as text
    end repeat
     
  5. Touchstone64, Sep 24, 2011
    Last edited: Sep 24, 2011

    Touchstone64 thread starter macrumors regular

    Joined:
    May 10, 2010
    Location:
    Manchester, UK
    #5
    The plot thickens. If I create a new AppleScript in the Editor (not using a template), and paste that code into an on run/end run block, I get the same error message (attached).

    I'm working on a fresh install of Lion, not more than a few weeks old. I've not installed Xcode yet as I'm waiting for the full iOS 5 'version'.

    Any ideas? Thanks for sticking with me... :)
     

    Attached Files:

  6. Red Menace macrumors 6502

    Red Menace

    Joined:
    May 29, 2011
    Location:
    Littleton, Colorado, USA
    #6
    All of the old AppleScript Studio terminology has been removed in Lion, so you can't use Automator Runner for Cocoa methods in your AppleScripts like you could before. You also can't run Cocoa AppleScripts directly in the AppleScript Editor, since there isn't support for that either - you will need to create a new Cocoa-AppleScript Applet using the File > New from Template menu, and insert your code there. Since this is a Cocoa wrapper application for your script, the way to change the script is to edit the script resource in the application bundle, save it and run the application again. There won't be any result or event log like in the AppleScript Editor, so you will need to use some dialogs to do any debugging.

    I'm not sure why Apple has done it it this way, but editing AppleScripts in Xcode 4 is also a bit of a kluge - Shane Stanley's AppleScript Explorer is designed to fit in the middle of this mess, and will run your script snippets from its editor with logging. Hopefully they will run into each other one day and get their chocolate and peanut butter mixed up.
     

Share This Page