0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

SavePanel for macOS

Last updated at Posted at 2022-03-19

Since allowedFileTypes is removed on macOS 12.0, you need workaround to replace the feature.

๐ŸŸฉScreenshot main:

qiita.rb
    func ExecIBScreenshot() {                                                               //<*๐Ÿ’™Screenshot*>
        let wShot:NSImage?  = macOS.CallGetScreenshot(appName, pPercent:1, pInverse:false)  //๐ŸžGet screenshot.
        if wShot == nil         { return }                                                  //
        else { }                                                                            //
        let wcgi:CGImage    = wShot!.cgImage(forProposedRect: nil, context: nil, hints: nil)!//Convert to CGImage.
        let wRep:NSBitmapImageRep   = NSBitmapImageRep(cgImage: wcgi)                       //
        let wData:NSData    = wRep.representation(using: .png, properties: [:])! as NSData  //
        var wName:String    = ""                                                            //
        if zScreenFolder == "" {                                                            //
            zScreenName     = "screenshotMMA"                                               //filename
            wName           = zScreenName                                                   //screenshotMMA for the alert.
        } else {                                                                            //
            zScreenNum          += 1                                                        //
            let wNum:String = Call.PickupLastWords(String(1000 + zScreenNum), pString: "999")//001, 002, ... 999
            wName           = zScreenName + wNum                                            //screenshotMMA999
        }                                                                                   //
        let wFolder:URL     = URL(fileURLWithPath:NSHomeDirectory()+"/Desktop")             //Save on the desktop.
        let wX              = macOS.SavePanelScreen(["png"], pMsg:cm.rcMSG[LT.msg17],       //20220318
                                pDirURL:wFolder, pCreateDir:true, pFName:wName+".png")      //Save panel
        let wA              = Call.GetFileInfo(wX.rURL, pExtentionArray: [], pSyspathArray: [])//Get file info.
        zScreenType         = "." + wA.rExtention                                           //
        let wNew:String     = Call.RemoveLastWords(wA.rName, pString: zScreenType)          //Remove the extention.
        if wNew == wName {                                                                  //
        } else {                                                                            //
            zScreenName     = wNew                                                          //Replace it to userโ€™s one.
        }                                                                                   //
        zScreenFolder       = Call.RemoveLastWords((wX.rURL).path, pString: zScreenName + zScreenType)//folder path
        Call.WriteDatafile(wX.rURL, pData: wData)                                           //Write the screenshot.
    }//๐Ÿ…ฟ๏ธ๐Ÿ…ฟ๏ธ๐Ÿ…ฟ๏ธ๐Ÿ…ฟ๏ธ๐Ÿ…ฟ๏ธ๐Ÿ…ฟ๏ธ๐Ÿ…ฟ๏ธ๐Ÿ…ฟ๏ธ๐Ÿ…ฟ๏ธ๐Ÿ…ฟ๏ธ๐Ÿ…ฟ๏ธ๐Ÿ…ฟ๏ธ๐Ÿ…ฟ๏ธ๐Ÿ…ฟ๏ธ๐Ÿ…ฟ๏ธ๐Ÿ…ฟ๏ธ๐Ÿ…ฟ๏ธ๐Ÿ…ฟ๏ธ๐Ÿ…ฟ๏ธ๐Ÿ…ฟ๏ธ๐Ÿ…ฟ๏ธ๐Ÿ…ฟ๏ธ๐Ÿ…ฟ๏ธ๐Ÿ…ฟ๏ธ๐Ÿ…ฟ๏ธ๐Ÿ…ฟ๏ธ๐Ÿ…ฟ๏ธ๐Ÿ…ฟ๏ธ๐Ÿ…ฟ๏ธ๐Ÿ…ฟ๏ธ๐Ÿ…ฟ๏ธ๐Ÿ…ฟ๏ธ๐Ÿ…ฟ๏ธ๐Ÿ…ฟ๏ธ๐Ÿ…ฟ๏ธ๐Ÿ…ฟ๏ธ๐Ÿ…ฟ๏ธ๐Ÿ…ฟ๏ธ๐Ÿ…ฟ๏ธ// 

๐ŸŸฉSavePanel for Screenshot:

qiita.rb
        func SavePanelScreen(_ pFType:[String], pMsg:String, pDirURL:URL?,  //๐ŸŽ20220318 (M19)<*NSSavePanel*>
        pCreateDir:Bool, pFName:String)-> (rURL:URL, rState:String) {   //๐ŸŽโ‘ nil: Cancelled โ‘กOK: rURL is OK.
        let wBool:Bool = false                                          //๐ŸŽ
        while wBool == false {                                          //๐ŸŽ
            let wSave                   = NSSavePanel()                 //๐ŸŽ
            wSave.nameFieldStringValue  = pFName                        //๐ŸŽ
//          if pFType.count > 0 {   wSave.allowedFileTypes   = pFType } //๐ŸŽSet Extention. โŒDesperated on macOS 12.0
            wSave.allowsOtherFileTypes  = false                         //๐ŸŽDo not allow other extentions.
            wSave.message               = pMsg                          //๐ŸŽMessaga for the panel.
            wSave.directoryURL          = pDirURL                       //๐ŸŽFolder name for the panel.
            wSave.canCreateDirectories  = pCreateDir                    //๐ŸŽA user can create a new folder.
            wSave.treatsFilePackagesAsDirectories  = false              //๐ŸŽ
            let wModal                  = wSave.runModal()              //๐ŸŽ
            if wModal.rawValue == NSApplication.ModalResponse.cancel.rawValue {//๐ŸŽ
                return (wSave.url!, rrNG)                               //๐ŸŽโ™ป๏ธreturnโ™ป๏ธ Canceled.
//          } else  { return (wSave.url!, "")  }                        //๐ŸŽโ™ป๏ธreturnโ™ป๏ธ Normal end โŒDesperated on macOS 12.0
            } else  {                                                   //๐ŸŽ20220318
                let wPath:String = wSave.url!.path + ""                 //๐ŸŽ20220318<โ€”-Use the file name + extention as is.
                return (URL(fileURLWithPath: wPath), "")                //๐ŸŽ20220318 โ™ป๏ธreturnโ™ป๏ธ Normal 
            }                                                           //๐ŸŽ
        }                                                               //๐ŸŽ
    }//-----------------------------------------------------------------//๐ŸŽ

๐ŸŸฉUsual SavePanel:

qiita.rb
    func OpenPanel(_ pFType:[String], pMulti:Bool, pDir:Bool,           //๐ŸŽ(M20)<*NSOpenPanel*>
        pFile:Bool, pCreateDir:Bool)->  [URL] {                         //๐ŸŽโ‘ nil: Cancelled โ‘กOK: rURL is OK.
        let wBool:Bool = false                                          //๐ŸŽ
        while wBool == false {                                          //๐ŸŽ
            let NSOpen = NSOpenPanel()                                  //๐ŸŽallowedContentTypes
//          if pFType.count > 0 { NSOpen.allowedFileTypes   = pFType }  //๐ŸŽ20220318 Set Extention. โŒDesperated on macOS 12.0
            NSOpen.allowsMultipleSelection  = pMulti                    //๐ŸŽMultiple selection
            NSOpen.canChooseDirectories     = pDir                      //๐ŸŽFolder
            NSOpen.canChooseFiles           = pFile                     //๐ŸŽFile
            NSOpen.canCreateDirectories     = pCreateDir                //๐ŸŽCreate a new folder
            NSOpen.treatsFilePackagesAsDirectories = false              //๐ŸŽ
            let wModal = NSOpen.runModal()                              //๐ŸŽ
            if wModal.rawValue == NSApplication.ModalResponse.cancel.rawValue {//๐ŸŽ
                return []                                               //๐ŸŽโ™ป๏ธreturnโ™ป๏ธ Canceled: []
            } else if NSOpen.urls.count > 0 {                           //๐ŸŽ
                var wURL:[URL]              = []                        //๐ŸŽ20220318
                var wFType:[String]         = []                        //๐ŸŽ20220318
                for ftype in pFType {                                   //๐ŸŽ20220318 Convert every extention to โ€œ.โ€ + extention.
                    wFType.append("."+ftype)                            //๐ŸŽ20220318
                }                                                       //๐ŸŽ20220318
                for url in NSOpen.urls {                                //๐ŸŽ20220318 The filepath has one of extentions that are allowed?
                    let wA = Call.GetFileType(url, pTypeArray: pFType)  //๐ŸŽ20220318 
                    if wA != "" {                                       //๐ŸŽ20220318 <โ€”-Yes
                        wURL.append(url)                                //๐ŸŽ20220318 Save the URL
                    } else { }                                          //๐ŸŽ20220318 <โ€”-No
                }                                                       //๐ŸŽ20220318
                return wURL                                             //๐ŸŽโ™ป๏ธreturnโ™ป๏ธ Normal end
            }                                                           //๐ŸŽ
        }                                                               //๐ŸŽ
    }//-----------------------------------------------------------------//๐ŸŽ
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?