読者です 読者をやめる 読者になる 読者になる

Cocoa API解説(macOS/iOS)

iOS , Mac アプリケーション開発のために使われる主要フレームワークの日本語情報です。

目次

setDesktopImageURL:forScreen:options:error:

INDEX>AppKit>NSWorkspace

デスクトップ画像をセットします

Objective-C

- (BOOL)setDesktopImageURL:(NSURL *)url
                 forScreen:(NSScreen *)screen
                   options:(NSDictionary *)options
                     error:(NSError **)error

Swift

func setDesktopImageURL(_ url: NSURL,
              forScreen screen: NSScreen,
                options options: [NSObject : AnyObject]!,
                  error error: NSErrorPointer) -> Bool

解説

デスクトップ画像をセットします

ImageScaleNone
f:id:jjj777:20150425211348p:plain

ImageScaleProportionallyDown
f:id:jjj777:20150425211453p:plain

ImageScaleProportionallyUpOrDown
f:id:jjj777:20150425211707p:plain

ImageScaleAxesIndependently
f:id:jjj777:20150425211912p:plain

返り値

Objective-C

- (BOOL)setDesktopImageURL:(NSURL *)url
                 forScreen:(NSScreen *)screen
                   options:(NSDictionary *)options
                     error:(NSError **)error

Swift

func setDesktopImageURL(_ url: NSURL,
              forScreen screen: NSScreen,
                options options: [NSObject : AnyObject]!,
                  error error: NSErrorPointer) -> Bool

引数

Objective-C

(NSURL *)url

Swift

_ url: NSURL

Objective-C

(NSScreen *)screen

Swift

screen: NSScreen

let NSWorkspaceDesktopImageScalingKey: String
let NSWorkspaceDesktopImageAllowClippingKey: String
let NSWorkspaceDesktopImageFillColorKey: String
OBJECTIVE-C
NSString * const NSWorkspaceDesktopImageScalingKey;
 NSImageScaling
 NSImageScaleProportionallyUpOrDown
 NSImageScaleProportionallyDown
NSString * const NSWorkspaceDesktopImageAllowClippingKey;
NSString * const NSWorkspaceDesktopImageFillColorKey;


Objective-C

(NSDictionary *)options

Swift

options: [NSObject : AnyObject]!,

Objective-C

(NSError **)error

Swift

error: NSErrorPointer

フレームワーク

ApplicationKit

クラス

NSWorkspace

使用可能

10.0

更新時のバージョン

OS X 10.10.3
Swift1.2

参照

関連記事(外部サイト)

例文

Objective-C

Swift

    //NSWorkspace setDesktopImageURL:forScreen:options:error:
    //デスクトップの画像を変える
    //引き伸ばし設定のみ実装、他のオプション未設定
    @IBAction func function039(sender: AnyObject) {
        //ファイルを選択
        var openPanel = NSOpenPanel()
        openPanel.allowsMultipleSelection = false
        openPanel.canChooseDirectories = false
        openPanel.canCreateDirectories = false
        openPanel.canChooseFiles = true
        openPanel.beginWithCompletionHandler { (result) -> Void in
            //オープンパネルでOKを選択したら
            if result == NSFileHandlingPanelOKButton {
                //選択したファイルを
                let theWorkspace : NSWorkspace = NSWorkspace.sharedWorkspace()
                let theURL : NSURL =  openPanel.URL!
                let filepath : NSString = theURL.path!
                var theError : NSError
                
                
                let num : NSNumber = NSNumber(unsignedLong:NSImageScaling.ImageScaleAxesIndependently.rawValue)
                
                //let num : NSNumber = NSNumber(unsignedLong:NSImageScaling.ImageScaleProportionallyUpOrDown.rawValue)
                
                //let num : NSNumber = NSNumber(unsignedLong:NSImageScaling.ImageScaleProportionallyDown.rawValue)

                //let num : NSNumber = NSNumber(unsignedLong:NSImageScaling.ImageScaleNone.rawValue)
                
                
                let dic : [ NSObject : AnyObject] = ["NSWorkspaceDesktopImageScalingKey" : num ]
                theWorkspace.setDesktopImageURL(theURL,
                    forScreen: NSScreen.mainScreen()!,
                    options: dic,
                    error: nil )

            }//if result
        }//openPanel.beginWithCompletionHandler
    }