Archive for the 'NSOpenPanel' Category

2012/12/10 AppleScriptObjCでOpen Panel/Save Panelを表示

AppleScriptObjCでファイルオープンのダイアログOpen Panel、ファイル保存のSave Panelをオープンするサンプルです。

Open Panelは比較的どーーでもいいのですが、Save Panelは各種オプションを表示してユーザーからの指定を受け付けたりするので、重要。そのための習作です。

panel.png

penel2.png

拡張して、最終的にはこんな感じになりました……

panel3.png

→ Xcodeプロジェクトのダウンロード

AppleScriptObjCファイル名:AppDelegate.applescript

– AppDelegate.applescript
– saveP1

– Created by Takaaki Naganoya on 2012/12/10.
– Copyright (c) 2012年 Takaaki Naganoya. All rights reserved.

–  open, save両方のパネルが表示できている(OS X 10.8.2)

script AppDelegate
  
  property parent : class “NSObject”
  
  
  on applicationWillFinishLaunching_(aNotification)
    – Insert code here to initialize your application before any files are opened
  end applicationWillFinishLaunching_
  
  on applicationShouldTerminate_(sender)
    – Insert code here to do any housekeeping before your application quits
    
return current application’s NSTerminateNow
  end applicationShouldTerminate_
  
  
  
  
  on clicked_(sender)
    
    set aTag to tag of sender
    
set aTag to aTag as integer
    
    if aTag = 1 then
      
      –Openダイアログ
      
set filePathList to showModalOpen_()
      
log filePathList
      
    else if aTag = 2 then
      –Saveダイアログ
      
set filePathList to showModalSave_()
      
log filePathList
      
    end if
  end clicked_
  
  
  
  
  
  on showModalSave_()
    
    set theFullPath to “”
    
    set thePanel to current application’s NSSavePanel’s savePanel()
    
set productFolder to POSIX path of (path to desktop)
    
    set myDirectoryPath to current application’s |NSURL|’s fileURLWithPath_(productFolder)
    
    tell thePanel
      
      setMessage_(“Save File:”)
      
setAllowedFileTypes_({“png”})
      
      setTitle_(“My Save Panel”)
      
setShowsHiddenFiles_(false)
      
setDirectoryURL_(myDirectoryPath)
      
setCanChooseFiles_(false)
      
setCanChooseDirectories_(false)
      
setAllowsMultipleSelection_(false)
      
      set returnCode to runModal()
      
    end tell
    
    set returnCode to returnCode as integer
    
    
    
    if returnCode = (current application’s NSFileHandlingPanelOKButton) as integer then
      
      set theURL to thePanel’s directoryURL()
      
tell theURL to set thePosixPath to |path|()
      
set thePosixPath to thePosixPath as string
      
      set theName to thePanel’s nameFieldStringValue()
      
set theName to theName as string
      
      set theFullPath to thePosixPath & “/” & theName
      
set hfsPath to (((theFullPath as text) as POSIX file) as text)
      
    else
      
      –log “Cancel pressed”
      
error -128
      
    end if
    
    return hfsPath
    
  end showModalSave_
  
  
  
  –NSSavePanel Problem
  
–http://macscripter.net/viewtopic.php?id=39639
  
  on showModalOpen_()
    
    set myFile to {}
    
    set thePanel to current application’s NSOpenPanel’s openPanel()
    
set productFolder to POSIX path of (path to desktop)
    
    set myDirectoryPath to current application’s |NSURL|’s fileURLWithPath_(productFolder)
    
    tell thePanel
      
      setMessage_(“Open File:”)
      
setAllowedFileTypes_({“png”})
      
      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|()
        
set hfsPath to (((thePosixPath as text) as POSIX file) as text)
        
set end of myFile to hfsPath
        
      end repeat
      
    else
      
      –log “Cancel pressed”
      
error -128
      
    end if
    
    return myFile
    
  end showModalOpen_
  
  
  
end script

▼新規書類に ▼カーソル位置に ▼ドキュメント末尾に