如何在 AppleScript 中使用侧边栏?

How to use a sidebar with AppleScript?

我想知道如何创建带有侧边栏的 window,基本上我想要的是一个侧边栏,可以通过单击名称打开一些 windows,如示例中所示下面,提前致谢。

您正在寻找的是一个 sidebar / source list - you can take a look at Apple’s older SidebarDemo 示例项目,用于使用大纲视图的相当小的示例(甚至可能相对容易转换为 ASObjC)。

取决于你最终想做什么,我想它可以用 AppleScript 来完成,但我认为你会发现应用程序越大,它就越痛苦。 AppleScript 非常适合原型设计和控制其他应用程序,但当事情开始变得庞大或复杂时(例如,您以前使用表格的经验)就不是那么好了。如果你打算做更严肃一点的应用程序,你可能会考虑使用一种也更严肃一点的语言。

好的,下面是在 Xcode AppleScript 应用程序中设置边栏的步骤。我只是展示功能;美化留给你玩

首先(显然)设置 GUI。打开 MainMenu.xib,并从对象库中添加三个项目:

  • A TableView在左侧window(显示侧边栏)
  • window右侧的自定义视图(显示详细视图)
  • 左侧对象列表中的数组控制器。

它应该看起来像这样(为清楚起见,红色注释): 您需要设置布局约束以保持侧边栏的宽度恒定并将自定义视图粘在其一侧,但您可以尝试一下。

现在我们将设置绑定和一些其他细节。首先,单击左侧的 Array Controller 对象,然后打开右侧的 Utilities 窗格。

  • 单击 'Attributes' 检查器。
    • 确保模式是 'class' 并且 'class name' 是 NSMutableDictionary
    • 向 'Keys' 部分添加两个名称:'title' 和 'isHeader'(这些是我们将在字典中使用的键)
    • 取消点击 'Select inserted objects',因为它很烦人。

接下来,单击列表中的 Table 视图,然后再次转到右侧的“实用程序”窗格。

  • 单击 'Attributes' 检查器并将列数设置为 1。
  • 单击 'Bindings' 检查器,转到 Table 内容部分,并创建两个绑定:
    • Content 中单击 Bind to 复选框,从弹出窗口中选择 Array Controller,并确保控制器键是 arrangedObjects
    • Selection Indexes 中进行相同的对象绑定,但使用 selectionIndexes 作为 Controller Key。
  • 点击 'Connections' 检查器,从 delegate outlet 拖动到左侧的 Delegate 对象,这样 table 视图将使用 AppDelegate 脚本作为它的 table 视图代表。

接下来,单击列表中的 Table 单元格视图,然后再次转到“实用程序”窗格。

  • 单击 'Identity' 检查器并将标识符设置为 'tableItem'(不带引号)。这让委托找到这个特定的视图并将其传递给 table.

最后,单击左侧的 Table View Cell 对象(不是它上面的 Table Cell View 对象,或者第二个 Table 查看它下方的单元格:令人困惑,我知道),然后再次转到右侧的“实用程序”窗格。

  • 单击 'Bindings' 检查器,转到顶部的“值”部分,然后创建一个绑定:
    • Value 中单击 Bind to 复选框,选择 Table Cell View 从弹出窗口中,将 Controller Key 留空,并将 Model Key Path 设置为 objectValue.title

它的工作方式是我们将字典列表转储到数组控制器中,Table 视图将拾取它并向每个 Table 单元格视图分配一个字典作为它的 objectValue,然后 Table View Cells 将提取数据并呈现它。

我们已经完成了 GUI,除了将它连接到脚本。现在,转到 Xcode 提供的 AppDelegate 脚本,您需要将其更改为如下所示:

script AppDelegate
    property parent : class "NSObject"

    -- IBOutlets
    property theWindow : missing value
    property arrayController : missing value
    property detailView : missing value

    on applicationWillFinishLaunching_(aNotification)
        -- Insert code here to initialize your application before any files are opened
        (* set up list of headers and lines for the side bar *)
        set sidebarList to {{title:"Header 1", isHeader:true}, {title:"Line 1", isHeader:false}, {title:"Header 2", isHeader:true}, {title:"Line 2", isHeader:false}, {title:"Line 3", isHeader:false}, {title:"Header 3", isHeader:true}, {title:"Line 4", isHeader:false}}
        arrayController's addObjects:sidebarList
    end applicationWillFinishLaunching_

    on applicationShouldTerminate_(sender)
        -- Insert code here to do any housekeeping before your application quits 
        return current application's NSTerminateNow
    end applicationShouldTerminate_

    (* table view delegate emthods *)

    on tableView:tableView isGroupRow:row
        -- header rows are group rows
        set rowData to arrayController's arrangedObjects's objectAtIndex:row
        return rowData's isHeader
    end

    on tableView:tableView shouldSelectRow:row
        -- don't want to select header rows
        set rowData to arrayController's arrangedObjects's objectAtIndex:row
        return not (rowData's isHeader)
    end

    on tableView:tableView viewForTableColumn:column row:row
        -- header rows get a special look
        set aView to tableView's makeViewWithIdentifier:"tableItem" owner:me
        return aView
    end

    on tableViewSelectionDidChange:aNotification
        (* 
         This is method gets notified right after a selection is made. This is one of 
         the places where you can change the detail view to show a the correct view for 
         selected sidebar item. For demonstration purposes I'm just swapping out 
         TextField views with the name of the sidebar item. Not to sophisticated, 
         but it get the point across
         *)
        set tableView to aNotification's object
        set selectedRowIdx to (tableView's selectedRow) as integer
        set rowData to arrayController's arrangedObjects's objectAtIndex:selectedRowIdx
        set newLabel to current application's NSTextField's labelWithString:(rowData's title )
        set newLabel's translatesAutoresizingMaskIntoConstraints to false
        set detailSubviews to (detailView's subviews) as list
        if detailSubviews is not {} then
            set oldLabel to item 1 of detailSubviews
            detailView's replaceSubview:oldLabel |with|:newLabel
        else
            detailView's addSubview:newLabel
        end

        set constraintX to newLabel's centerXAnchor's constraintEqualToAnchor:(detailView's centerXAnchor)
        set constraintY to newLabel's centerYAnchor's constraintEqualToAnchor:(detailView's centerYAnchor)
        constraintX's setActive:true
        constraintY's setActive:true
    end

end script

我添加了两个属性——arrayController 和 detailView,它们的值都为 'missing value'(表明它们是 IBOutlets)——我们将 link 提供给 GUI。我在 applicationWillFinishLaunching_ 中添加了两行以创建记录列表并将其添加到数组控制器,其余的是 Table 视图调用的委托方法。

要 link 启动 GUI,返回 MainMenu.xib,单击 Array Controller 对象,在 Utilities 窗格中打开 'Connections' 检查器,然后拖动一个 New Referencing Outlet到 Delegate 对象,将其与 属性 arrayController 连接。对自定义视图执行相同操作,将其连接到 属性 detailView。你应该可以走了。