设置背景图片位置

时间:2012-04-13 11:54:55

标签: objective-c macos app-store applescript

是否有任何符合Mac App Store标准的方式将桌面墙纸位置设置为“拉伸到填充屏幕”?

2 个答案:

答案 0 :(得分:4)

This code sample from Apple提供了对问题的良好概述以及如何在Cocoa中正确编码它。突出的代码片段如下所示:

- (void)updateScreenOptions:(NSScreen*)screen
{
    if (screen)
    {
        NSDictionary *screenOptions = [[NSWorkspace sharedWorkspace] desktopImageOptionsForScreen:curScreen];

        // the value is an NSNumber containing an NSImageScaling (scaling factor)
        NSNumber *scalingFactor = [screenOptions objectForKey:NSWorkspaceDesktopImageScalingKey];
        [scalingPopup selectItemAtIndex:[scalingFactor integerValue]];

        // the value is an NSNumber containing a BOOL (allow clipping)
        NSNumber *allowClipping = [screenOptions objectForKey:NSWorkspaceDesktopImageAllowClippingKey];
        [[clippingCheckbox cell] setState:[allowClipping boolValue]];

        // the value is an NSColor (fill color)
        NSColor *fillColorValue = [screenOptions objectForKey:NSWorkspaceDesktopImageFillColorKey];
        if (fillColorValue)
            [fillColor setColor:fillColorValue];
    }
}

正如some of the answer errata中所述,正确的方法在此处调用以解决您的问题似乎是[screenOptions setObject:scalingFactor forKey:NSWorkspaceDesktopImageScalingKey][[NSWorkspace sharedWorkspace] setDesktopImageURL:imageURL forScreen:curScreen options:screenOptions error:&error],它应该提供所需的功能。

我坦率地承认this was a collaborative effort,但似乎我们错误地找到了正确的解决方案。

(有关NSImageScaling枚举和NSWorkspace的其他信息,其中包含正确的缩放键。)

答案 1 :(得分:3)

不幸的是,没有符合Mac App Store标准的设置桌面图像选项的方法。他们只有与桌面图像有关的方法是:

– desktopImageURLForScreen:
– setDesktopImageURL:forScreen:options:error:
– desktopImageOptionsForScreen:

你可以通过执行AppleScript来做到这一点,但我不认为Apple会喜欢它。

希望这有帮助!

编辑:

[screenOptions setObject:scalingFactor forKey:NSWorkspaceDesktopImageScalingKey];
[[NSWorkspace sharedWorkspace] setDesktopImageURL:imageURL forScreen:curScreen options:screenOptions error:&error];