如何更改侧边栏的选择颜色?

How to change the selection color of a sidebar?

有没有办法改变侧边栏项目的选择颜色? 例如 macOS 10.14.x 深色主题的默认颜色是蓝色,是否可以更改此颜色? 我在这里看了一下: 但我很难翻译成 Applescript,在此先感谢。

如果没有示例或 MCVE 项目,提供直接解决方案将很棘手。您链接到的主题和答案覆盖了 NSTableViewNSTableRowView classes,因此我可以为您提供一个通用的解决方案. AppleScriptObjC 中的 Subclassing 可能会有点麻烦,具体取决于您需要引用的内容,但相当简单。本质上,您是在常规 class 和您的应用程序之间放置一个自定义 class,您可以在其中拦截各种标准方法调用。

对于基于单元格的table视图示例,使用文件>新建>文件向您的项目添加一个新的空文件。 .. 菜单项。 _file 的名称不重要(例如 TableViewHighlight.applescript 或其他),script 的名称将由 Xcode 使用。在这里,我使用 MyTableView 作为 class 名称,并从 AppDelegate 引用 tableView 出口 属性:

script MyTableView -- the name of your custom class

    property parent : class "NSTableView" -- the parent class to override
    property tableView : a reference to current application's NSApp's delegate's tableView
    property highlightColor : a reference to current application's NSColor's greenColor -- whatever

    # set the row highlight color
    on drawRow:row clipRect:clipRect
        if tableView's selectedRowIndexes's containsIndex:row then -- filter as desired
            highlightColor's setFill()
            current application's NSRectFill(tableView's rectOfRow:row)
        end if
        continue drawRow:row clipRect:clipRect -- super
    end drawRow:clipRect:

    # set the highlight color of stuff behind the row (grid lines, etc)
    on drawBackgroundInClipRect:clipRect
        highlightColor's setFill()
        current application's NSRectFill(clipRect)
        continue drawBackgroundInClipRect:clipRect -- super
    end drawBackgroundInClipRect:

end script

Interface Editor 中,使用 Identity Inspector 设置 table 视图的 class到 MyTableView class。最后,在您的 table 视图设置中将其突出显示设置为 none,因为它将由您的子 class 完成(再次假设 tableView 插座连接到 table 查看):

    tableView's setSelectionHighlightStyle: current application's NSTableViewSelectionHighlightStyleNone

对于基于视图的table视图示例,过程是相似的,但是NSTableRowView是子视图class。这里我使用的 script/class 的名称将是 MyTableRowView:

script MyTableRowView -- the name of your custom class

    property parent : class "NSTableRowView" -- the parent class to override
    property highlightColor : a reference to current application's NSColor's redColor -- whatever

    # draw the selected row
    on drawSelectionInRect:dirtyRect
        continue drawSelectionInRect:dirtyRect
        highlightColor's setFill()
        current application's NSRectFill(dirtyRect)
    end drawSelectionInRect:

end script

Interface Editor 中,使用 Attributes Inspector 将 table 视图的高亮设置为常规,并添加一个 tableView:rowViewForRow: 方法到 table 视图的委托:

on tableView:tableView rowViewForRow:row
    set rowIdentifier to "MyTableRow"
    set myRowView to tableView's makeViewWithIdentifier:rowIdentifier owner:me
    if myRowView is missing value then
        set myRowView to current application's MyTableRowView's alloc's initWithFrame:current application's NSZeroRect
        myRowView's setIdentifier:rowIdentifier
    end if
    return myRowView
end tableView:rowViewForRow:

当然还有其他选择,但这应该可以帮助您入门,并有助于翻译其中的一些内容 Objective-C answers/examples.